Client Overview
createClient is synchronous — it opens no connection, so no await. Each client is bound to one adapter; models are lower-cased properties (User → db.user). Examples use the example models unless a page declares its own schema.
Creating the Client
import { createClient } from "./generated/client.ts";
const db = createClient({
connection: "mysql://root:test_password@127.0.0.1:3306/app",
});
try {
// Use db to access the database
} finally {
await db.$close();
}2
3
4
5
6
7
8
9
10
11
Options
| Option | Type | Default | Description |
|---|---|---|---|
connection | string | Required | Connection URL; the adapter is chosen from the scheme, see Database Adapters |
logger | Logger | None | Event callback, see Event Logging |
maxAffected | number | None | Row limit for bulk writes, see Write Safety |
The adapter connects on the first database operation, where connection errors surface; $close() on an unused client returns without connecting. Virtual fields and global omit are added with $extend, see Extensions.
Switching Databases
The same models and generated client work with any adapter; switching requires no regeneration:
import { createClient } from "./generated/client.ts";
const db = createClient({ connection: "mysql://root:test_password@127.0.0.1:3306/app" });
// const db = createClient({ connection: "mysql://app:password@localhost:3306/app" });
// const db = createClient({ connection: "postgresql://app:password@localhost:5432/app" });2
3
4
5
Swapping the adapter does not migrate tables or data.
Closing the Client
$close() waits for started operations to finish, then releases connections. Calls after closing raise CLIENT_CLOSED; inside a transaction it raises TRANSACTION_SCOPE. Clients from $extend share shutdown state.
Reading Database Capabilities
$capabilities describes the current adapter and is readable right after createClient:
console.log(db.$capabilities);
// { returning, isolationLevels }2
Per-database values are listed in the capability matrix.
Error Handling
Client validation and mapped database errors use OrmError, identified by code:
import { OrmError } from "@ofis/tsorm";
try {
await db.user.create({ data: { email: "alice@example.com" } });
} catch (error) {
if (error instanceof OrmError && error.code === "UNIQUE_CONSTRAINT") {
// Handle the duplicate email
} else {
throw error;
}
}2
3
4
5
6
7
8
9
10
11
The full list is in Error Codes.
Sensitive data
Error messages never contain SQL parameters, but cause keeps the original driver error, which may contain input values. Filter them before writing to application logs.
Write Safety
Top-level bulk update, delete, and restore methods require a non-empty where; empty conditions raise VALIDATION. Nested writes are scoped by the parent relation, see Relation Writes. Use $clear to empty a table explicitly. maxAffected (positive safe integer, unset by default) caps rows for those bulk operations; exceeding it raises MASS_WRITE_BLOCKED before the write — the count and write are separate statements, so it guards against a wrong filter, not concurrent writers. $clear is exempt, because the call itself is the confirmation.
Execution Model
- One API call does not necessarily map to one SQL statement; relation loading and counts run in batches of at most 200 parent keys, never per parent.
selectandomitprune database columns while keeping relation keys, primary keys, and virtual field dependencies, and only loaded columns are decoded and validated.
Next Steps
- Queries: filters, projections, relation loading, and pagination
- Create, Update, and Delete: the write operations
- Transactions: grouping several operations into one transaction