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 -D drizzle-kit @types/pg
步骤 2 - 初始化驱动程序并进行查询
node-postgres
node-postgres with config
// 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 -D drizzle-kit
步骤 2 - 初始化驱动程序并进行查询
postgres.js
postgres.js with config
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');