Client
better() — options, client-level methods, and the values exported from better-drizzle.
import { better } from 'better-drizzle';
const client = better(db, options);better(db, options) wraps a Drizzle database instance and returns a fully typed client.
Options
Prop
Type
raw options
Prop
Type
Client methods
Beyond the per-table delegates, the client exposes:
| Method | Description |
|---|---|
client.<table> | the model delegate for each table |
repository(name) | resolve a delegate by schema key or DB table name |
transaction(fn, options?) | run fn in a transaction |
$raw(...) | run a safe raw read, returns rows |
$executeRaw(...) | run a safe raw write, returns { rowsAffected } |
$rawUnsafe(...) | run a raw string query (requires raw.allowUnsafe) |
$withContext(meta) | clone the client with default metadata merged into every operation |
afterCommit(cb) | register a callback for the current transaction scope |
afterRollback(cb) | register a callback for the current transaction scope |
repository(name) accepts either the TypeScript schema key or the underlying database table name:
const usersByKey = client.repository('users');
const usersByDbName = client.repository('app_users');afterCommit() and afterRollback() are useful when you're already holding a transaction-bound client deeper in your call stack and don't want to thread the callback registration through another abstraction.
Exports
import {
better,
definePlugin,
PaginationType,
OrderType,
version,
// error helpers
isDatabaseError,
getDatabaseErrorInfo,
isUniqueViolation,
isForeignKeyViolation,
isNotNullViolation,
isCheckViolation,
// transaction error
BetterDrizzleTransactionRollbackError,
} from 'better-drizzle';Enums
enum PaginationType {
Cursor = 1,
Offset,
}
enum OrderType {
Asc = 'asc',
Desc = 'desc',
}The library also exports its full type surface (BetterDrizzleClient, BetterDrizzleModelDelegate, QueryArgs, WhereInput, Plugin, hook context types, and more) for typing your own helpers.
Each package also exports its own version constant:
import { version } from 'better-drizzle';
import { version as softDeleteVersion } from '@better-drizzle/soft-delete';