@constructive-io/job-pg 0.3.20 → 0.4.1
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/README.md +59 -1
- package/esm/index.js +77 -0
- package/{dist/index.js → index.js} +6 -5
- package/package.json +17 -8
- package/CHANGELOG.md +0 -72
- package/jest.config.js +0 -18
- package/src/index.ts +0 -103
- package/tsconfig.esm.json +0 -9
- package/tsconfig.json +0 -9
- /package/{dist/index.d.ts → index.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1 +1,59 @@
|
|
|
1
|
-
# job-pg
|
|
1
|
+
# job-pg
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Education and Tutorials
|
|
6
|
+
|
|
7
|
+
1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
8
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
9
|
+
|
|
10
|
+
2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
11
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
12
|
+
|
|
13
|
+
3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
14
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
15
|
+
|
|
16
|
+
4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
17
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
18
|
+
|
|
19
|
+
5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
|
|
20
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
21
|
+
|
|
22
|
+
6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
23
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
24
|
+
|
|
25
|
+
7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
26
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
27
|
+
|
|
28
|
+
## Related Constructive Tooling
|
|
29
|
+
|
|
30
|
+
### 📦 Package Management
|
|
31
|
+
|
|
32
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
33
|
+
|
|
34
|
+
### 🧪 Testing
|
|
35
|
+
|
|
36
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
37
|
+
* [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
|
|
38
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
39
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
40
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
|
|
41
|
+
|
|
42
|
+
### 🧠 Parsing & AST
|
|
43
|
+
|
|
44
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
45
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
46
|
+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
47
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
48
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
49
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
50
|
+
|
|
51
|
+
## Credits
|
|
52
|
+
|
|
53
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
54
|
+
|
|
55
|
+
## Disclaimer
|
|
56
|
+
|
|
57
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
58
|
+
|
|
59
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Pool } from 'pg';
|
|
2
|
+
import { getJobPgConfig } from '@constructive-io/job-utils';
|
|
3
|
+
import { createLogger } from '@pgpmjs/logger';
|
|
4
|
+
// k8s only does SIGINT
|
|
5
|
+
// other events are bad for babel-watch
|
|
6
|
+
const SYS_EVENTS = [
|
|
7
|
+
// 'SIGUSR2',
|
|
8
|
+
'SIGINT'
|
|
9
|
+
// 'SIGTERM',
|
|
10
|
+
// 'SIGPIPE',
|
|
11
|
+
// 'SIGHUP',
|
|
12
|
+
// 'SIGABRT'
|
|
13
|
+
];
|
|
14
|
+
function once(fn, context) {
|
|
15
|
+
let called = false;
|
|
16
|
+
let result;
|
|
17
|
+
return function (...args) {
|
|
18
|
+
if (!called && fn) {
|
|
19
|
+
// context is just forwarded through, it is not inspected
|
|
20
|
+
result = fn.apply((context ?? this), args);
|
|
21
|
+
called = true;
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const pgPoolConfig = getJobPgConfig();
|
|
27
|
+
const logger = createLogger('job-pg');
|
|
28
|
+
const end = (pool) => {
|
|
29
|
+
try {
|
|
30
|
+
// Pool has internal state flags, but they are not part of the public type
|
|
31
|
+
const state = pool;
|
|
32
|
+
if (state.ended || state.ending) {
|
|
33
|
+
logger.error('DO NOT CLOSE pool, why are you trying to call end() when already ended?');
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
void pool.end();
|
|
37
|
+
logger.info('successfully closed pool.');
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
process.stderr.write(String(e));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
class PoolManager {
|
|
44
|
+
pgPool;
|
|
45
|
+
callbacks;
|
|
46
|
+
_closed;
|
|
47
|
+
constructor({ pgPool = new Pool(pgPoolConfig) } = {}) {
|
|
48
|
+
this.pgPool = pgPool;
|
|
49
|
+
this.callbacks = [];
|
|
50
|
+
this._closed = false;
|
|
51
|
+
const closeOnce = once(async () => {
|
|
52
|
+
logger.info('closing pg pool manager...');
|
|
53
|
+
await this.close();
|
|
54
|
+
}, this);
|
|
55
|
+
SYS_EVENTS.forEach((event) => {
|
|
56
|
+
process.on(event, closeOnce);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
onClose(fn, context, args = []) {
|
|
60
|
+
this.callbacks.push([fn, context, args]);
|
|
61
|
+
}
|
|
62
|
+
getPool() {
|
|
63
|
+
return this.pgPool;
|
|
64
|
+
}
|
|
65
|
+
async close() {
|
|
66
|
+
if (this._closed)
|
|
67
|
+
return;
|
|
68
|
+
for (const [fn, context, args] of this.callbacks) {
|
|
69
|
+
logger.info('closing fn', fn.name);
|
|
70
|
+
await fn.apply(context, args);
|
|
71
|
+
}
|
|
72
|
+
end(this.pgPool);
|
|
73
|
+
this._closed = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const mngr = new PoolManager();
|
|
77
|
+
export default mngr;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable no-console */
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
const pg_1 = require("pg");
|
|
5
4
|
const job_utils_1 = require("@constructive-io/job-utils");
|
|
5
|
+
const logger_1 = require("@pgpmjs/logger");
|
|
6
6
|
// k8s only does SIGINT
|
|
7
7
|
// other events are bad for babel-watch
|
|
8
8
|
const SYS_EVENTS = [
|
|
@@ -26,16 +26,17 @@ function once(fn, context) {
|
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
const pgPoolConfig = (0, job_utils_1.getJobPgConfig)();
|
|
29
|
+
const logger = (0, logger_1.createLogger)('job-pg');
|
|
29
30
|
const end = (pool) => {
|
|
30
31
|
try {
|
|
31
32
|
// Pool has internal state flags, but they are not part of the public type
|
|
32
33
|
const state = pool;
|
|
33
34
|
if (state.ended || state.ending) {
|
|
34
|
-
|
|
35
|
+
logger.error('DO NOT CLOSE pool, why are you trying to call end() when already ended?');
|
|
35
36
|
return;
|
|
36
37
|
}
|
|
37
38
|
void pool.end();
|
|
38
|
-
|
|
39
|
+
logger.info('successfully closed pool.');
|
|
39
40
|
}
|
|
40
41
|
catch (e) {
|
|
41
42
|
process.stderr.write(String(e));
|
|
@@ -50,7 +51,7 @@ class PoolManager {
|
|
|
50
51
|
this.callbacks = [];
|
|
51
52
|
this._closed = false;
|
|
52
53
|
const closeOnce = once(async () => {
|
|
53
|
-
|
|
54
|
+
logger.info('closing pg pool manager...');
|
|
54
55
|
await this.close();
|
|
55
56
|
}, this);
|
|
56
57
|
SYS_EVENTS.forEach((event) => {
|
|
@@ -67,7 +68,7 @@ class PoolManager {
|
|
|
67
68
|
if (this._closed)
|
|
68
69
|
return;
|
|
69
70
|
for (const [fn, context, args] of this.callbacks) {
|
|
70
|
-
|
|
71
|
+
logger.info('closing fn', fn.name);
|
|
71
72
|
await fn.apply(context, args);
|
|
72
73
|
}
|
|
73
74
|
end(this.pgPool);
|
package/package.json
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructive-io/job-pg",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "job pg",
|
|
5
5
|
"author": "Constructive <developers@constructive.io>",
|
|
6
6
|
"homepage": "https://github.com/constructive-io/jobs/tree/master/packages/job-pg#readme",
|
|
7
7
|
"license": "SEE LICENSE IN LICENSE",
|
|
8
|
-
"main": "
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"module": "esm/index.js",
|
|
10
|
+
"types": "index.d.ts",
|
|
9
11
|
"directories": {
|
|
10
12
|
"lib": "src",
|
|
11
13
|
"test": "__tests__"
|
|
12
14
|
},
|
|
13
15
|
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
16
|
+
"access": "public",
|
|
17
|
+
"directory": "dist"
|
|
15
18
|
},
|
|
16
19
|
"repository": {
|
|
17
20
|
"type": "git",
|
|
@@ -21,15 +24,21 @@
|
|
|
21
24
|
"test": "jest --passWithNoTests",
|
|
22
25
|
"test:watch": "jest --watch",
|
|
23
26
|
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",
|
|
24
|
-
"
|
|
25
|
-
"
|
|
27
|
+
"clean": "makage clean",
|
|
28
|
+
"prepack": "npm run build",
|
|
29
|
+
"build": "makage build",
|
|
30
|
+
"build:dev": "makage build --dev"
|
|
26
31
|
},
|
|
27
32
|
"bugs": {
|
|
28
33
|
"url": "https://github.com/constructive-io/jobs/issues"
|
|
29
34
|
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"makage": "^0.1.10"
|
|
37
|
+
},
|
|
30
38
|
"dependencies": {
|
|
31
|
-
"@constructive-io/job-utils": "^0.
|
|
32
|
-
"
|
|
39
|
+
"@constructive-io/job-utils": "^0.6.1",
|
|
40
|
+
"@pgpmjs/logger": "^1.4.0",
|
|
41
|
+
"pg": "8.17.1"
|
|
33
42
|
},
|
|
34
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "3ffd5718e86ea5fa9ca6e0930aeb510cf392f343"
|
|
35
44
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
# Change Log
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
-
|
|
6
|
-
## [0.3.20](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.19...@constructive-io/job-pg@0.3.20) (2026-01-09)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
9
|
-
|
|
10
|
-
## [0.3.19](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.18...@constructive-io/job-pg@0.3.19) (2026-01-07)
|
|
11
|
-
|
|
12
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
13
|
-
|
|
14
|
-
## [0.3.18](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.17...@constructive-io/job-pg@0.3.18) (2026-01-02)
|
|
15
|
-
|
|
16
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
17
|
-
|
|
18
|
-
## [0.3.17](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.16...@constructive-io/job-pg@0.3.17) (2026-01-02)
|
|
19
|
-
|
|
20
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
21
|
-
|
|
22
|
-
## [0.3.16](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.15...@constructive-io/job-pg@0.3.16) (2025-12-31)
|
|
23
|
-
|
|
24
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
25
|
-
|
|
26
|
-
## [0.3.15](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.14...@constructive-io/job-pg@0.3.15) (2025-12-31)
|
|
27
|
-
|
|
28
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
29
|
-
|
|
30
|
-
## [0.3.14](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.13...@constructive-io/job-pg@0.3.14) (2025-12-27)
|
|
31
|
-
|
|
32
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
33
|
-
|
|
34
|
-
## [0.3.13](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.12...@constructive-io/job-pg@0.3.13) (2025-12-26)
|
|
35
|
-
|
|
36
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
37
|
-
|
|
38
|
-
## [0.3.12](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.11...@constructive-io/job-pg@0.3.12) (2025-12-26)
|
|
39
|
-
|
|
40
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
41
|
-
|
|
42
|
-
## [0.3.11](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.10...@constructive-io/job-pg@0.3.11) (2025-12-24)
|
|
43
|
-
|
|
44
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
45
|
-
|
|
46
|
-
## [0.3.10](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.9...@constructive-io/job-pg@0.3.10) (2025-12-23)
|
|
47
|
-
|
|
48
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
49
|
-
|
|
50
|
-
## [0.3.9](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.8...@constructive-io/job-pg@0.3.9) (2025-12-22)
|
|
51
|
-
|
|
52
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
53
|
-
|
|
54
|
-
## [0.3.8](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.7...@constructive-io/job-pg@0.3.8) (2025-12-22)
|
|
55
|
-
|
|
56
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
57
|
-
|
|
58
|
-
## [0.3.7](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.6...@constructive-io/job-pg@0.3.7) (2025-12-21)
|
|
59
|
-
|
|
60
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
61
|
-
|
|
62
|
-
## [0.3.6](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.5...@constructive-io/job-pg@0.3.6) (2025-12-19)
|
|
63
|
-
|
|
64
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
65
|
-
|
|
66
|
-
## 0.3.5 (2025-12-18)
|
|
67
|
-
|
|
68
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
|
69
|
-
|
|
70
|
-
## [0.3.4](https://github.com/constructive-io/jobs/compare/@constructive-io/job-pg@0.3.3...@constructive-io/job-pg@0.3.4) (2025-12-17)
|
|
71
|
-
|
|
72
|
-
**Note:** Version bump only for package @constructive-io/job-pg
|
package/jest.config.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
preset: 'ts-jest',
|
|
4
|
-
testEnvironment: 'node',
|
|
5
|
-
transform: {
|
|
6
|
-
'^.+\\.tsx?$': [
|
|
7
|
-
'ts-jest',
|
|
8
|
-
{
|
|
9
|
-
babelConfig: false,
|
|
10
|
-
tsconfig: 'tsconfig.json',
|
|
11
|
-
},
|
|
12
|
-
],
|
|
13
|
-
},
|
|
14
|
-
transformIgnorePatterns: [`/node_modules/*`],
|
|
15
|
-
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
|
|
16
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
17
|
-
modulePathIgnorePatterns: ['dist/*'],
|
|
18
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
|
|
3
|
-
import { Pool, PoolConfig } from 'pg';
|
|
4
|
-
import { getJobPgConfig } from '@constructive-io/job-utils';
|
|
5
|
-
|
|
6
|
-
// k8s only does SIGINT
|
|
7
|
-
// other events are bad for babel-watch
|
|
8
|
-
const SYS_EVENTS = [
|
|
9
|
-
// 'SIGUSR2',
|
|
10
|
-
'SIGINT'
|
|
11
|
-
// 'SIGTERM',
|
|
12
|
-
// 'SIGPIPE',
|
|
13
|
-
// 'SIGHUP',
|
|
14
|
-
// 'SIGABRT'
|
|
15
|
-
];
|
|
16
|
-
|
|
17
|
-
function once<T extends (...args: unknown[]) => unknown>(
|
|
18
|
-
fn: T,
|
|
19
|
-
context?: unknown
|
|
20
|
-
): (...args: Parameters<T>) => ReturnType<T> | undefined {
|
|
21
|
-
let called = false;
|
|
22
|
-
let result: ReturnType<T> | undefined;
|
|
23
|
-
|
|
24
|
-
return function (this: unknown, ...args: Parameters<T>) {
|
|
25
|
-
if (!called && fn) {
|
|
26
|
-
// context is just forwarded through, it is not inspected
|
|
27
|
-
result = fn.apply((context ?? this) as never, args) as ReturnType<T>;
|
|
28
|
-
called = true;
|
|
29
|
-
}
|
|
30
|
-
return result;
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const pgPoolConfig: PoolConfig = getJobPgConfig() as PoolConfig;
|
|
35
|
-
|
|
36
|
-
const end = (pool: Pool): void => {
|
|
37
|
-
try {
|
|
38
|
-
// Pool has internal state flags, but they are not part of the public type
|
|
39
|
-
const state = pool as unknown as {
|
|
40
|
-
ended?: boolean;
|
|
41
|
-
ending?: boolean;
|
|
42
|
-
};
|
|
43
|
-
if (state.ended || state.ending) {
|
|
44
|
-
console.error(
|
|
45
|
-
'DO NOT CLOSE pool, why are you trying to call end() when already ended?'
|
|
46
|
-
);
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
void pool.end();
|
|
50
|
-
console.log('successfully closed pool.');
|
|
51
|
-
} catch (e) {
|
|
52
|
-
process.stderr.write(String(e));
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
// Callbacks registered for pool close events can accept arbitrary arguments
|
|
57
|
-
// (we forward whatever was passed to `onClose`).
|
|
58
|
-
type PoolCloseCallback = (...args: any[]) => Promise<void> | void;
|
|
59
|
-
|
|
60
|
-
class PoolManager {
|
|
61
|
-
private pgPool: Pool;
|
|
62
|
-
private callbacks: Array<[PoolCloseCallback, any, any[]]>;
|
|
63
|
-
private _closed: boolean;
|
|
64
|
-
|
|
65
|
-
constructor({ pgPool = new Pool(pgPoolConfig) }: { pgPool?: Pool } = {}) {
|
|
66
|
-
this.pgPool = pgPool;
|
|
67
|
-
this.callbacks = [];
|
|
68
|
-
this._closed = false;
|
|
69
|
-
|
|
70
|
-
const closeOnce = once(async () => {
|
|
71
|
-
console.log('closing pg pool manager...');
|
|
72
|
-
await this.close();
|
|
73
|
-
}, this);
|
|
74
|
-
|
|
75
|
-
SYS_EVENTS.forEach((event) => {
|
|
76
|
-
process.on(event, closeOnce);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
onClose(fn: PoolCloseCallback, context?: any, args: any[] = []): void {
|
|
81
|
-
this.callbacks.push([fn, context, args]);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
getPool(): Pool {
|
|
85
|
-
return this.pgPool;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async close(): Promise<void> {
|
|
89
|
-
if (this._closed) return;
|
|
90
|
-
|
|
91
|
-
for (const [fn, context, args] of this.callbacks) {
|
|
92
|
-
console.log('closing fn', fn.name);
|
|
93
|
-
await fn.apply(context, args);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
end(this.pgPool);
|
|
97
|
-
this._closed = true;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const mngr = new PoolManager();
|
|
102
|
-
|
|
103
|
-
export default mngr;
|
package/tsconfig.esm.json
DELETED
package/tsconfig.json
DELETED
|
File without changes
|