Database2025년 12월 1일2분 읽기

Postgres.js — Node.js 최고의 PostgreSQL 클라이언트

YS
YoungSam
조회 383

왜 Postgres.js인가

가장 빠른 PostgreSQL 클라이언트입니다. 태그드 템플릿으로 SQL 인젝션 안전한 쿼리를 작성합니다.

기본 사용법

import postgres from "postgres";
const sql = postgres("postgres://localhost/mydb");

// 안전한 파라미터화 쿼리
const users = await sql`
  SELECT * FROM users
  WHERE age > ${18}
  AND name LIKE ${`%Kim%`}
  ORDER BY created_at DESC
  LIMIT ${10}
`;

// 트랜잭션
await sql.begin(async (tx) => {
  const [user] = await tx`INSERT INTO users (name) VALUES (${"Kim"}) RETURNING *`;
  await tx`INSERT INTO logs (user_id, action) VALUES (${user.id}, ${"created"})`;
});
PostgreSQLNode.jsDatabase

댓글 0

아직 댓글이 없습니다.