@refpool/prisma 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 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,53 @@
1
+ # @refpool/prisma
2
+
3
+ Multi-tenant **`PrismaClient` pooling** built on [`@refpool/core`](../core)'s
4
+ reference-counted, bounded LRU pool.
5
+
6
+ A pool of `PrismaClient` instances keyed by tenant — useful for per-tenant
7
+ database URLs — bounded at `max`, shared between concurrent requests for the same
8
+ tenant, with idle clients `$disconnect`ed automatically.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install @refpool/prisma @prisma/client
14
+ ```
15
+
16
+ `@prisma/client` is an **optional** peer dependency (`>=5.0.0`). The adapter
17
+ types against a minimal `{ $connect, $disconnect }` shape, so it works with any
18
+ generated client without a hard build-time dependency.
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import { createPrismaPool, withResource } from '@refpool/prisma';
24
+ import { PrismaClient } from '@prisma/client';
25
+
26
+ const pool = createPrismaPool({
27
+ max: 20,
28
+ idleTtlMs: 60_000,
29
+ client: (tenantId) =>
30
+ new PrismaClient({
31
+ datasources: { db: { url: `postgres://localhost:5432/tenant_${tenantId}` } },
32
+ }),
33
+ });
34
+ pool.start();
35
+
36
+ const users = await withResource(pool, tenantId, (prisma) => prisma.user.findMany());
37
+
38
+ await pool.stop(); // on shutdown: disconnects every live client
39
+ ```
40
+
41
+ The factory calls your `client(key)` then `$connect`s it; `dispose` `$disconnect`s
42
+ it on eviction/drain. All other [`PoolOptions`](../core#pooloptionst) pass through.
43
+
44
+ ## API
45
+
46
+ - `createPrismaPool<TClient>(options)` → `RefCountedLruPool<TClient>` —
47
+ `options` is `PoolOptions` minus `factory`/`dispose`, plus
48
+ `client: (key) => TClient | Promise<TClient>` (a *not-yet-connected* client).
49
+ - `withResource(pool, key, fn)` — acquire `key`, run `fn(client)`, release in `finally`.
50
+
51
+ ## License
52
+
53
+ MIT © Atul Singh
package/dist/index.cjs ADDED
@@ -0,0 +1,61 @@
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
+ createPrismaPool: () => createPrismaPool,
24
+ withResource: () => withResource
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_core = require("@refpool/core");
28
+ function createPrismaPool(options) {
29
+ const { client, ...rest } = options;
30
+ return new import_core.RefCountedLruPool({
31
+ ...rest,
32
+ factory: async (key) => {
33
+ const instance = await client(key);
34
+ try {
35
+ await instance.$connect();
36
+ } catch (error) {
37
+ await instance.$disconnect().catch(() => {
38
+ });
39
+ throw error;
40
+ }
41
+ return instance;
42
+ },
43
+ dispose: async (instance) => {
44
+ await instance.$disconnect();
45
+ }
46
+ });
47
+ }
48
+ async function withResource(pool, key, fn) {
49
+ const handle = await pool.acquire(key);
50
+ try {
51
+ return await fn(handle.resource);
52
+ } finally {
53
+ handle.release();
54
+ }
55
+ }
56
+ // Annotate the CommonJS export names for ESM import in node:
57
+ 0 && (module.exports = {
58
+ createPrismaPool,
59
+ withResource
60
+ });
61
+ //# 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';\n\n/**\n * Minimal structural shape of a `PrismaClient`. Adapting against this avoids a\n * hard dependency on a generated client at build time.\n */\nexport interface PrismaClientLike {\n $connect(): Promise<void>;\n $disconnect(): Promise<void>;\n}\n\n/** Options for a per-key `PrismaClient` pool. */\nexport type PrismaPoolOptions<TClient extends PrismaClientLike> = Omit<\n PoolOptions<TClient>,\n 'factory' | 'dispose'\n> & {\n /** Construct a (not-yet-connected) PrismaClient for a tenant key. */\n client: (key: string) => TClient | Promise<TClient>;\n};\n\n/**\n * Create a reference-counted, bounded LRU pool of `PrismaClient` instances\n * keyed by tenant. The factory `$connect`s a client; dispose `$disconnect`s it\n * on eviction/drain.\n */\nexport function createPrismaPool<TClient extends PrismaClientLike>(\n options: PrismaPoolOptions<TClient>,\n): RefCountedLruPool<TClient> {\n const { client, ...rest } = options;\n return new RefCountedLruPool<TClient>({\n ...rest,\n factory: async (key) => {\n const instance = await client(key);\n try {\n await instance.$connect();\n } catch (error) {\n await instance.$disconnect().catch(() => {});\n throw error;\n }\n return instance;\n },\n dispose: async (instance) => {\n await instance.$disconnect();\n },\n });\n}\n\n/** Acquire `key`, run `fn`, and release the holder in `finally`. */\nexport async function withResource<TClient extends PrismaClientLike, R>(\n pool: RefCountedLruPool<TClient>,\n key: string,\n fn: (client: TClient) => Promise<R> | R,\n): Promise<R> {\n const handle: AcquireHandle<TClient> = 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;AA0B3B,SAAS,iBACd,SAC4B;AAC5B,QAAM,EAAE,QAAQ,GAAG,KAAK,IAAI;AAC5B,SAAO,IAAI,8BAA2B;AAAA,IACpC,GAAG;AAAA,IACH,SAAS,OAAO,QAAQ;AACtB,YAAM,WAAW,MAAM,OAAO,GAAG;AACjC,UAAI;AACF,cAAM,SAAS,SAAS;AAAA,MAC1B,SAAS,OAAO;AACd,cAAM,SAAS,YAAY,EAAE,MAAM,MAAM;AAAA,QAAC,CAAC;AAC3C,cAAM;AAAA,MACR;AACA,aAAO;AAAA,IACT;AAAA,IACA,SAAS,OAAO,aAAa;AAC3B,YAAM,SAAS,YAAY;AAAA,IAC7B;AAAA,EACF,CAAC;AACH;AAGA,eAAsB,aACpB,MACA,KACA,IACY;AACZ,QAAM,SAAiC,MAAM,KAAK,QAAQ,GAAG;AAC7D,MAAI;AACF,WAAO,MAAM,GAAG,OAAO,QAAQ;AAAA,EACjC,UAAE;AACA,WAAO,QAAQ;AAAA,EACjB;AACF;","names":[]}
@@ -0,0 +1,25 @@
1
+ import { PoolOptions, RefCountedLruPool } from '@refpool/core';
2
+
3
+ /**
4
+ * Minimal structural shape of a `PrismaClient`. Adapting against this avoids a
5
+ * hard dependency on a generated client at build time.
6
+ */
7
+ interface PrismaClientLike {
8
+ $connect(): Promise<void>;
9
+ $disconnect(): Promise<void>;
10
+ }
11
+ /** Options for a per-key `PrismaClient` pool. */
12
+ type PrismaPoolOptions<TClient extends PrismaClientLike> = Omit<PoolOptions<TClient>, 'factory' | 'dispose'> & {
13
+ /** Construct a (not-yet-connected) PrismaClient for a tenant key. */
14
+ client: (key: string) => TClient | Promise<TClient>;
15
+ };
16
+ /**
17
+ * Create a reference-counted, bounded LRU pool of `PrismaClient` instances
18
+ * keyed by tenant. The factory `$connect`s a client; dispose `$disconnect`s it
19
+ * on eviction/drain.
20
+ */
21
+ declare function createPrismaPool<TClient extends PrismaClientLike>(options: PrismaPoolOptions<TClient>): RefCountedLruPool<TClient>;
22
+ /** Acquire `key`, run `fn`, and release the holder in `finally`. */
23
+ declare function withResource<TClient extends PrismaClientLike, R>(pool: RefCountedLruPool<TClient>, key: string, fn: (client: TClient) => Promise<R> | R): Promise<R>;
24
+
25
+ export { type PrismaClientLike, type PrismaPoolOptions, createPrismaPool, withResource };
@@ -0,0 +1,25 @@
1
+ import { PoolOptions, RefCountedLruPool } from '@refpool/core';
2
+
3
+ /**
4
+ * Minimal structural shape of a `PrismaClient`. Adapting against this avoids a
5
+ * hard dependency on a generated client at build time.
6
+ */
7
+ interface PrismaClientLike {
8
+ $connect(): Promise<void>;
9
+ $disconnect(): Promise<void>;
10
+ }
11
+ /** Options for a per-key `PrismaClient` pool. */
12
+ type PrismaPoolOptions<TClient extends PrismaClientLike> = Omit<PoolOptions<TClient>, 'factory' | 'dispose'> & {
13
+ /** Construct a (not-yet-connected) PrismaClient for a tenant key. */
14
+ client: (key: string) => TClient | Promise<TClient>;
15
+ };
16
+ /**
17
+ * Create a reference-counted, bounded LRU pool of `PrismaClient` instances
18
+ * keyed by tenant. The factory `$connect`s a client; dispose `$disconnect`s it
19
+ * on eviction/drain.
20
+ */
21
+ declare function createPrismaPool<TClient extends PrismaClientLike>(options: PrismaPoolOptions<TClient>): RefCountedLruPool<TClient>;
22
+ /** Acquire `key`, run `fn`, and release the holder in `finally`. */
23
+ declare function withResource<TClient extends PrismaClientLike, R>(pool: RefCountedLruPool<TClient>, key: string, fn: (client: TClient) => Promise<R> | R): Promise<R>;
24
+
25
+ export { type PrismaClientLike, type PrismaPoolOptions, createPrismaPool, withResource };
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ // src/index.ts
2
+ import { RefCountedLruPool } from "@refpool/core";
3
+ function createPrismaPool(options) {
4
+ const { client, ...rest } = options;
5
+ return new RefCountedLruPool({
6
+ ...rest,
7
+ factory: async (key) => {
8
+ const instance = await client(key);
9
+ try {
10
+ await instance.$connect();
11
+ } catch (error) {
12
+ await instance.$disconnect().catch(() => {
13
+ });
14
+ throw error;
15
+ }
16
+ return instance;
17
+ },
18
+ dispose: async (instance) => {
19
+ await instance.$disconnect();
20
+ }
21
+ });
22
+ }
23
+ async function withResource(pool, key, fn) {
24
+ const handle = await pool.acquire(key);
25
+ try {
26
+ return await fn(handle.resource);
27
+ } finally {
28
+ handle.release();
29
+ }
30
+ }
31
+ export {
32
+ createPrismaPool,
33
+ withResource
34
+ };
35
+ //# 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';\n\n/**\n * Minimal structural shape of a `PrismaClient`. Adapting against this avoids a\n * hard dependency on a generated client at build time.\n */\nexport interface PrismaClientLike {\n $connect(): Promise<void>;\n $disconnect(): Promise<void>;\n}\n\n/** Options for a per-key `PrismaClient` pool. */\nexport type PrismaPoolOptions<TClient extends PrismaClientLike> = Omit<\n PoolOptions<TClient>,\n 'factory' | 'dispose'\n> & {\n /** Construct a (not-yet-connected) PrismaClient for a tenant key. */\n client: (key: string) => TClient | Promise<TClient>;\n};\n\n/**\n * Create a reference-counted, bounded LRU pool of `PrismaClient` instances\n * keyed by tenant. The factory `$connect`s a client; dispose `$disconnect`s it\n * on eviction/drain.\n */\nexport function createPrismaPool<TClient extends PrismaClientLike>(\n options: PrismaPoolOptions<TClient>,\n): RefCountedLruPool<TClient> {\n const { client, ...rest } = options;\n return new RefCountedLruPool<TClient>({\n ...rest,\n factory: async (key) => {\n const instance = await client(key);\n try {\n await instance.$connect();\n } catch (error) {\n await instance.$disconnect().catch(() => {});\n throw error;\n }\n return instance;\n },\n dispose: async (instance) => {\n await instance.$disconnect();\n },\n });\n}\n\n/** Acquire `key`, run `fn`, and release the holder in `finally`. */\nexport async function withResource<TClient extends PrismaClientLike, R>(\n pool: RefCountedLruPool<TClient>,\n key: string,\n fn: (client: TClient) => Promise<R> | R,\n): Promise<R> {\n const handle: AcquireHandle<TClient> = await pool.acquire(key);\n try {\n return await fn(handle.resource);\n } finally {\n handle.release();\n }\n}\n"],"mappings":";AAAA,SAAS,yBAAyB;AA0B3B,SAAS,iBACd,SAC4B;AAC5B,QAAM,EAAE,QAAQ,GAAG,KAAK,IAAI;AAC5B,SAAO,IAAI,kBAA2B;AAAA,IACpC,GAAG;AAAA,IACH,SAAS,OAAO,QAAQ;AACtB,YAAM,WAAW,MAAM,OAAO,GAAG;AACjC,UAAI;AACF,cAAM,SAAS,SAAS;AAAA,MAC1B,SAAS,OAAO;AACd,cAAM,SAAS,YAAY,EAAE,MAAM,MAAM;AAAA,QAAC,CAAC;AAC3C,cAAM;AAAA,MACR;AACA,aAAO;AAAA,IACT;AAAA,IACA,SAAS,OAAO,aAAa;AAC3B,YAAM,SAAS,YAAY;AAAA,IAC7B;AAAA,EACF,CAAC;AACH;AAGA,eAAsB,aACpB,MACA,KACA,IACY;AACZ,QAAM,SAAiC,MAAM,KAAK,QAAQ,GAAG;AAC7D,MAAI;AACF,WAAO,MAAM,GAAG,OAAO,QAAQ;AAAA,EACjC,UAAE;AACA,WAAO,QAAQ;AAAA,EACjB;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@refpool/prisma",
3
+ "version": "0.1.0",
4
+ "description": "Multi-tenant PrismaClient 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
+ "prisma",
11
+ "prisma-client",
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/prisma"
21
+ },
22
+ "homepage": "https://github.com/expedite-atul/refpool/tree/main/packages/prisma#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
+ "@prisma/client": ">=5.0.0"
49
+ },
50
+ "peerDependenciesMeta": {
51
+ "@prisma/client": {
52
+ "optional": true
53
+ }
54
+ },
55
+ "devDependencies": {
56
+ "@types/node": "^22.10.2",
57
+ "tsup": "^8.3.5",
58
+ "typescript": "^5.6.3",
59
+ "vitest": "^2.1.8"
60
+ },
61
+ "engines": {
62
+ "node": ">=18"
63
+ },
64
+ "scripts": {
65
+ "build": "tsup",
66
+ "typecheck": "tsc --noEmit",
67
+ "test": "vitest run"
68
+ }
69
+ }