Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

docs: clarification on minimal example with full error handling

Aperta
#3,328 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Documentazione
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
javascript, node.js, postgresql

Direzione di ricerca

Inizia esaminando le sezioni esistenti della documentazione che riguardano Client, Pool, connect, query, end ed error events. Confronta queste sezioni con i due esempi minimi nell'issue; la documentazione è completa quando spiega chiaramente il comportamento rilevante nella gestione degli errori e i compromessi dell'utilizzo di Client o Pool per una singola query.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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!

Lingua principale
JavaScript
Stelle
13.2k
Fork
1.4k
Merge medio
6g 15h
PR unite (30g)
6

Preparare l'ambiente

Non abbiamo ancora controllato i file di configurazione di questo progetto. Parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di brianc/node-postgres

Tutte le issue di brianc/node-postgres

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.