Overview

connect() opens the client. The first argument is the target. schema is required.

connect()Anatomy
export const db = connect(
url,url
{
schema,required
},
);

Target

A connection string, a pool from open, or { primary, replicas }. A string or a pool returns the client. A topology returns a promise.

Schema

The schema types the client. The other decisions are the pages below. close() is on this page.

Import connect from the driver you installed. The process keeps one client.

The schema

The client is typed by the schema you pass. There is no global register.

The declaration

The list is how often you reach for each part while opening a client. The target and the schema come first.

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 }); 
OrderPageYou use it for
1DriversThe import.
2TargetA URL, a pool, or memory.
3TopologyA primary and its replicas.
4CheckThe server, the floor, and the catalog hash.
5SessionPool size, TLS, search path, and deadlines.
6ErrorsHTTP statuses and row values.
7Generatorsuuidv4, uuidv7, and okid.
8ObserversNotices, transactions, and errors.
9OpenTelemetrySpans for those observers.

Close

A script calls close() when it is done. A server keeps the client for the life of the process. The call ends the pool only when this client opened it. Who opened it is Target.

Clientclose()
Opened the poolEnds that pool.
Adopted a poolResolves. The pool stays open.
using()Absent on that client.

await using is that call at the end of the block. It calls the same close(). The client has [Symbol.asyncDispose] when the runtime defines that symbol. A runtime without the symbol leaves the client unchanged.

On Bun the adapter keeps idleTimeout at 0, which is Bun's default, so a finished script exits without close().

script.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 async function read() {
  await using db = connect(process.env.DATABASE_URL!, { schema: app }); 
  await db.notes.find({ limit: 1 });
}

A second close() waits on the same close when this client opened the pool. The first call starts the close. A later call returns that promise. for() and unscoped() reuse it. An adopted pool resolves without closing it, on every call.

using() returns a client with no close. The root close() stops the probes and closes every pool that connect opened.

close() before the check settles, on a client that opened the pool, does not reject connected when the check succeeds. When the check fails, the rejection is The client was closed before it connected. Await connected before close(), or do not close it before use. That promise is Check.