Target
A string opens a pool. A pool from open is reused.
postgres.js, node-postgres, and Bun.sql take a string or a pool. PGlite takes a data directory, a pool, or undefined. The import is Drivers. A string means this client opened the pool. An adopted pool stays open. close() is that call.
| Value | Result |
|---|---|
| a string, postgres.js, node-postgres, or Bun.sql | Opens a pool. close() closes it. |
a pool from open | Reuses that pool. close() does not close it. |
{ primary, replicas } | One pool per endpoint. The call returns a promise. See Topology. |
| a string, PGlite | A data directory. connect returns a promise. close() closes it. |
undefined, PGlite | Memory. open uses memory://. close() closes it. |
string
A string is passed to open as url. The client owns the pool. close() closes it. node-postgres and Bun.sql do the same. How many connections is max.
import { connect } from "okmodel/pg/postgresjs";
import { schema, table, t } from "okmodel/pg";
const notes = table("notes", {
id: t.identity(),
title: t.text(),
});
const app = schema({ tables: [notes] });
export const db = connect(process.env.DATABASE_URL!, { schema: app }); The URL is not filled in. An empty DATABASE_URL is passed through. Check it before you call.
pool
A pool from open is reused. close() does not close it. PGlite open returns a promise, and connect does too. That pool is still reused.
import { connect, open } from "okmodel/pg/postgresjs";
import { schema, table, t } from "okmodel/pg";
const notes = table("notes", {
id: t.identity(),
title: t.text(),
});
const app = schema({ tables: [notes] });
const pool = open({ url: process.env.DATABASE_URL! });
export const db = connect(pool, { schema: app }); topology
{ primary, replicas } returns a promise. The object is Topology.
memory
undefined is memory. PGlite has no URL. dataDir is omitted, and open uses memory://. connect returns a promise. The client opened that database, so close() closes it.
import { connect } from "okmodel/pg/pglite";
import { schema, table, t } from "okmodel/pg";
const notes = table("notes", {
id: t.identity(),
title: t.text(),
});
const app = schema({ tables: [notes] });
export const db = await connect(undefined, { schema: app }); directory
A string is the data directory. connect returns a promise. close() closes it.
import { connect } from "okmodel/pg/pglite";
import { schema, table, t } from "okmodel/pg";
const notes = table("notes", {
id: t.identity(),
title: t.text(),
});
const app = schema({ tables: [notes] });
export const db = await connect("data", { schema: app });