@take-out/postgres 0.0.97 → 0.0.99

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.
Files changed (64) hide show
  1. package/dist/cjs/chunkedQuery.cjs +60 -0
  2. package/dist/cjs/chunkedQuery.js +51 -0
  3. package/dist/cjs/chunkedQuery.js.map +6 -0
  4. package/dist/cjs/createDb.cjs +34 -0
  5. package/dist/cjs/createDb.js +28 -0
  6. package/dist/cjs/createDb.js.map +6 -0
  7. package/dist/cjs/createPool.cjs +40 -0
  8. package/dist/cjs/createPool.js +32 -0
  9. package/dist/cjs/createPool.js.map +6 -0
  10. package/dist/cjs/createServerHelpers.cjs +1 -1
  11. package/dist/cjs/createServerHelpers.js +1 -1
  12. package/dist/cjs/createServerHelpers.js.map +1 -1
  13. package/dist/cjs/getDBClient.cjs +172 -0
  14. package/dist/cjs/getDBClient.js +179 -0
  15. package/dist/cjs/getDBClient.js.map +6 -0
  16. package/dist/cjs/index.cjs +9 -32
  17. package/dist/cjs/index.js +4 -22
  18. package/dist/cjs/index.js.map +2 -2
  19. package/dist/cjs/migrate.cjs +1 -1
  20. package/dist/cjs/migrate.js +1 -1
  21. package/dist/esm/chunkedQuery.js +35 -0
  22. package/dist/esm/chunkedQuery.js.map +6 -0
  23. package/dist/esm/chunkedQuery.mjs +36 -0
  24. package/dist/esm/chunkedQuery.mjs.map +1 -0
  25. package/dist/esm/createDb.js +13 -0
  26. package/dist/esm/createDb.js.map +6 -0
  27. package/dist/esm/createDb.mjs +11 -0
  28. package/dist/esm/createDb.mjs.map +1 -0
  29. package/dist/esm/createPool.js +8 -0
  30. package/dist/esm/createPool.js.map +6 -0
  31. package/dist/esm/createPool.mjs +6 -0
  32. package/dist/esm/createPool.mjs.map +1 -0
  33. package/dist/esm/createServerHelpers.js +1 -1
  34. package/dist/esm/createServerHelpers.mjs +1 -1
  35. package/dist/esm/getDBClient.js +155 -0
  36. package/dist/esm/getDBClient.js.map +6 -0
  37. package/dist/esm/getDBClient.mjs +136 -0
  38. package/dist/esm/getDBClient.mjs.map +1 -0
  39. package/dist/esm/index.js +3 -14
  40. package/dist/esm/index.js.map +1 -1
  41. package/dist/esm/index.mjs +4 -15
  42. package/dist/esm/index.mjs.map +1 -1
  43. package/dist/esm/migrate.js +1 -1
  44. package/dist/esm/migrate.mjs +1 -1
  45. package/package.json +6 -16
  46. package/src/createDb.ts +14 -0
  47. package/src/createPool.ts +7 -0
  48. package/src/createServerHelpers.ts +1 -1
  49. package/src/index.ts +3 -31
  50. package/src/migrate.ts +1 -1
  51. package/types/chunkedQuery.d.ts +17 -0
  52. package/types/chunkedQuery.d.ts.map +1 -0
  53. package/types/createDb.d.ts +3 -0
  54. package/types/createDb.d.ts.map +1 -0
  55. package/types/createPool.d.ts +3 -0
  56. package/types/createPool.d.ts.map +1 -0
  57. package/types/createServerHelpers.d.ts +1 -1
  58. package/types/createServerHelpers.d.ts.map +1 -1
  59. package/types/getDBClient.d.ts +11 -0
  60. package/types/getDBClient.d.ts.map +1 -0
  61. package/types/index.d.ts +3 -7
  62. package/types/index.d.ts.map +1 -1
  63. /package/src/{helpers/chunkedQuery.ts → chunkedQuery.ts} +0 -0
  64. /package/src/{helpers/getDBClient.ts → getDBClient.ts} +0 -0
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/getDBClient.ts"],
4
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAA+C;AAY/C,MAAM,QAAQ,oBAAI,IAShB,GAEI,gBAAgB,CAAC,qBAA6B,kBAE9C,uBAAuB,CAAC,kBAA0B,WAA0B;AAChF,QAAM,MAAM,cAAc,gBAAgB;AAE1C,SAAK,MAAM,IAAI,GAAG,KAChB,MAAM,IAAI,KAAK;AAAA,IACb,MAAM,IAAI,UAAAA,QAAG,KAAK,MAAM;AAAA,IACxB,gBAAgB;AAAA,IAChB,qBAAqB;AA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AAM,YAAY,IAAI,UAAAD,QAAG,OAAO,cAAc;AAC9C,mBAAM,UAAU,QAAQ,GACjB;AAAA,IACT;AAAA,IACA;AAAA,MACE,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,QAAQ;AAAA,MACR,YAAY;AAAA,IACd;AAAA,EACF;AAGF;",
5
+ "names": ["pg", "client"]
6
+ }
@@ -1,9 +1,7 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
5
  var __export = (target, all) => {
8
6
  for (var name in all) __defProp(target, name, {
9
7
  get: all[name],
@@ -17,22 +15,12 @@ var __export = (target, all) => {
17
15
  });
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
- value: mod,
27
- enumerable: !0
28
- }) : target, mod)),
29
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
- value: !0
31
- }), mod);
18
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
19
+ value: !0
20
+ }), mod);
32
21
  var index_exports = {};
33
22
  __export(index_exports, {
34
- createDb: () => createDb,
35
- createPool: () => createPool,
23
+ createDb: () => import_createDb.createDb,
36
24
  createServerHelpers: () => import_createServerHelpers.createServerHelpers,
37
25
  createSql: () => import_sql.createSql,
38
26
  getDBClient: () => import_getDBClient.getDBClient,
@@ -41,19 +29,8 @@ __export(index_exports, {
41
29
  updateInChunks: () => import_chunkedQuery.updateInChunks
42
30
  });
43
31
  module.exports = __toCommonJS(index_exports);
44
- var import_node_postgres = require("drizzle-orm/node-postgres"),
45
- import_pg = __toESM(require("pg"), 1),
46
- import_sql = require("./sql.cjs"),
32
+ var import_sql = require("./sql.cjs"),
47
33
  import_createServerHelpers = require("./createServerHelpers.cjs"),
48
- import_getDBClient = require("./helpers/getDBClient.cjs"),
49
- import_chunkedQuery = require("./helpers/chunkedQuery.cjs");
50
- const createPool = connectionString => new import_pg.default.Pool({
51
- connectionString
52
- }),
53
- createDb = (connectionString, schema) => {
54
- const pool = createPool(connectionString);
55
- return (0, import_node_postgres.drizzle)(pool, {
56
- schema,
57
- logger: !1
58
- });
59
- };
34
+ import_getDBClient = require("./getDBClient.cjs"),
35
+ import_chunkedQuery = require("./chunkedQuery.cjs"),
36
+ import_createDb = require("./createDb.cjs");
package/dist/cjs/index.js CHANGED
@@ -1,8 +1,7 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
5
  var __export = (target, all) => {
7
6
  for (var name in all)
8
7
  __defProp(target, name, { get: all[name], enumerable: !0 });
@@ -12,18 +11,10 @@ var __export = (target, all) => {
12
11
  !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
12
  return to;
14
13
  };
15
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
16
- // If the importer is in node compatibility mode or this is not an ESM
17
- // file that has been converted to a CommonJS file using a Babel-
18
- // compatible transform (i.e. "__esModule" has not been set), then set
19
- // "default" to the CommonJS "module.exports" for node compatibility.
20
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
21
- mod
22
- )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
23
15
  var index_exports = {};
24
16
  __export(index_exports, {
25
- createDb: () => createDb,
26
- createPool: () => createPool,
17
+ createDb: () => import_createDb.createDb,
27
18
  createServerHelpers: () => import_createServerHelpers.createServerHelpers,
28
19
  createSql: () => import_sql.createSql,
29
20
  getDBClient: () => import_getDBClient.getDBClient,
@@ -32,14 +23,5 @@ __export(index_exports, {
32
23
  updateInChunks: () => import_chunkedQuery.updateInChunks
33
24
  });
34
25
  module.exports = __toCommonJS(index_exports);
35
- var import_node_postgres = require("drizzle-orm/node-postgres"), import_pg = __toESM(require("pg"), 1), import_sql = require("./sql"), import_createServerHelpers = require("./createServerHelpers"), import_getDBClient = require("./helpers/getDBClient"), import_chunkedQuery = require("./helpers/chunkedQuery");
36
- const createPool = (connectionString) => new import_pg.default.Pool({
37
- connectionString
38
- }), createDb = (connectionString, schema) => {
39
- const pool = createPool(connectionString);
40
- return (0, import_node_postgres.drizzle)(pool, {
41
- schema,
42
- logger: !1
43
- });
44
- };
26
+ var import_sql = require("./sql"), import_createServerHelpers = require("./createServerHelpers"), import_getDBClient = require("./getDBClient"), import_chunkedQuery = require("./chunkedQuery"), import_createDb = require("./createDb");
45
27
  //# sourceMappingURL=index.js.map
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAAwB,sCACxB,YAAe,2BAyBf,aAAyC,kBAEzC,6BAAwD,kCAExD,qBAA8D,kCAE9D,sBAAgD;AA1BzC,MAAM,aAAa,CAAC,qBAClB,IAAI,UAAAA,QAAG,KAAK;AAAA,EACjB;AACF,CAAC,GAGU,WAAW,CACtB,kBACA,WAC4B;AAC5B,QAAM,OAAO,WAAW,gBAAgB;AACxC,aAAO,8BAAQ,MAAM;AAAA,IACnB;AAAA,IACA,QAAQ;AAAA,EACV,CAAC;AACH;",
5
- "names": ["pg"]
4
+ "mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAyC,kBACzC,6BAAwD,kCACxD,qBAA8D,0BAC9D,sBAAgD,2BAChD,kBAAyB;",
5
+ "names": []
6
6
  }
@@ -24,7 +24,7 @@ __export(migrate_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(migrate_exports);
26
26
  var import_node_path = require("node:path"),
27
- import_getDBClient = require("./helpers/getDBClient.cjs");
27
+ import_getDBClient = require("./getDBClient.cjs");
28
28
  const isServerless = !!(process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.AWS_LAMBDA_RUNTIME_API || process.env.LAMBDA_RUNTIME_DIR || process.env.IS_SERVERLESS);
29
29
  async function migrate(options) {
30
30
  const {
@@ -17,7 +17,7 @@ __export(migrate_exports, {
17
17
  migrate: () => migrate
18
18
  });
19
19
  module.exports = __toCommonJS(migrate_exports);
20
- var import_node_path = require("node:path"), import_getDBClient = require("./helpers/getDBClient");
20
+ var import_node_path = require("node:path"), import_getDBClient = require("./getDBClient");
21
21
  const isServerless = !!(process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.AWS_LAMBDA_RUNTIME_API || process.env.LAMBDA_RUNTIME_DIR || process.env.IS_SERVERLESS);
22
22
  async function migrate(options) {
23
23
  const {
@@ -0,0 +1,35 @@
1
+ async function processInChunks(client, query, processor, options = {}) {
2
+ const { chunkSize = 1e3, onProgress } = options;
3
+ let offset = 0, hasMore = !0, totalProcessed = 0;
4
+ const countQuery = query.replace(/SELECT .+ FROM/, "SELECT COUNT(*) FROM").replace(/ORDER BY .+/, ""), countResult = await client.query(countQuery), totalCount = Number.parseInt(countResult.rows[0].count, 10);
5
+ for (; hasMore; ) {
6
+ const paginatedQuery = `${query} LIMIT ${chunkSize} OFFSET ${offset}`, result = await client.query(paginatedQuery);
7
+ if (result.rows.length === 0) {
8
+ hasMore = !1;
9
+ break;
10
+ }
11
+ await processor(result.rows), totalProcessed += result.rows.length, offset += chunkSize, onProgress && onProgress(totalProcessed, totalCount), result.rows.length < chunkSize && (hasMore = !1);
12
+ }
13
+ }
14
+ async function updateInChunks(client, tableName, selectQuery, transformer, options = {}) {
15
+ let totalUpdated = 0;
16
+ return await processInChunks(
17
+ client,
18
+ selectQuery,
19
+ async (rows) => {
20
+ for (const row of rows) {
21
+ const updates = await transformer(row);
22
+ if (updates && Object.keys(updates).length > 0) {
23
+ const setClause = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(", "), values = [row.id, ...Object.values(updates)];
24
+ await client.query(`UPDATE ${tableName} SET ${setClause} WHERE id = $1`, values), totalUpdated++;
25
+ }
26
+ }
27
+ },
28
+ options
29
+ ), totalUpdated;
30
+ }
31
+ export {
32
+ processInChunks,
33
+ updateInChunks
34
+ };
35
+ //# sourceMappingURL=chunkedQuery.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/chunkedQuery.ts"],
4
+ "mappings": "AAUA,eAAsB,gBACpB,QACA,OACA,WACA,UAA+B,CAAC,GACjB;AACf,QAAM,EAAE,YAAY,KAAM,WAAW,IAAI;AACzC,MAAI,SAAS,GACT,UAAU,IACV,iBAAiB;AAGrB,QAAM,aAAa,MAChB,QAAQ,kBAAkB,sBAAsB,EAChD,QAAQ,eAAe,EAAE,GACtB,cAAc,MAAM,OAAO,MAAM,UAAU,GAC3C,aAAa,OAAO,SAAS,YAAY,KAAK,CAAC,EAAE,OAAO,EAAE;AAEhE,SAAO,WAAS;AACd,UAAM,iBAAiB,GAAG,KAAK,UAAU,SAAS,WAAW,MAAM,IAC7D,SAAS,MAAM,OAAO,MAAS,cAAc;AAEnD,QAAI,OAAO,KAAK,WAAW,GAAG;AAC5B,gBAAU;AACV;AAAA,IACF;AAEA,UAAM,UAAU,OAAO,IAAI,GAE3B,kBAAkB,OAAO,KAAK,QAC9B,UAAU,WAEN,cACF,WAAW,gBAAgB,UAAU,GAInC,OAAO,KAAK,SAAS,cACvB,UAAU;AAAA,EAEd;AACF;AAKA,eAAsB,eACpB,QACA,WACA,aACA,aACA,UAA+B,CAAC,GACf;AACjB,MAAI,eAAe;AAEnB,eAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,OAAO,SAAS;AACd,iBAAW,OAAO,MAAM;AACtB,cAAM,UAAU,MAAM,YAAY,GAAG;AAErC,YAAI,WAAW,OAAO,KAAK,OAAO,EAAE,SAAS,GAAG;AAE9C,gBAAM,YAAY,OAAO,KAAK,OAAO,EAClC,IAAI,CAAC,KAAK,UAAU,GAAG,GAAG,OAAO,QAAQ,CAAC,EAAE,EAC5C,KAAK,IAAI,GAEN,SAAS,CAAC,IAAI,IAAI,GAAG,OAAO,OAAO,OAAO,CAAC;AAEjD,gBAAM,OAAO,MAAM,UAAU,SAAS,QAAQ,SAAS,kBAAkB,MAAM,GAE/E;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA;AAAA,EACF,GAEO;AACT;",
5
+ "names": []
6
+ }
@@ -0,0 +1,36 @@
1
+ async function processInChunks(client, query, processor, options = {}) {
2
+ const {
3
+ chunkSize = 1e3,
4
+ onProgress
5
+ } = options;
6
+ let offset = 0,
7
+ hasMore = !0,
8
+ totalProcessed = 0;
9
+ const countQuery = query.replace(/SELECT .+ FROM/, "SELECT COUNT(*) FROM").replace(/ORDER BY .+/, ""),
10
+ countResult = await client.query(countQuery),
11
+ totalCount = Number.parseInt(countResult.rows[0].count, 10);
12
+ for (; hasMore;) {
13
+ const paginatedQuery = `${query} LIMIT ${chunkSize} OFFSET ${offset}`,
14
+ result = await client.query(paginatedQuery);
15
+ if (result.rows.length === 0) {
16
+ hasMore = !1;
17
+ break;
18
+ }
19
+ await processor(result.rows), totalProcessed += result.rows.length, offset += chunkSize, onProgress && onProgress(totalProcessed, totalCount), result.rows.length < chunkSize && (hasMore = !1);
20
+ }
21
+ }
22
+ async function updateInChunks(client, tableName, selectQuery, transformer, options = {}) {
23
+ let totalUpdated = 0;
24
+ return await processInChunks(client, selectQuery, async rows => {
25
+ for (const row of rows) {
26
+ const updates = await transformer(row);
27
+ if (updates && Object.keys(updates).length > 0) {
28
+ const setClause = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(", "),
29
+ values = [row.id, ...Object.values(updates)];
30
+ await client.query(`UPDATE ${tableName} SET ${setClause} WHERE id = $1`, values), totalUpdated++;
31
+ }
32
+ }
33
+ }, options), totalUpdated;
34
+ }
35
+ export { processInChunks, updateInChunks };
36
+ //# sourceMappingURL=chunkedQuery.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["processInChunks","client","query","processor","options","chunkSize","onProgress","offset","hasMore","totalProcessed","countQuery","replace","countResult","totalCount","Number","parseInt","rows","count","paginatedQuery","result","length","updateInChunks","tableName","selectQuery","transformer","totalUpdated","row","updates","Object","keys","setClause","map","key","index","join","values","id"],"sources":["../../src/chunkedQuery.ts"],"sourcesContent":[null],"mappings":"AAUA,eAAsBA,gBACpBC,MAAA,EACAC,KAAA,EACAC,SAAA,EACAC,OAAA,GAA+B,CAAC,GACjB;EACf,MAAM;IAAEC,SAAA,GAAY;IAAMC;EAAW,IAAIF,OAAA;EACzC,IAAIG,MAAA,GAAS;IACTC,OAAA,GAAU;IACVC,cAAA,GAAiB;EAGrB,MAAMC,UAAA,GAAaR,KAAA,CAChBS,OAAA,CAAQ,kBAAkB,sBAAsB,EAChDA,OAAA,CAAQ,eAAe,EAAE;IACtBC,WAAA,GAAc,MAAMX,MAAA,CAAOC,KAAA,CAAMQ,UAAU;IAC3CG,UAAA,GAAaC,MAAA,CAAOC,QAAA,CAASH,WAAA,C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AAA,CAAM,UAAUoB,SAAS,QAAQQ,SAAS,kBAAkBK,MAAM,GAE/EV,YAAA;MACF;IACF;EACF,GACArB,OACF,GAEOqB,YAAA;AACT","ignoreList":[]}
@@ -0,0 +1,13 @@
1
+ import { drizzle } from "drizzle-orm/node-postgres";
2
+ import { createPool } from "./createPool";
3
+ const createDb = (connectionString, schema) => {
4
+ const pool = createPool(connectionString);
5
+ return drizzle(pool, {
6
+ schema,
7
+ logger: !1
8
+ });
9
+ };
10
+ export {
11
+ createDb
12
+ };
13
+ //# sourceMappingURL=createDb.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/createDb.ts"],
4
+ "mappings": "AAAA,SAA8B,eAAe;AAE7C,SAAS,kBAAkB;AAEpB,MAAM,WAAW,CACtB,kBACA,WAC4B;AAC5B,QAAM,OAAO,WAAW,gBAAgB;AACxC,SAAO,QAAQ,MAAM;AAAA,IACnB;AAAA,IACA,QAAQ;AAAA,EACV,CAAC;AACH;",
5
+ "names": []
6
+ }
@@ -0,0 +1,11 @@
1
+ import { drizzle } from "drizzle-orm/node-postgres";
2
+ import { createPool } from "./createPool.mjs";
3
+ const createDb = (connectionString, schema) => {
4
+ const pool = createPool(connectionString);
5
+ return drizzle(pool, {
6
+ schema,
7
+ logger: !1
8
+ });
9
+ };
10
+ export { createDb };
11
+ //# sourceMappingURL=createDb.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["drizzle","createPool","createDb","connectionString","schema","pool","logger"],"sources":["../../src/createDb.ts"],"sourcesContent":[null],"mappings":"AAAA,SAA8BA,OAAA,QAAe;AAE7C,SAASC,UAAA,QAAkB;AAEpB,MAAMC,QAAA,GAAWA,CACtBC,gBAAA,EACAC,MAAA,KAC4B;EAC5B,MAAMC,IAAA,GAAOJ,UAAA,CAAWE,gBAAgB;EACxC,OAAOH,OAAA,CAAQK,IAAA,EAAM;IACnBD,MAAA;IACAE,MAAA,EAAQ;EACV,CAAC;AACH","ignoreList":[]}
@@ -0,0 +1,8 @@
1
+ import pg from "pg";
2
+ const createPool = (connectionString) => new pg.Pool({
3
+ connectionString
4
+ });
5
+ export {
6
+ createPool
7
+ };
8
+ //# sourceMappingURL=createPool.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/createPool.ts"],
4
+ "mappings": "AAAA,OAAO,QAAuB;AAEvB,MAAM,aAAa,CAAC,qBAClB,IAAI,GAAG,KAAK;AAAA,EACjB;AACF,CAAC;",
5
+ "names": []
6
+ }
@@ -0,0 +1,6 @@
1
+ import pg from "pg";
2
+ const createPool = connectionString => new pg.Pool({
3
+ connectionString
4
+ });
5
+ export { createPool };
6
+ //# sourceMappingURL=createPool.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["pg","createPool","connectionString","Pool"],"sources":["../../src/createPool.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAOA,EAAA,MAAuB;AAEvB,MAAMC,UAAA,GAAcC,gBAAA,IAClB,IAAIF,EAAA,CAAGG,IAAA,CAAK;EACjBD;AACF,CAAC","ignoreList":[]}
@@ -1,4 +1,4 @@
1
- import { getDBClient } from "./helpers/getDBClient";
1
+ import { getDBClient } from "./getDBClient";
2
2
  import { createSql, setDefaultPool } from "./sql";
3
3
  function createServerHelpers(pool) {
4
4
  const sql = createSql(pool);
@@ -1,4 +1,4 @@
1
- import { getDBClient } from "./helpers/getDBClient.mjs";
1
+ import { getDBClient } from "./getDBClient.mjs";
2
2
  import { createSql, setDefaultPool } from "./sql.mjs";
3
3
  function createServerHelpers(pool) {
4
4
  const sql = createSql(pool);
@@ -0,0 +1,155 @@
1
+ import pg from "pg";
2
+ const cache = /* @__PURE__ */ new Map(), createPoolKey = (connectionString) => connectionString, getOrCreatePoolCache = (connectionString, config) => {
3
+ const key = createPoolKey(connectionString);
4
+ return cache.has(key) || cache.set(key, {
5
+ pool: new pg.Pool(config),
6
+ maxConnections: null,
7
+ reservedConnections: null,
8
+ openedConnections: null,
9
+ openedConnectionsLastUpdate: null
10
+ }), cache.get(key);
11
+ };
12
+ async function getDBClient(options = {}) {
13
+ const { pool, connectionString, retries = 8 } = options;
14
+ if (!pool && !connectionString)
15
+ throw new Error("Either pool or connectionString must be provided");
16
+ let client = null;
17
+ try {
18
+ return client = await tryToGetNewClientFromPool(pool, connectionString, retries), client;
19
+ } catch (error) {
20
+ throw console.error("Failed to get DB client:", error), error;
21
+ }
22
+ }
23
+ async function tryToGetNewClientFromPool(providedPool, connectionString, retries) {
24
+ const { default: retry } = await import("async-retry");
25
+ return await retry(
26
+ async () => {
27
+ if (providedPool) {
28
+ console.info("Connecting to provided pool...");
29
+ const client2 = await providedPool.connect();
30
+ return console.info("Connected to pool"), client2;
31
+ }
32
+ if (!connectionString)
33
+ throw new Error("No connection string provided");
34
+ const poolCache = getOrCreatePoolCache(connectionString, {
35
+ connectionString,
36
+ connectionTimeoutMillis: 5e3,
37
+ // idle_session_timeout set to 35s on server, client timeout at 30s
38
+ // fix via https://github.com/brianc/node-postgres/issues/2718#issuecomment-2094885323
39
+ idleTimeoutMillis: 3e4,
40
+ allowExitOnIdle: !0
41
+ });
42
+ console.info(`Connecting to pool ${connectionString}...`);
43
+ const client = await poolCache.pool.connect();
44
+ return console.info("Connected to pool"), client;
45
+ },
46
+ {
47
+ retries,
48
+ minTimeout: 300,
49
+ factor: 2,
50
+ maxTimeout: 8e3
51
+ }
52
+ );
53
+ }
54
+ async function queryDb(queryText, params, options = {}) {
55
+ let client = null;
56
+ try {
57
+ return client = await tryToGetNewClientFromPool(
58
+ options.pool,
59
+ options.connectionString,
60
+ options.retries || 8
61
+ ), await client.query(queryText, params);
62
+ } catch (error) {
63
+ throw console.error("Database query failed:", {
64
+ query: queryText,
65
+ error: error instanceof Error ? error.message : String(error)
66
+ }), error;
67
+ } finally {
68
+ if (client && options.connectionString)
69
+ if (await checkForTooManyConnections(
70
+ client,
71
+ options.connectionString
72
+ )) {
73
+ const poolCache = cache.get(createPoolKey(options.connectionString));
74
+ client.release(), await poolCache?.pool.end(), poolCache && cache.delete(createPoolKey(options.connectionString));
75
+ } else
76
+ client.release();
77
+ else client && client.release();
78
+ }
79
+ }
80
+ async function checkForTooManyConnections(client, connectionString) {
81
+ const poolCache = cache.get(createPoolKey(connectionString));
82
+ if (!poolCache) return !1;
83
+ const currentTime = Date.now(), openedConnectionsMaxAge = 1e4, maxConnectionsTolerance = 0.9;
84
+ if (poolCache.maxConnections === null || poolCache.reservedConnections === null) {
85
+ const [maxConnections, reservedConnections] = await getConnectionLimits(client);
86
+ console.info(
87
+ `Max connections: ${maxConnections}, Reserved connections: ${reservedConnections}`
88
+ ), poolCache.maxConnections = maxConnections, poolCache.reservedConnections = reservedConnections;
89
+ }
90
+ if (poolCache.openedConnections === null || poolCache.openedConnectionsLastUpdate === null || currentTime - poolCache.openedConnectionsLastUpdate > openedConnectionsMaxAge) {
91
+ const openedConnections = await getOpenedConnections(client, connectionString);
92
+ poolCache.openedConnections = openedConnections, poolCache.openedConnectionsLastUpdate = currentTime;
93
+ }
94
+ return poolCache.openedConnections > (poolCache.maxConnections - poolCache.reservedConnections) * maxConnectionsTolerance ? (console.warn(
95
+ `Too many connections detected: ${poolCache.openedConnections}/${poolCache.maxConnections - poolCache.reservedConnections}`
96
+ ), !0) : !1;
97
+ }
98
+ async function getConnectionLimits(client) {
99
+ console.info("Getting connection limits...");
100
+ const maxConnectionsResult = await client.query("SHOW max_connections"), reservedConnectionResult = await client.query(
101
+ "SHOW superuser_reserved_connections"
102
+ );
103
+ return [
104
+ Number.parseInt(maxConnectionsResult.rows[0].max_connections, 10),
105
+ Number.parseInt(reservedConnectionResult.rows[0].superuser_reserved_connections, 10)
106
+ ];
107
+ }
108
+ async function getOpenedConnections(client, connectionString) {
109
+ const dbName = new URL(connectionString).pathname.slice(1);
110
+ console.info("Getting opened connections...");
111
+ const openConnectionsResult = await client.query(
112
+ "SELECT numbackends as opened_connections FROM pg_stat_database WHERE datname = $1",
113
+ [dbName]
114
+ ), result = Number.parseInt(
115
+ openConnectionsResult.rows[0]?.opened_connections || 0,
116
+ 10
117
+ );
118
+ return console.info(`Opened connections: ${result}`), result;
119
+ }
120
+ async function getNewClient(options = {}) {
121
+ const { connectionString } = options;
122
+ if (!connectionString)
123
+ throw new Error("connectionString is required for getNewClient");
124
+ try {
125
+ return await tryToGetNewClient(connectionString);
126
+ } catch (error) {
127
+ throw console.error("Failed to get new client:", error), error;
128
+ }
129
+ }
130
+ async function tryToGetNewClient(connectionString) {
131
+ const configurations = {
132
+ connectionString,
133
+ connectionTimeoutMillis: 5e3,
134
+ idleTimeoutMillis: 3e4,
135
+ allowExitOnIdle: !0
136
+ }, { default: retry } = await import("async-retry");
137
+ return await retry(
138
+ async () => {
139
+ const newClient = new pg.Client(configurations);
140
+ return await newClient.connect(), newClient;
141
+ },
142
+ {
143
+ retries: 10,
144
+ minTimeout: 100,
145
+ factor: 2,
146
+ maxTimeout: 5e3
147
+ }
148
+ );
149
+ }
150
+ export {
151
+ getDBClient,
152
+ getNewClient,
153
+ queryDb
154
+ };
155
+ //# sourceMappingURL=getDBClient.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/getDBClient.ts"],
4
+ "mappings": "AAAA,OAAO,QAAwC;AAY/C,MAAM,QAAQ,oBAAI,IAShB,GAEI,gBAAgB,CAAC,qBAA6B,kBAE9C,uBAAuB,CAAC,kBAA0B,WAA0B;AAChF,QAAM,MAAM,cAAc,gBAAgB;AAE1C,SAAK,MAAM,IAAI,GAAG,KAChB,MAAM,IAAI,KAAK;AAAA,IACb,MAAM,IAAI,GAAG,KAAK,MAAM;AAAA,IACxB,gBAAgB;AAAA,IAChB,qBAAqB;AAAA,IACrB,mBAAmB;AAAA,IACnB,6BAA6B;AAAA,EAC/B,CAAC,GAGI,MAAM,IAAI,GAAG;AACtB;AAEA,eAAsB,YAAY,UAA8B,CAAC,GAAwB;AACvF,QAAM,EAAE,MAAM,kBAAkB,UAAU,EAA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;AAAA,EACF;AAGF;",
5
+ "names": ["client"]
6
+ }
@@ -0,0 +1,136 @@
1
+ import pg from "pg";
2
+ const cache = /* @__PURE__ */new Map(),
3
+ createPoolKey = connectionString => connectionString,
4
+ getOrCreatePoolCache = (connectionString, config) => {
5
+ const key = createPoolKey(connectionString);
6
+ return cache.has(key) || cache.set(key, {
7
+ pool: new pg.Pool(config),
8
+ maxConnections: null,
9
+ reservedConnections: null,
10
+ openedConnections: null,
11
+ openedConnectionsLastUpdate: null
12
+ }), cache.get(key);
13
+ };
14
+ async function getDBClient(options = {}) {
15
+ const {
16
+ pool,
17
+ connectionString,
18
+ retries = 8
19
+ } = options;
20
+ if (!pool && !connectionString) throw new Error("Either pool or connectionString must be provided");
21
+ let client = null;
22
+ try {
23
+ return client = await tryToGetNewClientFromPool(pool, connectionString, retries), client;
24
+ } catch (error) {
25
+ throw console.error("Failed to get DB client:", error), error;
26
+ }
27
+ }
28
+ async function tryToGetNewClientFromPool(providedPool, connectionString, retries) {
29
+ const {
30
+ default: retry
31
+ } = await import("async-retry");
32
+ return await retry(async () => {
33
+ if (providedPool) {
34
+ console.info("Connecting to provided pool...");
35
+ const client2 = await providedPool.connect();
36
+ return console.info("Connected to pool"), client2;
37
+ }
38
+ if (!connectionString) throw new Error("No connection string provided");
39
+ const poolCache = getOrCreatePoolCache(connectionString, {
40
+ connectionString,
41
+ connectionTimeoutMillis: 5e3,
42
+ // idle_session_timeout set to 35s on server, client timeout at 30s
43
+ // fix via https://github.com/brianc/node-postgres/issues/2718#issuecomment-2094885323
44
+ idleTimeoutMillis: 3e4,
45
+ allowExitOnIdle: !0
46
+ });
47
+ console.info(`Connecting to pool ${connectionString}...`);
48
+ const client = await poolCache.pool.connect();
49
+ return console.info("Connected to pool"), client;
50
+ }, {
51
+ retries,
52
+ minTimeout: 300,
53
+ factor: 2,
54
+ maxTimeout: 8e3
55
+ });
56
+ }
57
+ async function queryDb(queryText, params, options = {}) {
58
+ let client = null;
59
+ try {
60
+ return client = await tryToGetNewClientFromPool(options.pool, options.connectionString, options.retries || 8), await client.query(queryText, params);
61
+ } catch (error) {
62
+ throw console.error("Database query failed:", {
63
+ query: queryText,
64
+ error: error instanceof Error ? error.message : String(error)
65
+ }), error;
66
+ } finally {
67
+ if (client && options.connectionString) {
68
+ if (await checkForTooManyConnections(client, options.connectionString)) {
69
+ const poolCache = cache.get(createPoolKey(options.connectionString));
70
+ client.release(), await poolCache?.pool.end(), poolCache && cache.delete(createPoolKey(options.connectionString));
71
+ } else client.release();
72
+ } else client && client.release();
73
+ }
74
+ }
75
+ async function checkForTooManyConnections(client, connectionString) {
76
+ const poolCache = cache.get(createPoolKey(connectionString));
77
+ if (!poolCache) return !1;
78
+ const currentTime = Date.now(),
79
+ openedConnectionsMaxAge = 1e4,
80
+ maxConnectionsTolerance = 0.9;
81
+ if (poolCache.maxConnections === null || poolCache.reservedConnections === null) {
82
+ const [maxConnections, reservedConnections] = await getConnectionLimits(client);
83
+ console.info(`Max connections: ${maxConnections}, Reserved connections: ${reservedConnections}`), poolCache.maxConnections = maxConnections, poolCache.reservedConnections = reservedConnections;
84
+ }
85
+ if (poolCache.openedConnections === null || poolCache.openedConnectionsLastUpdate === null || currentTime - poolCache.openedConnectionsLastUpdate > openedConnectionsMaxAge) {
86
+ const openedConnections = await getOpenedConnections(client, connectionString);
87
+ poolCache.openedConnections = openedConnections, poolCache.openedConnectionsLastUpdate = currentTime;
88
+ }
89
+ return poolCache.openedConnections > (poolCache.maxConnections - poolCache.reservedConnections) * maxConnectionsTolerance ? (console.warn(`Too many connections detected: ${poolCache.openedConnections}/${poolCache.maxConnections - poolCache.reservedConnections}`), !0) : !1;
90
+ }
91
+ async function getConnectionLimits(client) {
92
+ console.info("Getting connection limits...");
93
+ const maxConnectionsResult = await client.query("SHOW max_connections"),
94
+ reservedConnectionResult = await client.query("SHOW superuser_reserved_connections");
95
+ return [Number.parseInt(maxConnectionsResult.rows[0].max_connections, 10), Number.parseInt(reservedConnectionResult.rows[0].superuser_reserved_connections, 10)];
96
+ }
97
+ async function getOpenedConnections(client, connectionString) {
98
+ const dbName = new URL(connectionString).pathname.slice(1);
99
+ console.info("Getting opened connections...");
100
+ const openConnectionsResult = await client.query("SELECT numbackends as opened_connections FROM pg_stat_database WHERE datname = $1", [dbName]),
101
+ result = Number.parseInt(openConnectionsResult.rows[0]?.opened_connections || 0, 10);
102
+ return console.info(`Opened connections: ${result}`), result;
103
+ }
104
+ async function getNewClient(options = {}) {
105
+ const {
106
+ connectionString
107
+ } = options;
108
+ if (!connectionString) throw new Error("connectionString is required for getNewClient");
109
+ try {
110
+ return await tryToGetNewClient(connectionString);
111
+ } catch (error) {
112
+ throw console.error("Failed to get new client:", error), error;
113
+ }
114
+ }
115
+ async function tryToGetNewClient(connectionString) {
116
+ const configurations = {
117
+ connectionString,
118
+ connectionTimeoutMillis: 5e3,
119
+ idleTimeoutMillis: 3e4,
120
+ allowExitOnIdle: !0
121
+ },
122
+ {
123
+ default: retry
124
+ } = await import("async-retry");
125
+ return await retry(async () => {
126
+ const newClient = new pg.Client(configurations);
127
+ return await newClient.connect(), newClient;
128
+ }, {
129
+ retries: 10,
130
+ minTimeout: 100,
131
+ factor: 2,
132
+ maxTimeout: 5e3
133
+ });
134
+ }
135
+ export { getDBClient, getNewClient, queryDb };
136
+ //# sourceMappingURL=getDBClient.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["pg","cache","Map","createPoolKey","connectionString","getOrCreatePoolCache","config","key","has","set","pool","Pool","maxConnections","reservedConnections","openedConnections","openedConnectionsLastUpdate","get","getDBClient","options","retries","Error","client","tryToGetNewClientFromPool","error","console","providedPool","default","retry","info","client2","connect","poolCache","connectionTimeoutMillis","idleTimeoutMillis","allowExitOnIdle","minTimeout","factor","maxTimeout","queryDb","queryText","params","query","message","String","checkForTooManyConnections","release","end","delete","currentTime","Date","now","openedConnectionsMaxAge","maxConnectionsTolerance","getConnectionLimits","getOpenedConnections","warn","maxConnectionsResult","reservedConnectionResult","Number","parseInt","rows","max_connections","superuser_reserved_connections","dbName","URL","pathname","slice","openConnectionsResult","result","opened_connections","getNewClient","tryToGetNewClient","configurations","newClient","Client"],"sources":["../../src/getDBClient.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAOA,EAAA,MAAwC;AAY/C,MAAMC,KAAA,GAAQ,mBAAIC,GAAA,CAShB;EAEIC,aAAA,GAAiBC,gBAAA,IAA6BA,gBAAA;EAE9CC,oBAAA,GAAuBA,CAACD,gBAAA,EAA0BE,MAAA,KAA0B;IAChF,MAAMC,GAAA,GAAMJ,aAAA,CAAcC,gBAAgB;IAE1C,OAAKH,KAAA,CAAMO,GAAA,CAAID,GAAG,KAChBN,KAAA,CAAMQ,GAAA,CAAIF,GAAA,EAAK;MACbG,IAAA,EAAM,IAAIV,EAAA,CAAGW,IAAA,CAAKL,MAAM;MACxBM,cAAA,EAAgB;MAChBC,mBAAA,EAAqB;MACrBC,iBAAA,EAAmB;MACnBC,2BAAA,EAA6B;IAC/B,CAAC,GAGId,KAAA,CAAMe,GAAA,CAAIT,GAAG;EACtB;AAEA,eAAsBU,YAAYC,OAAA,GAA8B,CAAC,GAAwB;EACvF,MAAM;IAAER,IAAA;IAAMN,gBAAA;IAAkBe,OAAA,GAAU;EAAE,IAAID,OAAA;EAEhD,IAAI,CAACR,IAAA,IAAQ,CAACN,gBAAA,EACZ,MAAM,IAAIgB,KAAA,CAAM,kDAAkD;EAGpE,IAAIC,MAAA,GAA4B;EAEhC,IAAI;IACF,OAAAA,MAAA,GAAS,MAAMC,yBAAA,CAA0BZ,IAAA,EAAMN,gBAAA,EAAkBe,OAAO,GACjEE,MAAA;EACT,SAASE,KAAA,EAAO;IACd,MAAAC,OAAA,CAAQD,KAAA,CAAM,4BAA4BA,KAAK,GACzCA,KAAA;EACR;AACF;AAEA,eAAeD,0BACbG,YAAA,EACArB,gBAAA,EACAe,OAAA,EACqB;EACrB,MAAM;IAAEO,OAAA,EAASC;EAAM,IAAI,MAAM,OAAO,aAA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