Drizzle <> PostgreSQL
This guide assumes familiarity with:
- Drizzle๊ณผ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ๊ธฐ๋ณธ์ฌํญ
- node-postgres ๊ธฐ๋ณธ์ฌํญ
- postgres.js ๊ธฐ๋ณธ์ฌํญ
Drizzle๋ node-postgres ๋ฐ postgres.js ๋๋ผ์ด๋ฒ๋ฅผ ์ฌ์ฉํ์ฌ PostgreSQL ์ฐ๊ฒฐ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ํฉ๋๋ค.
๋ ๋๋ผ์ด๋ฒ๋ฅผ ๋ชจ๋ ์ฌ์ฉํ๊ณ Drizzle ORM๊ณผ ํตํฉํ๋ ๋์ ๋ฐ๊ฒฌํ node-postgres์ postgres.js ๋๋ผ์ด๋ฒ ์ฌ์ด์๋ ๋ช ๊ฐ์ง ์ฐจ์ด์ ์ด ์์ต๋๋ค. ์๋ฅผ ๋ค์ด:
node-postgres๋ฅผ ์ฌ์ฉํ๋ฉดpg-native๋ฅผ ์ค์นํ์ฌnode-postgres์ Drizzle์ ์๋๋ฅผ ์ฝ 10% ํฅ์์ํฌ ์ ์์ต๋๋ค.node-postgres๋ ์ ์ญ์ ์ผ๋ก ํจ์นํ์ง ์๊ณ ๋ ์ฟผ๋ฆฌ๋ณ๋ก ํ์ ํ์๋ฅผ ์ ๊ณตํ๋ ๊ฒ์ ์ง์ํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ Types Docs๋ฅผ ์ฐธ์กฐํ์ธ์.postgres.js๋ ๊ธฐ๋ณธ์ ์ผ๋ก prepared statements๋ฅผ ์ฌ์ฉํ๋ฉฐ, ์ด๋ฅผ ์ตํธ ์์ํด์ผ ํ ์ ์์ต๋๋ค. ์ด๋ 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 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');