@refpool/typeorm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +57 -0
- package/dist/index.cjs +62 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Atul Singh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @refpool/typeorm
|
|
2
|
+
|
|
3
|
+
Multi-tenant **TypeORM `DataSource` pooling** built on
|
|
4
|
+
[`@refpool/core`](../core)'s reference-counted, bounded LRU pool.
|
|
5
|
+
|
|
6
|
+
Instead of holding one initialized `DataSource` per tenant forever, you get a
|
|
7
|
+
bounded pool: at most `max` live data sources, shared between concurrent requests
|
|
8
|
+
for the same tenant, with the coldest idle ones reclaimed automatically.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @refpool/typeorm typeorm
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`typeorm` (`^0.3.0`) is a **required** peer dependency — this package statically
|
|
17
|
+
imports it, so you must install `typeorm` alongside `@refpool/typeorm`.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createTypeOrmPool, withResource } from '@refpool/typeorm';
|
|
23
|
+
|
|
24
|
+
const pool = createTypeOrmPool({
|
|
25
|
+
max: 20,
|
|
26
|
+
idleTtlMs: 60_000,
|
|
27
|
+
config: (tenantId) => ({
|
|
28
|
+
type: 'postgres',
|
|
29
|
+
url: `postgres://localhost:5432/tenant_${tenantId}`,
|
|
30
|
+
entities: [/* ... */],
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
pool.start();
|
|
34
|
+
|
|
35
|
+
// Acquire, run, release — release is guaranteed even if `fn` throws.
|
|
36
|
+
const users = await withResource(pool, tenantId, async (dataSource) => {
|
|
37
|
+
return dataSource.getRepository(User).find();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await pool.stop(); // on shutdown: destroys every live DataSource
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The factory builds a `DataSource` from `config(key)` and calls `.initialize()`;
|
|
44
|
+
`dispose` calls `.destroy()` on eviction/drain. All other
|
|
45
|
+
[`PoolOptions`](../core#pooloptionst) (`max`, `idleTtlMs`, `breaker`, `prewarm`,
|
|
46
|
+
`statsIntervalMs`, …) pass straight through.
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
- `createTypeOrmPool(options)` → `RefCountedLruPool<DataSource>` —
|
|
51
|
+
`options` is `PoolOptions` minus `factory`/`dispose`, plus
|
|
52
|
+
`config: (key) => DataSourceOptions | Promise<DataSourceOptions>`.
|
|
53
|
+
- `withResource(pool, key, fn)` — acquire `key`, run `fn(dataSource)`, release in `finally`.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT © Atul Singh
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createTypeOrmPool: () => createTypeOrmPool,
|
|
24
|
+
withResource: () => withResource
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var import_core = require("@refpool/core");
|
|
28
|
+
var import_typeorm = require("typeorm");
|
|
29
|
+
function createTypeOrmPool(options) {
|
|
30
|
+
const { config, ...rest } = options;
|
|
31
|
+
return new import_core.RefCountedLruPool({
|
|
32
|
+
...rest,
|
|
33
|
+
factory: async (key) => {
|
|
34
|
+
const dataSource = new import_typeorm.DataSource(await config(key));
|
|
35
|
+
try {
|
|
36
|
+
await dataSource.initialize();
|
|
37
|
+
} catch (error) {
|
|
38
|
+
await dataSource.destroy().catch(() => {
|
|
39
|
+
});
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
return dataSource;
|
|
43
|
+
},
|
|
44
|
+
dispose: async (dataSource) => {
|
|
45
|
+
await dataSource.destroy();
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async function withResource(pool, key, fn) {
|
|
50
|
+
const handle = await pool.acquire(key);
|
|
51
|
+
try {
|
|
52
|
+
return await fn(handle.resource);
|
|
53
|
+
} finally {
|
|
54
|
+
handle.release();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
58
|
+
0 && (module.exports = {
|
|
59
|
+
createTypeOrmPool,
|
|
60
|
+
withResource
|
|
61
|
+
});
|
|
62
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { RefCountedLruPool } from '@refpool/core';\nimport type { AcquireHandle, PoolOptions } from '@refpool/core';\nimport { DataSource } from 'typeorm';\nimport type { DataSourceOptions } from 'typeorm';\n\n/** Options for a per-key TypeORM `DataSource` pool. */\nexport type TypeOrmPoolOptions = Omit<PoolOptions<DataSource>, 'factory' | 'dispose'> & {\n /** Build the `DataSource` options for a tenant key. */\n config: (key: string) => DataSourceOptions | Promise<DataSourceOptions>;\n};\n\n/**\n * Create a reference-counted, bounded LRU pool of TypeORM `DataSource`\n * instances keyed by tenant. The factory builds and `.initialize()`s a\n * `DataSource`; dispose `.destroy()`s it on eviction/drain.\n */\nexport function createTypeOrmPool(options: TypeOrmPoolOptions): RefCountedLruPool<DataSource> {\n const { config, ...rest } = options;\n return new RefCountedLruPool<DataSource>({\n ...rest,\n factory: async (key) => {\n const dataSource = new DataSource(await config(key));\n try {\n await dataSource.initialize();\n } catch (error) {\n await dataSource.destroy().catch(() => {});\n throw error;\n }\n return dataSource;\n },\n dispose: async (dataSource) => {\n await dataSource.destroy();\n },\n });\n}\n\n/** Acquire `key`, run `fn`, and release the holder in `finally`. */\nexport async function withResource<R>(\n pool: RefCountedLruPool<DataSource>,\n key: string,\n fn: (dataSource: DataSource) => Promise<R> | R,\n): Promise<R> {\n const handle: AcquireHandle<DataSource> = await pool.acquire(key);\n try {\n return await fn(handle.resource);\n } finally {\n handle.release();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAkC;AAElC,qBAA2B;AAcpB,SAAS,kBAAkB,SAA4D;AAC5F,QAAM,EAAE,QAAQ,GAAG,KAAK,IAAI;AAC5B,SAAO,IAAI,8BAA8B;AAAA,IACvC,GAAG;AAAA,IACH,SAAS,OAAO,QAAQ;AACtB,YAAM,aAAa,IAAI,0BAAW,MAAM,OAAO,GAAG,CAAC;AACnD,UAAI;AACF,cAAM,WAAW,WAAW;AAAA,MAC9B,SAAS,OAAO;AACd,cAAM,WAAW,QAAQ,EAAE,MAAM,MAAM;AAAA,QAAC,CAAC;AACzC,cAAM;AAAA,MACR;AACA,aAAO;AAAA,IACT;AAAA,IACA,SAAS,OAAO,eAAe;AAC7B,YAAM,WAAW,QAAQ;AAAA,IAC3B;AAAA,EACF,CAAC;AACH;AAGA,eAAsB,aACpB,MACA,KACA,IACY;AACZ,QAAM,SAAoC,MAAM,KAAK,QAAQ,GAAG;AAChE,MAAI;AACF,WAAO,MAAM,GAAG,OAAO,QAAQ;AAAA,EACjC,UAAE;AACA,WAAO,QAAQ;AAAA,EACjB;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PoolOptions, RefCountedLruPool } from '@refpool/core';
|
|
2
|
+
import { DataSource, DataSourceOptions } from 'typeorm';
|
|
3
|
+
|
|
4
|
+
/** Options for a per-key TypeORM `DataSource` pool. */
|
|
5
|
+
type TypeOrmPoolOptions = Omit<PoolOptions<DataSource>, 'factory' | 'dispose'> & {
|
|
6
|
+
/** Build the `DataSource` options for a tenant key. */
|
|
7
|
+
config: (key: string) => DataSourceOptions | Promise<DataSourceOptions>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Create a reference-counted, bounded LRU pool of TypeORM `DataSource`
|
|
11
|
+
* instances keyed by tenant. The factory builds and `.initialize()`s a
|
|
12
|
+
* `DataSource`; dispose `.destroy()`s it on eviction/drain.
|
|
13
|
+
*/
|
|
14
|
+
declare function createTypeOrmPool(options: TypeOrmPoolOptions): RefCountedLruPool<DataSource>;
|
|
15
|
+
/** Acquire `key`, run `fn`, and release the holder in `finally`. */
|
|
16
|
+
declare function withResource<R>(pool: RefCountedLruPool<DataSource>, key: string, fn: (dataSource: DataSource) => Promise<R> | R): Promise<R>;
|
|
17
|
+
|
|
18
|
+
export { type TypeOrmPoolOptions, createTypeOrmPool, withResource };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PoolOptions, RefCountedLruPool } from '@refpool/core';
|
|
2
|
+
import { DataSource, DataSourceOptions } from 'typeorm';
|
|
3
|
+
|
|
4
|
+
/** Options for a per-key TypeORM `DataSource` pool. */
|
|
5
|
+
type TypeOrmPoolOptions = Omit<PoolOptions<DataSource>, 'factory' | 'dispose'> & {
|
|
6
|
+
/** Build the `DataSource` options for a tenant key. */
|
|
7
|
+
config: (key: string) => DataSourceOptions | Promise<DataSourceOptions>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Create a reference-counted, bounded LRU pool of TypeORM `DataSource`
|
|
11
|
+
* instances keyed by tenant. The factory builds and `.initialize()`s a
|
|
12
|
+
* `DataSource`; dispose `.destroy()`s it on eviction/drain.
|
|
13
|
+
*/
|
|
14
|
+
declare function createTypeOrmPool(options: TypeOrmPoolOptions): RefCountedLruPool<DataSource>;
|
|
15
|
+
/** Acquire `key`, run `fn`, and release the holder in `finally`. */
|
|
16
|
+
declare function withResource<R>(pool: RefCountedLruPool<DataSource>, key: string, fn: (dataSource: DataSource) => Promise<R> | R): Promise<R>;
|
|
17
|
+
|
|
18
|
+
export { type TypeOrmPoolOptions, createTypeOrmPool, withResource };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { RefCountedLruPool } from "@refpool/core";
|
|
3
|
+
import { DataSource } from "typeorm";
|
|
4
|
+
function createTypeOrmPool(options) {
|
|
5
|
+
const { config, ...rest } = options;
|
|
6
|
+
return new RefCountedLruPool({
|
|
7
|
+
...rest,
|
|
8
|
+
factory: async (key) => {
|
|
9
|
+
const dataSource = new DataSource(await config(key));
|
|
10
|
+
try {
|
|
11
|
+
await dataSource.initialize();
|
|
12
|
+
} catch (error) {
|
|
13
|
+
await dataSource.destroy().catch(() => {
|
|
14
|
+
});
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
return dataSource;
|
|
18
|
+
},
|
|
19
|
+
dispose: async (dataSource) => {
|
|
20
|
+
await dataSource.destroy();
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async function withResource(pool, key, fn) {
|
|
25
|
+
const handle = await pool.acquire(key);
|
|
26
|
+
try {
|
|
27
|
+
return await fn(handle.resource);
|
|
28
|
+
} finally {
|
|
29
|
+
handle.release();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
createTypeOrmPool,
|
|
34
|
+
withResource
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { RefCountedLruPool } from '@refpool/core';\nimport type { AcquireHandle, PoolOptions } from '@refpool/core';\nimport { DataSource } from 'typeorm';\nimport type { DataSourceOptions } from 'typeorm';\n\n/** Options for a per-key TypeORM `DataSource` pool. */\nexport type TypeOrmPoolOptions = Omit<PoolOptions<DataSource>, 'factory' | 'dispose'> & {\n /** Build the `DataSource` options for a tenant key. */\n config: (key: string) => DataSourceOptions | Promise<DataSourceOptions>;\n};\n\n/**\n * Create a reference-counted, bounded LRU pool of TypeORM `DataSource`\n * instances keyed by tenant. The factory builds and `.initialize()`s a\n * `DataSource`; dispose `.destroy()`s it on eviction/drain.\n */\nexport function createTypeOrmPool(options: TypeOrmPoolOptions): RefCountedLruPool<DataSource> {\n const { config, ...rest } = options;\n return new RefCountedLruPool<DataSource>({\n ...rest,\n factory: async (key) => {\n const dataSource = new DataSource(await config(key));\n try {\n await dataSource.initialize();\n } catch (error) {\n await dataSource.destroy().catch(() => {});\n throw error;\n }\n return dataSource;\n },\n dispose: async (dataSource) => {\n await dataSource.destroy();\n },\n });\n}\n\n/** Acquire `key`, run `fn`, and release the holder in `finally`. */\nexport async function withResource<R>(\n pool: RefCountedLruPool<DataSource>,\n key: string,\n fn: (dataSource: DataSource) => Promise<R> | R,\n): Promise<R> {\n const handle: AcquireHandle<DataSource> = await pool.acquire(key);\n try {\n return await fn(handle.resource);\n } finally {\n handle.release();\n }\n}\n"],"mappings":";AAAA,SAAS,yBAAyB;AAElC,SAAS,kBAAkB;AAcpB,SAAS,kBAAkB,SAA4D;AAC5F,QAAM,EAAE,QAAQ,GAAG,KAAK,IAAI;AAC5B,SAAO,IAAI,kBAA8B;AAAA,IACvC,GAAG;AAAA,IACH,SAAS,OAAO,QAAQ;AACtB,YAAM,aAAa,IAAI,WAAW,MAAM,OAAO,GAAG,CAAC;AACnD,UAAI;AACF,cAAM,WAAW,WAAW;AAAA,MAC9B,SAAS,OAAO;AACd,cAAM,WAAW,QAAQ,EAAE,MAAM,MAAM;AAAA,QAAC,CAAC;AACzC,cAAM;AAAA,MACR;AACA,aAAO;AAAA,IACT;AAAA,IACA,SAAS,OAAO,eAAe;AAC7B,YAAM,WAAW,QAAQ;AAAA,IAC3B;AAAA,EACF,CAAC;AACH;AAGA,eAAsB,aACpB,MACA,KACA,IACY;AACZ,QAAM,SAAoC,MAAM,KAAK,QAAQ,GAAG;AAChE,MAAI;AACF,WAAO,MAAM,GAAG,OAAO,QAAQ;AAAA,EACjC,UAAE;AACA,WAAO,QAAQ;AAAA,EACjB;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@refpool/typeorm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Multi-tenant TypeORM DataSource pooling built on @refpool/core's reference-counted, bounded LRU pool.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Atul Singh <atulsingh.harsh@gmail.com> (https://atulsingh.io)",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"keywords": [
|
|
10
|
+
"typeorm",
|
|
11
|
+
"datasource",
|
|
12
|
+
"pool",
|
|
13
|
+
"multi-tenant",
|
|
14
|
+
"connection-pool",
|
|
15
|
+
"refpool"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/expedite-atul/refpool.git",
|
|
20
|
+
"directory": "packages/typeorm"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/expedite-atul/refpool/tree/main/packages/typeorm#readme",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/expedite-atul/refpool/issues"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"require": "./dist/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"README.md",
|
|
42
|
+
"LICENSE"
|
|
43
|
+
],
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@refpool/core": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"typeorm": "^0.3.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^22.10.2",
|
|
52
|
+
"tsup": "^8.3.5",
|
|
53
|
+
"typeorm": "^0.3.20",
|
|
54
|
+
"typescript": "^5.6.3",
|
|
55
|
+
"vitest": "^2.1.8"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=18"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsup",
|
|
62
|
+
"typecheck": "tsc --noEmit",
|
|
63
|
+
"test": "vitest run"
|
|
64
|
+
}
|
|
65
|
+
}
|