Connect from JavaScript and TypeScript

Use node-postgres with PLOMID — connect, parameterized queries, transactions, and cleanup.

Version
Latest
v0.1.0 · latest 1 min read
On this page
  1. Prerequisites
  2. Complete workflow
  3. Notes and gotchas
  4. Pooling

Status: Supported with node-postgres (pg, verified end-to-end including parameterized vector queries).

Prerequisites#

shsource
npm install pg

Server running locally (see Quickstart). Credentials below come from environment variables.

Complete workflow#

javascriptsource
import { Client } from "pg";
 
const client = new Client({
  host: process.env.PLOMID_HOST ?? "127.0.0.1",
  port: Number(process.env.PLOMID_PORT ?? 5432),
  database: process.env.PLOMID_DB ?? "plomid",
  user: process.env.PLOMID_USER,
  password: process.env.PLOMID_PASSWORD,
});
 
await client.connect();
try {
  // Parameterized query — always use $n params, never string interpolation.
  const { rows } = await client.query(
    "SELECT id, name FROM users WHERE id = $1;",
    [1]
  );
  console.log(rows);
 
  // Transaction with rollback on error.
  await client.query("BEGIN");
  try {
    await client.query(
      "INSERT INTO users (id, name) VALUES ($1, $2);",
      [3, "hedy"]
    );
    await client.query("COMMIT");
  } catch (err) {
    await client.query("ROLLBACK");
    throw err;
  }
} finally {
  await client.end();
}

Notes and gotchas#

  • BIGINT arrives as strings by default ({ id: '1', name: 'ada' }). This is standard node-postgres behavior, not a PLOMID quirk — convert with Number() / BigInt() or configure a type parser.
  • Vector parameters are text: ["[1.0, 0.0, 0.0]", 5] for ORDER BY embedding <-> $1 LIMIT $2.
  • TypeScript: type the rows at the boundary (rows as Array<{ id: string; name: string }>), then map to your domain types.

Pooling#

javascriptsource
import { Pool } from "pg";
 
const pool = new Pool({
  host: process.env.PLOMID_HOST ?? "127.0.0.1",
  port: Number(process.env.PLOMID_PORT ?? 5432),
  database: process.env.PLOMID_DB ?? "plomid",
  user: process.env.PLOMID_USER,
  password: process.env.PLOMID_PASSWORD,
  max: 10,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 5000,
});
 
try {
  const { rows } = await pool.query("SELECT count(*) FROM users;");
  console.log(rows);
} finally {
  await pool.end();
}

Keep max modest and aligned with server --max-connections; checkouts should be short-lived with statement timeouts behind them — see Pooling and errors.

Next: Pooling and errors · Vector application integration.

Was this page helpful?