왜 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"})`;
});
댓글 0