@refpool/prisma
v0.1.0
Published
Multi-tenant PrismaClient pooling built on @refpool/core's reference-counted, bounded LRU pool.
Maintainers
Readme
@refpool/prisma
Multi-tenant PrismaClient pooling built on @refpool/core's
reference-counted, bounded LRU pool.
A pool of PrismaClient instances keyed by tenant — useful for per-tenant
database URLs — bounded at max, shared between concurrent requests for the same
tenant, with idle clients $disconnected automatically.
Install
npm install @refpool/prisma @prisma/client@prisma/client is an optional peer dependency (>=5.0.0). The adapter
types against a minimal { $connect, $disconnect } shape, so it works with any
generated client without a hard build-time dependency.
Usage
import { createPrismaPool, withResource } from '@refpool/prisma';
import { PrismaClient } from '@prisma/client';
const pool = createPrismaPool({
max: 20,
idleTtlMs: 60_000,
client: (tenantId) =>
new PrismaClient({
datasources: { db: { url: `postgres://localhost:5432/tenant_${tenantId}` } },
}),
});
pool.start();
const users = await withResource(pool, tenantId, (prisma) => prisma.user.findMany());
await pool.stop(); // on shutdown: disconnects every live clientThe factory calls your client(key) then $connects it; dispose $disconnects
it on eviction/drain. All other PoolOptions pass through.
API
createPrismaPool<TClient>(options)→RefCountedLruPool<TClient>—optionsisPoolOptionsminusfactory/dispose, plusclient: (key) => TClient | Promise<TClient>(a not-yet-connected client).withResource(pool, key, fn)— acquirekey, runfn(client), release infinally.
License
MIT © Atul Singh
