Skip to main content
Version: 3.0 Beta

Neon

Neon can be used with the regular pg driver as described in the PostgreSQL guide. It also provides a serverless driver that uses HTTP/WebSocket protocol which works in edge environments. This guide explains how to use the serverless driver with ZenStack.

Installing driver​

npm install @neondatabase/serverless

Creating ZenStackClient​

import { schema } from '@/zenstack/schema';
import { Pool } from '@neondatabase/serverless';
import { ZenStackClient } from '@zenstackhq/orm';
import { PostgresDialect } from '@zenstackhq/orm/dialects/postgres';

const db = new ZenStackClient(schema, {
dialect: new PostgresDialect({
pool: new Pool({
connectionString: process.env.DATABASE_URL,
}),
}),
});

Notes about edge runtime​

Edge environments are ephemeral and often only live to serve a single request. Instead of holding a global singleton client instance, you should create a new one per request. The serverless driver is designed to be used this way.

export async function handler(req: Request): Promise<Response> {
const db = createClient();
// use db...
await db.$disconnect();
return new Response('ok');
}
Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below