While we aim to provide the best experience possible with the native @netlify/database module, we integrate with the best tooling in the community, so that you can choose the workflow that is right for you.
In this section, we’ll cover some tools that have first-class support for Netlify Database and describe how you can bring your own.
Drizzle ORM
Section titled “Drizzle ORM”Drizzle ORM is a TypeScript ORM that lets you define your database schema in code and interact with it using a type-safe query builder. It also includes Drizzle Kit, a CLI companion for generating SQL migrations from your schema changes.
To use Drizzle ORM with Netlify Database, start by installing drizzle-orm alongside @netlify/database:
npm install @netlify/database drizzle-orm@betanpm install -D drizzle-kit@betaConfiguring migrations
Section titled “Configuring migrations”By default, Drizzle Kit outputs migrations to a drizzle directory. For Netlify Database migrations to work, you need to change this to your configured migrations directory so that Netlify can apply them automatically.
You can do this by setting the out property in your Drizzle config:
import { defineConfig } from "drizzle-kit";
export default defineConfig({ dialect: "postgresql", schema: "./db/schema.ts", out: "netlify/database/migrations",});Then, generate migrations with Drizzle Kit whenever you change your schema:
npx drizzle-kit generateSchema definition
Section titled “Schema definition”Define your database schema using Drizzle’s schema builder:
import { pgTable, serial, text, timestamp } from "drizzle-orm/pg-core";
export const users = pgTable("users", { id: serial().primaryKey(), name: text().notNull(), email: text().notNull().unique(), createdAt: timestamp().defaultNow(),});Querying
Section titled “Querying”Drizzle ORM has a native Netlify Database adapter. Import drizzle from drizzle-orm/netlify-db and call it with your schema — the connection is configured automatically.
import { drizzle } from "drizzle-orm/netlify-db";import * as schema from "./schema";
export const db = drizzle({ schema });Then use the Drizzle client in your application code:
import { desc } from "drizzle-orm";import type { Config, Context } from "@netlify/functions";import { db } from "../../db";import { users } from "../../db/schema";
export default async (req: Request, context: Context) => { if (req.method === "GET") { const allUsers = await db.select().from(users).orderBy(desc(users.createdAt));
return Response.json(allUsers); }
if (req.method === "POST") { const { name, email } = await req.json(); const [user] = await db.insert(users).values({ name, email }).returning();
return Response.json(user, { status: 201 }); }
return new Response("Method not allowed", { status: 405 });};
export const config: Config = { path: "/api/users",};Database drivers
Section titled “Database drivers”You can use any Postgres-compatible database driver or ORM to interact with Netlify Database. For that, you can access the full database connection string in one of two ways:
- Using the
getConnectionString()method from the@netlify/databasemodule - Reading the
NETLIFY_DB_URLenvironment variable, which is available across builds, agent runners, functions and edge functions
Example: pg
Section titled “Example: pg”This example uses the @netlify/database module to obtain the connection string and the pg module to query the database.
import { getConnectionString } from "@netlify/database";import pg from "pg";
const pool = new pg.Pool({ connectionString: getConnectionString() });const { rows: users } = await pool.query("SELECT * FROM users");Example: postgres
Section titled “Example: postgres”This example uses the postgres module to query database, retrieving the connection string from the NETLIFY_DB_URL environment variable.
import process from "node:process";import postgres from "postgres";
const sql = postgres(process.env.NETLIFY_DB_URL);const users = await sql`SELECT * FROM users`;Did you find this doc useful?
Your feedback helps us improve our docs.