RepositoryJavaScriptIEIreland
vitaly-t

vitaly-t/pg-promise

PostgreSQL interface for Node.js

3.5ksao
220fork
1issue
0lượt xem

🐘 pg-promise — Giao diện PostgreSQL mạnh mẽ cho Node.js

Built on top of node-postgres, nhưng thêm nhiều hơn chỉ là promises — auto connections, auto transactions, và query formatting engine.

pg-promise là thư viện giao diện PostgreSQL cho Node.js, được xây dựng trên nền [node-postgres] và ra đời từ năm 2015. Ban đầu chỉ thêm support cho promises cho driver gốc, nhưng đến nay thư viện đã mở rộng đáng kể — promises giờ chỉ là một phần nhỏ trong tổng thể chức năng.

Thư viện hoạt động trên Node.js 16–24 và tương thích PostgreSQL 10–18, được viết bằng JavaScript với hỗ trợ TypeScript đầy đủ.

✅ Điểm nổi bật

  • Tự động kết nối: quản lý connection pool mà không cần viết mã quản lý thủ công.
  • Tự động transaction: hỗ trợ transaction lồng nhau với conditional transaction.
  • Query formatting engine: engine formatting nội bộ với index variables ($1, $2), named parameters ($*propName*), và nhiều filter (JSON, CSV, SQL Names, Raw Text, Open Values).
  • Result-specific methods: [none], [one], [oneOrNone], [many], [manyOrNone], [any] — mỗi method tự động validate số dòng trả về, từ chối nếu không đúng kỳ vọng.
  • Task & Transaction protocol: [task], [tx], [txIf] cho shared connections + automatic transactions, kèm [batch], [page], [sequence].
  • External SQL files: hỗ trợ tốt cho việc tổ chức query trong file riêng, tách biệt logic SQL khỏi code.
  • Global events reporting: central handling cho việc giám sát và log truy vấn.
  • Customizable protocol: mở rộng protocol thông qua event extend.

💡 Vì sao đáng chú ý

pg-promise giải quyết vấn đề cốt lõi khi làm việc với PostgreSQL trong Node.js: quản lý connection đúng cách và tránh SQL injection. Thư viện nhấn mạnh rằng tuyệt đối không dùng ES6 template strings hay string concatenation để tạo query — chỉ query formatting engine của nó mới biết cách escape giá trị đúng cách cho PostgreSQL.

Điểm mạnh nhất nằm ở cách tiếp cận declarative cho result handling: thay vì tự validate số dòng trả về, developer chọn method phù hợp và library tự động reject nếu dữ liệu không khớp. Kết hợp với transaction nesting, external SQL files, và stream support, pg-promise là một giải pháp toàn diện cho ứng dụng Node.js cần tương tác PostgreSQL ở quy mô thực tế.


#PostgreSQL #NodeJS #Promises #TypeScript #TiniX

Dự án tương tự

Khám phá các dự án và mô hình AI khác có sự tương đồng.

RepositoryTypeScript
freeCodeCamp/freeCodeCamp

freeCodeCamp.org's open-source codebase and curriculum. Learn math, programming, and computer science for free.

459.9k
Dec 2, 2026
RepositoryRust
oven-sh/bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one

97.1k
Nov 29, 2026
RepositoryTypeScript
nestjs/nest

A progressive TypeScript framework for Node.js backends: from a small API in one file to efficient, scalable, enterprise-grade systems 🚀

77.2k
Nov 30, 2026
RepositoryTypeScript
prisma/orm

The Prisma ORM: write your application in TypeScript with human-readable queries instead of SQL. Host your database on Prisma Postgres and your app on Prisma Compute.

47.8k
Nov 29, 2026
RepositoryJavaScript
Chalarangelo/30-seconds-of-code

Coding articles to level up your development skills

129.6k
Nov 17, 2026
RepositoryJavaScript
nodejs/node

Node.js JavaScript runtime ✨🐢🚀✨

124.1k
Nov 30, 2026
RepositoryJavaScript
react/react

The library for web and native user interfaces.

253.0k
Nov 30, 2026
RepositoryJavaScript
trekhleb/javascript-algorithms

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

196.8k
Jul 26, 2026
RepositoryC++
electron/electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS

124.3k
Dec 2, 2026