This tutorial demonstrates how to build Todo app using Drizzle ORM with Neon database and Next.js.
Backend
Install Drizzle Kit and Drizzle ORM
The first step is to install Drizzle Kit and Drizzle ORM in your project.
Here, we’re installing the necessary packages: drizzle-orm for database interactions and @neondatabase/serverless for working with the Neon database. The drizzle-kit package is used as a dev-dependency for handling configuration and migrations.
Connect Drizzle ORM to the Neon database
In this snippet, we import functionality from @neondatabase/serverless and drizzle-orm, establish a connection to the Neon database, and initialize Drizzle.
Setup Drizzle config file
Then, set up the Drizzle configuration. Drizzle config is a configuration file used by Drizzle Kit. It contains all the information about your database connection, migration folder, and schema files.
This configuration file specifies the database schema location, path for generating migrations, and database connection through the connection string.
Declare todos schema
Here we define the todo table with fields id, text, and done, using data types from Drizzle ORM.
Push your schema changes directly to the database without generating any migrations files using drizzle-kit push command:
Now, we’re ready to write queries using Drizzle ORM.
Functions
Add todo:
The addTodo function inserts a new record into the todo table.
Get todos:
Here, we fetch all todos, sorted by their identifier.
Edit todo:
The editTodo function updates the text of a todo by its identifier.
Toggle todo:
This method toggles the status of a todo to its opposite state.
Delete todo:
The deleteTodo function removes a todo by its identifier.
Add todo
Get todos
Edit todo
Toggle todo
Delete todo
Frontend
Initiate Next.js app with Tailwind CSS
Initiate a new Next.js application with Tailwind CSS support using the npx create-next-app@latest command. Name your project when prompted, navigate to the project directory, and start the development server with npm run dev. Your Next.js app is now set up for customization and styling with Tailwind CSS.
Define a TypeScript type
Define a TypeScript type for a todo item with three properties: id of type number, text of type string, and done of type boolean. This type, named todoType, represents the structure of a typical todo item within your application.
Create a home page for a to-do application
Todos.tsx:
Create Todos components that represents the main interface of a Todo app. It manages the state of todo items, provides functions for creating, editing, toggling, and deleting todos, and renders the individual todo items using the Todo component.
Todo.tsx:
Create a Todo component that represents a single todo item. It includes features for displaying and editing the todo text, marking it as done with a checkbox, and providing actions for editing, saving, canceling, and deleting the todo.
AddTodo:
The AddTodo component provides a simple form for adding new todo items to the Todo app. It includes an input field for entering the todo text and a button for triggering the addition of the new todo.
Todos.tsx
Todo.tsx
AddTodo.tsx
Establish server-side functions
In this step, we establish server-side functions in the todoActions file to handle crucial operations on todo items:
getData:
Fetches all existing todo items from the database.
addTodo:
Adds a new todo item to the database with the provided text.
Initiates revalidation of the home page using revalidatePath("/").
deleteTodo:
Removes a todo item from the database based on its unique ID.
Triggers a revalidation of the home page.
toggleTodo:
Toggles the completion status of a todo item, updating the database accordingly.
Revalidates the home page after the operation.
editTodo:
Modifies the text of a todo item identified by its ID in the database.