@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 it with 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
In
db.ts, connect to the Neon database with 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
Define a schema for the database with Drizzle ORM primitives. Create a
schema.ts file and add this code.
Then use the
drizzle-kit CLI to generate an initial SQL migration.
The command creates a
drizzle directory containing a .sql migration file and a meta directory.
File Tree
Execute these migrations with a
migrate.ts script. The script opens a new connection to the Neon database and executes all unexecuted migrations in the drizzle directory.
migrate.ts
Run the script with
bun.
terminal
Now add some data to the database. Create a
seed.ts file with the following contents.
Then run this file.
terminal
The database now has a schema and sample data. Query it with Drizzle by replacing the contents of
index.ts with the following.
Then run the file. It prints the three authors you 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 complete documentation.