Drizzle <> PostgreSQL

本指南假定您熟悉

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

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

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

接下来是什么?