joist-knex 2.0.3 → 2.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/build/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Entity, FilterAndSettings, MaybeAbstractEntityConstructor } from "joist-core";
2
2
  import { Knex } from "knex";
3
+ import pg from "pg";
3
4
  /**
4
5
  * Builds the Knex queries from `em.find`-style parameters.
5
6
  *
@@ -26,4 +27,19 @@ export declare function buildQuery<T extends Entity>(knex: Knex, type: MaybeAbst
26
27
  pruneJoins?: boolean;
27
28
  keepAliases?: string[];
28
29
  }): Knex.QueryBuilder<{}, unknown[]>;
30
+ /**
31
+ * Creates a Knex instance that delegates to an existing pg Pool or PoolClient
32
+ * instead of managing its own internal connection pool.
33
+ *
34
+ * When given a `pg.Pool`, each knex query acquires a connection from the pool and
35
+ * releases it when the query completes.
36
+ *
37
+ * When given a `pg.PoolClient`, all knex queries run on that specific client. This
38
+ * is useful when the client has an open transaction (via `BEGIN`) — the knex queries
39
+ * will participate in that transaction and be affected by `COMMIT`/`ROLLBACK`.
40
+ *
41
+ * The returned Knex instance has no internal pool, so there is no need to call
42
+ * `.destroy()` on it — the caller owns the Pool/PoolClient lifecycle.
43
+ */
44
+ export declare function createKnex(poolOrClient: pg.Pool | pg.PoolClient): Knex;
29
45
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAe,8BAA8B,EAAkB,MAAM,YAAY,CAAC;AACpH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAG5B;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EACzC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,8BAA8B,CAAC,CAAC,CAAC,EACvC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG;IAC7B,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB,GACA,IAAI,CAAC,YAAY,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,CAclC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAe,8BAA8B,EAAkB,MAAM,YAAY,CAAC;AACpH,OAAO,EAA0B,IAAI,EAAE,MAAM,MAAM,CAAC;AACpD,OAAO,EAAE,MAAM,IAAI,CAAC;AAGpB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EACzC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,8BAA8B,CAAC,CAAC,CAAC,EACvC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG;IAC7B,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB,GACA,IAAI,CAAC,YAAY,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,CAclC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,UAAU,CAAC,YAAY,EAAE,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,UAAU,GAAG,IAAI,CAUtE"}
package/build/index.js CHANGED
@@ -1,7 +1,13 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.buildQuery = buildQuery;
7
+ exports.createKnex = createKnex;
4
8
  const joist_core_1 = require("joist-core");
9
+ const knex_1 = require("knex");
10
+ const pg_1 = __importDefault(require("pg"));
5
11
  const buildKnexQuery_1 = require("./buildKnexQuery");
6
12
  /**
7
13
  * Builds the Knex queries from `em.find`-style parameters.
@@ -31,4 +37,30 @@ function buildQuery(knex, type, filter) {
31
37
  const parsed = (0, joist_core_1.parseFindQuery)(meta, where, { conditions, orderBy, pruneJoins, keepAliases, softDeletes });
32
38
  return (0, buildKnexQuery_1.buildKnexQuery)(knex, parsed, { limit, offset });
33
39
  }
40
+ /**
41
+ * Creates a Knex instance that delegates to an existing pg Pool or PoolClient
42
+ * instead of managing its own internal connection pool.
43
+ *
44
+ * When given a `pg.Pool`, each knex query acquires a connection from the pool and
45
+ * releases it when the query completes.
46
+ *
47
+ * When given a `pg.PoolClient`, all knex queries run on that specific client. This
48
+ * is useful when the client has an open transaction (via `BEGIN`) — the knex queries
49
+ * will participate in that transaction and be affected by `COMMIT`/`ROLLBACK`.
50
+ *
51
+ * The returned Knex instance has no internal pool, so there is no need to call
52
+ * `.destroy()` on it — the caller owns the Pool/PoolClient lifecycle.
53
+ */
54
+ function createKnex(poolOrClient) {
55
+ const knex = (0, knex_1.knex)({ client: "pg", connection: {}, pool: { max: 0 } });
56
+ if (poolOrClient instanceof pg_1.default.Pool) {
57
+ knex.client.acquireConnection = () => poolOrClient.connect();
58
+ knex.client.releaseConnection = (conn) => conn.release();
59
+ }
60
+ else {
61
+ knex.client.acquireConnection = () => poolOrClient;
62
+ knex.client.releaseConnection = () => { };
63
+ }
64
+ return knex;
65
+ }
34
66
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AA0BA,gCAqBC;AA/CD,2CAAoH;AAEpH,qDAAkD;AAElD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,UAAU,CACxB,IAAU,EACV,IAAuC,EACvC,MAGC;IAED,MAAM,IAAI,GAAG,IAAA,wBAAW,EAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,EACJ,KAAK,EACL,UAAU,EACV,OAAO,EACP,KAAK,EACL,MAAM,EACN,UAAU,G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EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAChF,IAAI,YAAY,YAAY,YAAE,CAAC,IAAI,EAAE,CAAC;QACpC,IAAI,CAAC,MAAM,CAAC,iBAAiB,GAAG,GAAG,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC;QAC7D,IAAI,CAAC,MAAM,CAAC,iBAAiB,GAAG,CAAC,IAAmB,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;IAC1E,CAAC;SAAM,CAAC;QACN,IAAI,CAAC,MAAM,CAAC,iBAAiB,GAAG,GAAG,EAAE,CAAC,YAAY,CAAC;QACnD,IAAI,CAAC,MAAM,CAAC,iBAAiB,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joist-knex",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,7 +16,7 @@
16
16
  "build"
17
17
  ],
18
18
  "peerDependencies": {
19
- "joist-core": "2.0.3",
19
+ "joist-core": "2.1.0",
20
20
  "knex": "^3.1.0",
21
21
  "pg": "^8.16.3"
22
22
  },