@refpool/pg 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 +54 -0
- package/dist/index.cjs +52 -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 +26 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -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,54 @@
|
|
|
1
|
+
# @refpool/pg
|
|
2
|
+
|
|
3
|
+
Multi-tenant **node-postgres (`pg`) `Pool` pooling** built on
|
|
4
|
+
[`@refpool/core`](../core)'s reference-counted, bounded LRU pool.
|
|
5
|
+
|
|
6
|
+
A pool of `pg.Pool` instances keyed by tenant: at most `max` live pools, shared
|
|
7
|
+
between concurrent requests for the same tenant, with idle ones reclaimed and
|
|
8
|
+
`.end()`ed automatically — so you never accumulate one live connection pool per
|
|
9
|
+
tenant forever.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @refpool/pg pg
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`pg` (`^8.0.0`) is a **required** peer dependency — this package statically
|
|
18
|
+
imports it, so you must install `pg` alongside `@refpool/pg`.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { createPgPool, withResource } from '@refpool/pg';
|
|
24
|
+
|
|
25
|
+
const pool = createPgPool({
|
|
26
|
+
max: 25,
|
|
27
|
+
idleTtlMs: 60_000,
|
|
28
|
+
config: (tenantId) => ({
|
|
29
|
+
connectionString: `postgres://localhost:5432/tenant_${tenantId}`,
|
|
30
|
+
max: 5, // inner node-postgres pool size, per tenant
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
pool.start();
|
|
34
|
+
|
|
35
|
+
const { rows } = await withResource(pool, tenantId, (pg) =>
|
|
36
|
+
pg.query('select now()'),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
await pool.stop(); // on shutdown: ends every live pg.Pool
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The factory builds a `pg.Pool` from `config(key)`; `dispose` calls `.end()` on
|
|
43
|
+
eviction/drain. All other [`PoolOptions`](../core#pooloptionst) pass through.
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
- `createPgPool(options)` → `RefCountedLruPool<Pool>` —
|
|
48
|
+
`options` is `PoolOptions` minus `factory`/`dispose`, plus
|
|
49
|
+
`config: (key) => PoolConfig | Promise<PoolConfig>`.
|
|
50
|
+
- `withResource(pool, key, fn)` — acquire `key`, run `fn(pgPool)`, release in `finally`.
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT © Atul Singh
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
createPgPool: () => createPgPool,
|
|
24
|
+
withResource: () => withResource
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var import_core = require("@refpool/core");
|
|
28
|
+
var import_pg = require("pg");
|
|
29
|
+
function createPgPool(options) {
|
|
30
|
+
const { config, ...rest } = options;
|
|
31
|
+
return new import_core.RefCountedLruPool({
|
|
32
|
+
...rest,
|
|
33
|
+
factory: async (key) => new import_pg.Pool(await config(key)),
|
|
34
|
+
dispose: async (pool) => {
|
|
35
|
+
await pool.end();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async function withResource(pool, key, fn) {
|
|
40
|
+
const handle = await pool.acquire(key);
|
|
41
|
+
try {
|
|
42
|
+
return await fn(handle.resource);
|
|
43
|
+
} finally {
|
|
44
|
+
handle.release();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
+
0 && (module.exports = {
|
|
49
|
+
createPgPool,
|
|
50
|
+
withResource
|
|
51
|
+
});
|
|
52
|
+
//# 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 { Pool } from 'pg';\nimport type { PoolConfig } from 'pg';\n\n/** Options for a per-key node-postgres `Pool` pool. */\nexport type PgPoolOptions = Omit<PoolOptions<Pool>, 'factory' | 'dispose'> & {\n /** Build the node-postgres `PoolConfig` for a tenant key. */\n config: (key: string) => PoolConfig | Promise<PoolConfig>;\n};\n\n/**\n * Create a reference-counted, bounded LRU pool of node-postgres `Pool`\n * instances keyed by tenant. The factory builds a `Pool`; dispose `.end()`s\n * it on eviction/drain.\n */\nexport function createPgPool(options: PgPoolOptions): RefCountedLruPool<Pool> {\n const { config, ...rest } = options;\n return new RefCountedLruPool<Pool>({\n ...rest,\n factory: async (key) => new Pool(await config(key)),\n dispose: async (pool) => {\n await pool.end();\n },\n });\n}\n\n/** Acquire `key`, run `fn`, and release the holder in `finally`. */\nexport async function withResource<R>(\n pool: RefCountedLruPool<Pool>,\n key: string,\n fn: (pgPool: Pool) => Promise<R> | R,\n): Promise<R> {\n const handle: AcquireHandle<Pool> = 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,gBAAqB;AAcd,SAAS,aAAa,SAAiD;AAC5E,QAAM,EAAE,QAAQ,GAAG,KAAK,IAAI;AAC5B,SAAO,IAAI,8BAAwB;AAAA,IACjC,GAAG;AAAA,IACH,SAAS,OAAO,QAAQ,IAAI,eAAK,MAAM,OAAO,GAAG,CAAC;AAAA,IAClD,SAAS,OAAO,SAAS;AACvB,YAAM,KAAK,IAAI;AAAA,IACjB;AAAA,EACF,CAAC;AACH;AAGA,eAAsB,aACpB,MACA,KACA,IACY;AACZ,QAAM,SAA8B,MAAM,KAAK,QAAQ,GAAG;AAC1D,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 { Pool, PoolConfig } from 'pg';
|
|
3
|
+
|
|
4
|
+
/** Options for a per-key node-postgres `Pool` pool. */
|
|
5
|
+
type PgPoolOptions = Omit<PoolOptions<Pool>, 'factory' | 'dispose'> & {
|
|
6
|
+
/** Build the node-postgres `PoolConfig` for a tenant key. */
|
|
7
|
+
config: (key: string) => PoolConfig | Promise<PoolConfig>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Create a reference-counted, bounded LRU pool of node-postgres `Pool`
|
|
11
|
+
* instances keyed by tenant. The factory builds a `Pool`; dispose `.end()`s
|
|
12
|
+
* it on eviction/drain.
|
|
13
|
+
*/
|
|
14
|
+
declare function createPgPool(options: PgPoolOptions): RefCountedLruPool<Pool>;
|
|
15
|
+
/** Acquire `key`, run `fn`, and release the holder in `finally`. */
|
|
16
|
+
declare function withResource<R>(pool: RefCountedLruPool<Pool>, key: string, fn: (pgPool: Pool) => Promise<R> | R): Promise<R>;
|
|
17
|
+
|
|
18
|
+
export { type PgPoolOptions, createPgPool, withResource };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PoolOptions, RefCountedLruPool } from '@refpool/core';
|
|
2
|
+
import { Pool, PoolConfig } from 'pg';
|
|
3
|
+
|
|
4
|
+
/** Options for a per-key node-postgres `Pool` pool. */
|
|
5
|
+
type PgPoolOptions = Omit<PoolOptions<Pool>, 'factory' | 'dispose'> & {
|
|
6
|
+
/** Build the node-postgres `PoolConfig` for a tenant key. */
|
|
7
|
+
config: (key: string) => PoolConfig | Promise<PoolConfig>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Create a reference-counted, bounded LRU pool of node-postgres `Pool`
|
|
11
|
+
* instances keyed by tenant. The factory builds a `Pool`; dispose `.end()`s
|
|
12
|
+
* it on eviction/drain.
|
|
13
|
+
*/
|
|
14
|
+
declare function createPgPool(options: PgPoolOptions): RefCountedLruPool<Pool>;
|
|
15
|
+
/** Acquire `key`, run `fn`, and release the holder in `finally`. */
|
|
16
|
+
declare function withResource<R>(pool: RefCountedLruPool<Pool>, key: string, fn: (pgPool: Pool) => Promise<R> | R): Promise<R>;
|
|
17
|
+
|
|
18
|
+
export { type PgPoolOptions, createPgPool, withResource };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { RefCountedLruPool } from "@refpool/core";
|
|
3
|
+
import { Pool } from "pg";
|
|
4
|
+
function createPgPool(options) {
|
|
5
|
+
const { config, ...rest } = options;
|
|
6
|
+
return new RefCountedLruPool({
|
|
7
|
+
...rest,
|
|
8
|
+
factory: async (key) => new Pool(await config(key)),
|
|
9
|
+
dispose: async (pool) => {
|
|
10
|
+
await pool.end();
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
async function withResource(pool, key, fn) {
|
|
15
|
+
const handle = await pool.acquire(key);
|
|
16
|
+
try {
|
|
17
|
+
return await fn(handle.resource);
|
|
18
|
+
} finally {
|
|
19
|
+
handle.release();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
createPgPool,
|
|
24
|
+
withResource
|
|
25
|
+
};
|
|
26
|
+
//# 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 { Pool } from 'pg';\nimport type { PoolConfig } from 'pg';\n\n/** Options for a per-key node-postgres `Pool` pool. */\nexport type PgPoolOptions = Omit<PoolOptions<Pool>, 'factory' | 'dispose'> & {\n /** Build the node-postgres `PoolConfig` for a tenant key. */\n config: (key: string) => PoolConfig | Promise<PoolConfig>;\n};\n\n/**\n * Create a reference-counted, bounded LRU pool of node-postgres `Pool`\n * instances keyed by tenant. The factory builds a `Pool`; dispose `.end()`s\n * it on eviction/drain.\n */\nexport function createPgPool(options: PgPoolOptions): RefCountedLruPool<Pool> {\n const { config, ...rest } = options;\n return new RefCountedLruPool<Pool>({\n ...rest,\n factory: async (key) => new Pool(await config(key)),\n dispose: async (pool) => {\n await pool.end();\n },\n });\n}\n\n/** Acquire `key`, run `fn`, and release the holder in `finally`. */\nexport async function withResource<R>(\n pool: RefCountedLruPool<Pool>,\n key: string,\n fn: (pgPool: Pool) => Promise<R> | R,\n): Promise<R> {\n const handle: AcquireHandle<Pool> = 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,YAAY;AAcd,SAAS,aAAa,SAAiD;AAC5E,QAAM,EAAE,QAAQ,GAAG,KAAK,IAAI;AAC5B,SAAO,IAAI,kBAAwB;AAAA,IACjC,GAAG;AAAA,IACH,SAAS,OAAO,QAAQ,IAAI,KAAK,MAAM,OAAO,GAAG,CAAC;AAAA,IAClD,SAAS,OAAO,SAAS;AACvB,YAAM,KAAK,IAAI;AAAA,IACjB;AAAA,EACF,CAAC;AACH;AAGA,eAAsB,aACpB,MACA,KACA,IACY;AACZ,QAAM,SAA8B,MAAM,KAAK,QAAQ,GAAG;AAC1D,MAAI;AACF,WAAO,MAAM,GAAG,OAAO,QAAQ;AAAA,EACjC,UAAE;AACA,WAAO,QAAQ;AAAA,EACjB;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@refpool/pg",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Multi-tenant node-postgres (pg) Pool 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
|
+
"pg",
|
|
11
|
+
"node-postgres",
|
|
12
|
+
"postgres",
|
|
13
|
+
"pool",
|
|
14
|
+
"multi-tenant",
|
|
15
|
+
"connection-pool",
|
|
16
|
+
"refpool"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/expedite-atul/refpool.git",
|
|
21
|
+
"directory": "packages/node-postgres"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/expedite-atul/refpool/tree/main/packages/node-postgres#readme",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/expedite-atul/refpool/issues"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.cjs",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"import": "./dist/index.js",
|
|
37
|
+
"require": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"README.md",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
],
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@refpool/core": "0.1.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"pg": "^8.0.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@testcontainers/postgresql": "^10.16.0",
|
|
53
|
+
"@types/node": "^22.10.2",
|
|
54
|
+
"@types/pg": "^8.11.10",
|
|
55
|
+
"pg": "^8.13.1",
|
|
56
|
+
"tsup": "^8.3.5",
|
|
57
|
+
"typescript": "^5.6.3",
|
|
58
|
+
"vitest": "^2.1.8"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=18"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsup",
|
|
65
|
+
"typecheck": "tsc --noEmit",
|
|
66
|
+
"test": "vitest run"
|
|
67
|
+
}
|
|
68
|
+
}
|