@agentuity/drizzle
Drizzle ORM integration with resilient PostgreSQL connections for Agentuity projects.
Installation
bun add @agentuity/drizzle
Features
- Type-safe queries - Full TypeScript support with Drizzle ORM's schema inference
- Automatic reconnection - Built on @agentuity/postgres with exponential backoff
- Convenient re-exports - Common Drizzle utilities available from a single import
- Auth integration - Works seamlessly with @agentuity/auth via drizzleAdapter
Basic Usage
import { createPostgresDrizzle, pgTable, text, serial, eq } from '@agentuity/drizzle';
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull().unique(),
});
const { db, close } = createPostgresDrizzle({
schema: { users },
});
const allUsers = await db.select().from(users);
const user = await db.select().from(users).where(eq(users.id, 1));
await db.insert(users).values({ name: 'Alice', email: 'alice@example.com' });
await close();
Custom Configuration
import { createPostgresDrizzle } from '@agentuity/drizzle';
import * as schema from './schema';
const { db, client, close } = createPostgresDrizzle({
connectionString: 'postgres://user:pass@localhost:5432/mydb',
schema,
logger: true,
reconnect: {
maxAttempts: 5,
initialDelayMs: 100,
maxDelayMs: 30000,
},
onConnect: () => console.log('Connected to database'),
onReconnected: () => console.log('Reconnected to database'),
});
console.log(client.stats);
Using with @agentuity/auth
import { createPostgresDrizzle, drizzleAdapter } from '@agentuity/drizzle';
import { createAuth } from '@agentuity/auth';
import * as schema from './schema';
const { db, close } = createPostgresDrizzle({ schema });
const auth = createAuth({
database: drizzleAdapter(db, {
provider: 'pg',
}),
});
Accessing the Underlying Client
The client property gives you access to the @agentuity/postgres client for raw queries:
const { db, client, close } = createPostgresDrizzle({ schema });
const users = await db.select().from(schema.users);
const result = await client`SELECT NOW()`;
if (client.connected) {
console.log('Database is connected');
}
console.log(client.stats);
Available Re-exports
Query Operators (from drizzle-orm)
import {
sql,
eq,
and,
or,
not,
desc,
asc,
gt,
gte,
lt,
lte,
ne,
isNull,
isNotNull,
inArray,
notInArray,
between,
like,
ilike,
} from '@agentuity/drizzle';
Schema Definitions (from drizzle-orm/pg-core)
import {
pgTable,
pgSchema,
pgEnum,
text,
varchar,
char,
integer,
smallint,
bigint,
serial,
smallserial,
bigserial,
boolean,
timestamp,
date,
time,
interval,
json,
jsonb,
uuid,
numeric,
real,
doublePrecision,
inet,
cidr,
macaddr,
macaddr8,
primaryKey,
foreignKey,
unique,
uniqueIndex,
index,
check,
} from '@agentuity/drizzle';
Postgres Client (from @agentuity/postgres)
import {
postgres,
PostgresClient,
type CallablePostgresClient,
type PostgresConfig,
type ReconnectConfig,
type ConnectionStats,
} from '@agentuity/drizzle';
Auth Adapter (from better-auth)
import { drizzleAdapter } from '@agentuity/drizzle';
API Reference
createPostgresDrizzle(config?)
Creates a Drizzle ORM instance with a resilient PostgreSQL connection.
Parameters
config.connectionString | string | PostgreSQL connection URL. Defaults to DATABASE_URL |
config.connection | PostgresConfig | Full connection configuration object |
config.schema | TSchema | Drizzle schema for type-safe queries |
config.logger | boolean | DrizzleLogger | Enable query logging |
config.reconnect | ReconnectConfig | Reconnection behavior configuration |
config.onConnect | () => void | Called when initially connected |
config.onReconnected | () => void | Called after successful reconnection |
Returns
db | BunSQLDatabase<TSchema> | The Drizzle database instance |
client | CallablePostgresClient | The underlying postgres client |
close | () => Promise<void> | Cleanup function |
License
Apache-2.0