Practice: Node.js and PostgreSQL · PostgreSQL

Ch 1 · PostgreSQL Connection with Node.js — card 3 of 4.

Now write the database code yourself in Node.js. You will set up a connection pool, run a query that uses placeholders instead of pasted-in values, and carry out a transaction that cleans up after errors. A correct answer uses $1-style parameters, calls ROLLBACK when something goes wrong and always releases the client, whether the transaction succeeds or fails.

  1. Set up a connection pool
  2. Execute a parameterized query
  3. Run a transaction with error handling
const { Pool } = require("pg");
const pool = new Pool({ /* config */ });

// Query with parameters
// Transaction with BEGIN/COMMIT/ROLLBACK