Drizzle <> PostgreSQL

This guide assumes familiarity with:

Drizzle๋Š” node-postgres ๋ฐ postgres.js ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ PostgreSQL ์—ฐ๊ฒฐ์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

๋‘ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๊ณ  Drizzle ORM๊ณผ ํ†ตํ•ฉํ•˜๋Š” ๋™์•ˆ ๋ฐœ๊ฒฌํ•œ node-postgres์™€ postgres.js ๋“œ๋ผ์ด๋ฒ„ ์‚ฌ์ด์—๋Š” ๋ช‡ ๊ฐ€์ง€ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด:

node-postgres

1๋‹จ๊ณ„ - ํŒจํ‚ค์ง€ ์„ค์น˜

npm
yarn
pnpm
bun
npm i drizzle-orm pg
npm i -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 { 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 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');

๋‹ค์Œ ๋‹จ๊ณ„๋Š”?