A minimalist todo application built with Next.js and TiDB Cloud, demonstrating how to build a full-stack application with serverless database capabilities using Drizzle ORM.
Visit https://tidbcloud-example-with-nextjs-todo.vercel.app/ to experience the demo.
- Sign up for a TiDB Cloud account
- Create a Serverless Tier cluster (free)
- Get your connection details from the TiDB Cloud console
Create a .env
file in the root directory with your TiDB Cloud connection string:
DATABASE_URL=mysql://{user}:{password}@{host}:{port}/{database}
The connection string can be found in your TiDB Cloud console under the "Connect" button for your cluster. Make sure to:
- Include your database name at the end of the URL
- URL encode any special characters in the password
The project uses Drizzle ORM with the following schema:
import { mysqlTable, int, text, boolean, timestamp } from "drizzle-orm/mysql-core";
export const todo = mysqlTable("todo", {
id: int("id").primaryKey().autoincrement(),
title: text("title").notNull(),
completed: boolean("completed").notNull().default(false),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});
To create the table, run the following SQL in the SQL Editor of your TiDB Cloud web console, you can replace todo_app
with your own database name if you want to use a different database name:
CREATE DATABASE IF NOT EXISTS `todo_app`;
use `todo_app`;
CREATE TABLE `todo` (
`id` int AUTO_INCREMENT NOT NULL,
`title` text NOT NULL,
`completed` boolean NOT NULL DEFAULT false,
`created_at` timestamp DEFAULT (now()),
`updated_at` timestamp DEFAULT (now()),
CONSTRAINT `todo_id` PRIMARY KEY(`id`)
);
pnpm install
pnpm dev
Visit http://localhost:3000
to see your todo app in action.
- Create, read, update, and delete todos
- Mark todos as complete/incomplete
- Server-side rendering with Next.js
- Serverless database with TiDB Cloud
- Type-safe database queries with Drizzle ORM
- Optimistic UI updates
├── src/
│ ├── server/
│ │ ├── db/
│ │ │ ├── index.ts # Database connection
│ │ │ └── schema.ts # Drizzle schema
│ ├── components/ # React components
│ └── pages/ # Next.js pages
└── .env # Environment variables
The easiest way to deploy is using Vercel:
- Push your code to GitHub
- Import your repository in Vercel
- Add your
DATABASE_URL
environment variable in Vercel project settings - Deploy!