You should have installed the dotenv package for managing environment variables.
npm
yarn
pnpm
bun
IMPORTANT
In case you face the issue with resolving dependencies during installation:
If you’re not using React Native, forcing the installation with --force or --legacy-peer-deps should resolve the issue. If you are using React Native, then you need to use the exact version of React which is compatible with your React Native version.
Setup Neon and Drizzle ORM
Create a new Neon project
Log in to the Neon Console and navigate to the Projects section. Select a project or click the New Project button to create a new one.
Your Neon projects come with a ready-to-use Postgres database named neondb. We’ll use it in this tutorial.
Setup connection string variable
Navigate to the Connection Details section in the project console to find your database connection string. It should look similar to this:
Add the DATABASE_URL environment variable to your .env or .env.local file, which you’ll use to connect to the Neon database.
Connect Drizzle ORM to your database
Create a drizzle.ts file in your src/db folder and set up your database configuration:
Declare todo schema
Here we define the todo table with fields id, text, and done, using data types from Drizzle ORM.
Setup Drizzle config file
Drizzle config - a configuration file that is used by Drizzle Kit and contains all the information about your database connection, migration folder and schema files.
Create a drizzle.config.ts file in the root of your project and add the following content:
Applying changes to the database
You can generate migrations using drizzle-kit generate command and then run them using the drizzle-kit migrate command.
Generate migrations:
These migrations are stored in the drizzle/migrations directory, as specified in your drizzle.config.ts. This directory will contain the SQL files necessary to update your database schema and a meta folder for storing snapshots of the schema at different migration stages.
Push command is good for situations where you need to quickly test new schema designs or changes in a local development environment, allowing for fast iterations without the overhead of managing migration files.
Establish server-side functions
In this step, we establish server-side functions in the src/actions/todoAction.ts 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.
Initiates a revalidation of the home page.
Setup home page with Next.js
Define a TypeScript type
Define a TypeScript type for a todo item in src/types/todoType.ts 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
src/components/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.
src/components/addTodo.tsx:
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.
src/components/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
addTodo.tsx
todos.tsx
Update the page.tsx file in the src/app folder to fetch the todo items from the database and render the Todos component: