nepalcargoservices.com

Connecting Neon with Drizzle in Next.js: A User-Friendly Guide

Written on

Chapter 1: Introduction to Drizzle

Drizzle stands out as an Object Relational Mapping (ORM) tool within the TypeScript ecosystem. While I have frequently utilized Prisma and found it to be quite effective, Drizzle offers a fresh perspective and a slightly different approach. In this section, we will explore how to establish a connection and define a schema in Drizzle.

Note: Neon operates on Postgres

Creating a Database

We can utilize a single database for free. Simply navigate to the project dashboard and initiate a new project. Once created, you will be presented with a connection string. Copy this string and save it in a .env file. Here’s a sample:

DATABASE_URL="postgresql://drizzle_owner:[email protected]/drizzle?sslmode=require"

At this point, we can set Neon aside and proceed to the Next.js setup.

Installing Required Dependencies

To get started, run the following commands in your terminal:

npm i drizzle-orm @neondatabase/serverless

npm i -D drizzle-kit

npm i dotenv

npm i -D pg

Db Instance Setup

Create a folder named db in the root directory and add a drizzle.ts file. This is where we will register our schema soon.

import { neon } from "@neondatabase/serverless";

import { drizzle } from "drizzle-orm/neon-http";

const sql = neon(process.env.DATABASE_URL!);

// @ts-ignore

const db = drizzle(sql);

export default db;

Defining the Schema

To verify that everything is functioning correctly, let’s create a basic todo schema.

import { pgTable, serial, text } from "drizzle-orm/pg-core";

export const todos = pgTable("todos", {

id: serial("id").primaryKey(),

label: text("label").notNull(),

});

Updating the Database Instance

Now, let’s create a Neon DB instance. Modify drizzle.ts as follows:

import { neon } from "@neondatabase/serverless";

import { drizzle } from "drizzle-orm/neon-http";

import * as schema from "./schema"; // new

const sql = neon(process.env.DATABASE_URL!);

// @ts-ignore

const db = drizzle(sql, { schema }); // updated

export default db;

Configuring Drizzle

Next, we need to establish the Drizzle configuration. This informs Drizzle that we have defined a schema.

import "dotenv/config";

import type { Config } from "drizzle-kit";

export default {

schema: "./db/schema.ts",

out: "./drizzle",

driver: "pg",

dbCredentials: {

connectionString: process.env.DATABASE_URL!,

},

} satisfies Config;

Database Migration

We can now propagate our schema to the database.

npx drizzle-kit push:pg

If all goes well, you should see a success message. You can verify the changes directly in Neon or through Drizzle Studio.

npm run db:studio

Conclusion

Setting up a database with Drizzle is straightforward, and I appreciate the direction it is heading. In the upcoming tutorial, I will guide you on how to interact with the database programmatically.

If you enjoyed this article and wish to join our expanding community, please hit the follow button so we can continue this journey of knowledge together. Your thoughts and feedback are greatly appreciated, so feel free to share!

Stackademic 🎓

Thank you for reading to the end. Before you leave, please consider giving a clap and following the author! 👏

Follow us on X | LinkedIn | YouTube | Discord

Check out our other platforms: In Plain English | CoFeed | Venture | Cubed

Explore more content at Stackademic.com

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Unlock the Transformative Power of Rising Early

Discover the unexpected advantages of waking up early and how to make it a rewarding part of your daily routine.

The Urgent Need to Break the Habit of Prolonged Sitting

Discover why breaking the habit of prolonged sitting is crucial for your health and how small changes can lead to better well-being.

Modern Python Programming: Key Principles for 2022 and Beyond

Explore essential principles to write contemporary Python software effectively, leveraging the latest features and tools.