Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

docs: clarification on minimal example with full error handling

Đang mở
#3,328 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tài liệu
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, node.js, postgresql
Lĩnh vực
databases, documentation

Hướng nghiên cứu

Bắt đầu bằng việc xem lại các phần tài liệu hiện có bao quát Client, Pool, connect, query, end và error events. So sánh các phần đó với hai ví dụ tối thiểu trong issue; tài liệu được hoàn thiện khi giải thích rõ hành vi liên quan đến việc xử lý lỗi và những đánh đổi khi sử dụng Client hoặc Pool cho một truy vấn duy nhất.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

question

The docs somewhat talk about it in pieces, but it wasn't 100% clear what a minimal code example with full error handling looks like, so I was hoping to get some clarity around that. I believe the following are both minimal examples, but I have a few questions surrounding them.

Client example:

import pg from 'pg';

const client = new pg.Client();

await client.connect();

try {
  await client.query('SELECT $1::text as message', ['Hello world!']);
} finally {
  await client.end();
}

Pool example:

import pg from 'pg';

const pool = new pg.Pool();

try {
  await pool.query('SELECT $1::text as message', ['Hello world!']);
} finally {
  await pool.end();
}

My couple questions:

  1. Does either example need an actual event listener for error, or because I'm calling connect -> query -> end directly after each other, is it not possible for an error event to actually be emitted? I know in normal case errors that happen during those are rejected with the promise, but I wasn't sure if it was actually possible for an error event to be emitted between say the connect and query calls, etc.
  2. Is there any advantage or disadvantage to using Pool even if it's only a single call and will then be torn down? The only advantage I can see is it requires one less line of code since you don't need to explicitly call connect, but I didn't know if there was other overhead or considered an antipattern*?

* I know instantiating a bunch of pools is an antipattern, but think of a usecase more akin to lambda or something, where it's not possible to keep a pool active, so I'm basically just spinning up some code, running a single query, then spinning everything down. Does using Pool have any negatives for that use case, or does defaulting to Pool over Client make sense?

Thanks for any info/help!

Ngôn ngữ chính
JavaScript
Star
13.2k
Fork
1.4k
Merge trung bình
6 ngày 15 giờ
Pull request đã merge (30 ngày)
6

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của brianc/node-postgres

Tất cả issue của brianc/node-postgres

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.