@refpool/drizzle
v0.1.0
Published
Multi-tenant Drizzle (node-postgres) database pooling built on @refpool/core's reference-counted, bounded LRU pool.
Downloads
83
Maintainers
Readme
@refpool/drizzle
Multi-tenant Drizzle (node-postgres) database pooling built on
@refpool/core's reference-counted, bounded LRU pool.
Each pooled Drizzle db wraps its own dedicated pg.Pool. You get at most max
live database instances, shared between concurrent requests for the same tenant,
with idle ones reclaimed and their underlying pools .end()ed automatically.
Install
npm install @refpool/drizzle drizzle-orm pgdrizzle-orm and pg are required peer dependencies — this package
statically imports both, so you must install them alongside @refpool/drizzle.
Usage
import { createDrizzlePool, withResource } from '@refpool/drizzle';
import * as schema from './schema';
const pool = createDrizzlePool({
max: 20,
idleTtlMs: 60_000,
schema, // optional — binds the schema to every db instance
config: (tenantId) => ({
connectionString: `postgres://localhost:5432/tenant_${tenantId}`,
}),
});
pool.start();
const users = await withResource(pool, tenantId, (db) =>
db.select().from(schema.users),
);
await pool.stop(); // on shutdown: ends every underlying pg.PoolThe factory builds a pg.Pool from config(key) and wraps it with drizzle()
(binding schema when provided); dispose .end()s the underlying pool on
eviction/drain. All other PoolOptions pass through.
API
createDrizzlePool<TSchema>(options)→RefCountedLruPool<NodePgDatabase<TSchema>>—optionsisPoolOptionsminusfactory/dispose, plusconfig: (key) => PoolConfig | Promise<PoolConfig>and an optionalschema.withResource(pool, key, fn)— acquirekey, runfn(db), release infinally.
License
MIT © Atul Singh
