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.

ValueResult
a string, postgres.js, node-postgres, or Bun.sqlOpens a pool. close() closes it.
a pool from openReuses that pool. close() does not close it.
{ primary, replicas }One pool per endpoint. The call returns a promise. See Topology.
a string, PGliteA data directory. connect returns a promise. close() closes it.
undefined, PGliteMemory. 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.

db.ts
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.

pool.ts
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.

memory.ts
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.

directory.ts
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 });