Type Mapping, Dates, Big Numbers, and Errors

Database types do not always map one-to-one to JavaScript types. PostgreSQL BIGINT values may exceed JavaScript's safe integer range, so drivers often return them as strings unless configured otherwise. NUMERIC values are frequently returned as strings to avoid precision loss. TIMESTAMPTZ values need deliberate handling of time zones, and database errors should be mapped to safe application responses without leaking sensitive SQL or data.

Treat driver behavior as part of your application's contract. Test boundary values and avoid silently converting large IDs or exact decimals to JavaScript number when precision matters.

Handle a numeric value deliberately
const { rows } = await pool.query(
  "SELECT id, price FROM products WHERE id = $1",
  [productId]
);

// Depending on driver configuration, BIGINT/NUMERIC may be strings.
const product = rows[0];
const priceAsText = product.price;
💡 Keep exact decimal values as strings or use a suitable decimal library when arithmetic must remain exact. Convert only when the target type's precision limits are acceptable.
Key Takeaways
  • BIGINT and NUMERIC may be returned as strings by drivers.
  • JavaScript number has finite integer precision.
  • Map database errors to safe API errors without exposing secrets.
Knowledge Check

Why should a monetary NUMERIC value not be blindly converted to JavaScript number?