Skip to content

Unlimited seats on Netlify Pro for $20/month → Learn more 👥

Tooling

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 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:

Terminal window
npm install @netlify/database drizzle-orm@beta
npm install -D drizzle-kit@beta

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:

drizzle.config.ts
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:

Terminal window
npx drizzle-kit generate

Define your database schema using Drizzle’s schema builder:

db/schema.ts
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(),
});

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.

db/index.ts
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:

netlify/functions/api.ts
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",
};

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:

  1. Using the getConnectionString() method from the @netlify/database module
  2. Reading the NETLIFY_DB_URL environment variable, which is available across builds, agent runners, functions and edge functions

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");

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`;