@refpool/typeorm
v0.1.0
Published
Multi-tenant TypeORM DataSource pooling built on @refpool/core's reference-counted, bounded LRU pool.
Downloads
77
Maintainers
Readme
@refpool/typeorm
Multi-tenant TypeORM DataSource pooling built on
@refpool/core's reference-counted, bounded LRU pool.
Instead of holding one initialized DataSource per tenant forever, you get a
bounded pool: at most max live data sources, shared between concurrent requests
for the same tenant, with the coldest idle ones reclaimed automatically.
Install
npm install @refpool/typeorm typeormtypeorm (^0.3.0) is a required peer dependency — this package statically
imports it, so you must install typeorm alongside @refpool/typeorm.
Usage
import { createTypeOrmPool, withResource } from '@refpool/typeorm';
const pool = createTypeOrmPool({
max: 20,
idleTtlMs: 60_000,
config: (tenantId) => ({
type: 'postgres',
url: `postgres://localhost:5432/tenant_${tenantId}`,
entities: [/* ... */],
}),
});
pool.start();
// Acquire, run, release — release is guaranteed even if `fn` throws.
const users = await withResource(pool, tenantId, async (dataSource) => {
return dataSource.getRepository(User).find();
});
await pool.stop(); // on shutdown: destroys every live DataSourceThe factory builds a DataSource from config(key) and calls .initialize();
dispose calls .destroy() on eviction/drain. All other
PoolOptions (max, idleTtlMs, breaker, prewarm,
statsIntervalMs, …) pass straight through.
API
createTypeOrmPool(options)→RefCountedLruPool<DataSource>—optionsisPoolOptionsminusfactory/dispose, plusconfig: (key) => DataSourceOptions | Promise<DataSourceOptions>.withResource(pool, key, fn)— acquirekey, runfn(dataSource), release infinally.
License
MIT © Atul Singh
