@neondatabase/serverless driver or through an ORM like Drizzle.
Drizzle ORM supports both a SQL-like “query builder” API and an ORM-like Queries API. Get started by creating a project directory, initializing the directory using bun init, and installing Drizzle and the Neon serverless driver.
terminal
Create a
.env.local file and add your Neon Postgres connection string to it.
.env.local
We will connect to the Neon database using the Neon serverless driver, wrapped in a Drizzle database instance.
To see the database in action, add these lines to
index.ts.
Then run
index.ts with Bun.
terminal
We can define a schema for our database using Drizzle ORM primitives. Create a
schema.ts file and add this code.
We then use the
drizzle-kit CLI to generate an initial SQL migration.
This creates a new
drizzle directory containing a .sql migration file and meta directory.
File Tree
We can execute these migrations with a simple
migrate.ts script. This script creates a new connection to the Neon database and executes all unexecuted migrations in the drizzle directory.
migrate.ts
We can run this script with
bun to execute the migration.
terminal
We can now add some data to our database. Create a
seed.ts file with the following contents.
Then run this file.
terminal
We now have a database with a schema and sample data. We can use Drizzle to query it. Replace the contents of
index.ts with the following.
Then run the file. You should see the three authors we inserted.
terminal
This example used the Neon serverless driver’s SQL-over-HTTP functionality. Neon’s serverless driver also exposes
Client and Pool constructors to enable sessions, interactive transactions, and node-postgres compatibility. Refer to Neon’s documentation for a complete overview.
Refer to the Drizzle website for more documentation on using the Drizzle ORM.