Drizzle <> PostgreSQL
Drizzle 通过 node-postgres
和 postgres.js
驱动程序原生支持 PostgreSQL 连接。
我们在使用 node-postgres
和 postgres.js
驱动程序并将其与 Drizzle ORM 集成时,发现了一些差异。例如:
- 使用
node-postgres
,您可以安装pg-native
将node-postgres
和 Drizzle 的速度提高约 10%。 node-postgres
支持在每个查询的基础上提供类型解析器,而无需全局修补。有关更多详细信息,请参阅类型文档。postgres.js
默认使用预编译语句,您可能需要选择禁用它。这在 AWS 环境中可能会是一个潜在问题,请牢记这一点。- 如果您想贡献其他内容,我们欢迎您在此处提交 PR:这里
node-postgres
步骤 1 - 安装包
npm
yarn
pnpm
bun
npm i drizzle-orm pg
npm i -D drizzle-kit @types/pg
步骤 2 - 初始化驱动程序并执行查询
node-postgres
带配置的 node-postgres
// Make sure to install the 'pg' package
import { drizzle } from 'drizzle-orm/node-postgres';
const db = drizzle(process.env.DATABASE_URL);
const result = await db.execute('select 1');
如果您需要提供现有驱动程序
// Make sure to install the 'pg' package
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
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');
postgres.js
步骤 1 - 安装包
npm
yarn
pnpm
bun
npm i drizzle-orm postgres
npm i -D drizzle-kit
步骤 2 - 初始化驱动并执行查询
postgres.js
带配置的 postgres.js
import { drizzle } from 'drizzle-orm/postgres-js';
const db = drizzle(process.env.DATABASE_URL);
const result = await db.execute('select 1');
如果您需要提供现有驱动程序
// Make sure to install the 'postgres' package
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
const queryClient = postgres(process.env.DATABASE_URL);
const db = drizzle({ client: queryClient });
const result = await db.execute('select 1');