import { drizzle } from 'drizzle-orm/node-postgres';const db = drizzle(process.env.DATABASE_URL);const result = await db.execute('select 1');
import { drizzle } from 'drizzle-orm/node-postgres';const db = drizzle({ connection: { connectionString: process.env.DATABASE_URL, ssl: true }});const result = await db.execute('select 1');
import { drizzle } from "drizzle-orm/node-postgres";import { Pool } from "pg";const pool = new Pool({ connectionString: process.env.DATABASE_URL,});const db = drizzle({ client: pool });const result = await db.execute("select 1");
bun add drizzle-orm @neondatabase/serverless -D drizzle-kit
第 2 步 - 初始化驱动并执行查询
Neon HTTP
Neon WebSockets
import { neon, neonConfig } from '@neondatabase/serverless';import { drizzle } from 'drizzle-orm/neon-http';// PlanetScale Postgres 连接所需neonConfig.fetchEndpoint = (host) => `https://${host}/sql`;const sql = neon(process.env.DATABASE_URL!);const db = drizzle({ client: sql });const result = await db.execute('select 1');
import { Pool, neonConfig } from '@neondatabase/serverless';import { drizzle } from 'drizzle-orm/neon-serverless';// PlanetScale Postgres 连接所需neonConfig.pipelineConnect = false;neonConfig.wsProxy = (host, port) => `${host}/v2?address=${host}:${port}`;const pool = new Pool({ connectionString: process.env.DATABASE_URL });const db = drizzle({ client: pool });const result = await db.execute('select 1');
// 适用于 Node.js 环境 - 需安装 'ws' 包import ws from "ws";import { Pool, neonConfig } from "@neondatabase/serverless";import { drizzle } from "drizzle-orm/neon-serverless";neonConfig.webSocketConstructor = ws;// PlanetScale Postgres 连接所需neonConfig.pipelineConnect = false;neonConfig.wsProxy = (host, port) => `${host}/v2?address=${host}:${port}`;const pool = new Pool({ connectionString: process.env.DATABASE_URL });const db = drizzle({ client: pool });const result = await db.execute("select 1");