Drizzle <> PostgreSQL

This guide assumes familiarity with:

Drizzle 原生支持与 node-postgrespostgres.js 驱动程序的 PostgreSQL 连接。

我们在使用 node-postgrespostgres.js 驱动程序并将它们与 Drizzle ORM 集成时发现了它们之间的一些差异。例如:

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

下一步是什么?