@constructive-io/job-pg 0.3.5

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/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
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.5 (2025-12-18)
7
+
8
+ **Note:** Version bump only for package @constructive-io/job-pg
9
+
10
+ ## [0.3.4](https://github.com/constructive-io/jobs/compare/@launchql/job-pg@0.3.3...@launchql/job-pg@0.3.4) (2025-12-17)
11
+
12
+ **Note:** Version bump only for package @launchql/job-pg
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Constructive <developers@constructive.io>
5
+ Copyright (c) 2020-present, Interweb, Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # job-pg
package/dist/env.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ declare const _default: Readonly<{
2
+ PGUSER: string;
3
+ PGHOST: string;
4
+ PGPASSWORD: string;
5
+ PGPORT: number;
6
+ PGDATABASE: string;
7
+ }> & import("envalid").CleanEnv & {
8
+ readonly [varName: string]: string;
9
+ };
10
+ export default _default;
package/dist/env.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const envalid_1 = require("envalid");
4
+ exports.default = (0, envalid_1.cleanEnv)(process.env, {
5
+ PGUSER: (0, envalid_1.str)({ default: 'postgres' }),
6
+ PGHOST: (0, envalid_1.str)({ default: 'localhost' }),
7
+ PGPASSWORD: (0, envalid_1.str)({ default: 'password' }),
8
+ PGPORT: (0, envalid_1.port)({ default: 5432 }),
9
+ PGDATABASE: (0, envalid_1.str)({ default: 'jobs' })
10
+ }, { dotEnvPath: null });
@@ -0,0 +1,15 @@
1
+ import { Pool } from 'pg';
2
+ type PoolCloseCallback = (...args: any[]) => Promise<void> | void;
3
+ declare class PoolManager {
4
+ private pgPool;
5
+ private callbacks;
6
+ private _closed;
7
+ constructor({ pgPool }?: {
8
+ pgPool?: Pool;
9
+ });
10
+ onClose(fn: PoolCloseCallback, context?: any, args?: any[]): void;
11
+ getPool(): Pool;
12
+ close(): Promise<void>;
13
+ }
14
+ declare const mngr: PoolManager;
15
+ export default mngr;
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ /* eslint-disable no-console */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const pg_1 = require("pg");
5
+ const job_utils_1 = require("@constructive-io/job-utils");
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
+ function once(fn, context) {
17
+ let called = false;
18
+ let result;
19
+ return function (...args) {
20
+ if (!called && fn) {
21
+ // context is just forwarded through, it is not inspected
22
+ result = fn.apply((context ?? this), args);
23
+ called = true;
24
+ }
25
+ return result;
26
+ };
27
+ }
28
+ const pgPoolConfig = (0, job_utils_1.getJobPgConfig)();
29
+ const end = (pool) => {
30
+ try {
31
+ // Pool has internal state flags, but they are not part of the public type
32
+ const state = pool;
33
+ if (state.ended || state.ending) {
34
+ console.error('DO NOT CLOSE pool, why are you trying to call end() when already ended?');
35
+ return;
36
+ }
37
+ void pool.end();
38
+ console.log('successfully closed pool.');
39
+ }
40
+ catch (e) {
41
+ process.stderr.write(String(e));
42
+ }
43
+ };
44
+ class PoolManager {
45
+ pgPool;
46
+ callbacks;
47
+ _closed;
48
+ constructor({ pgPool = new pg_1.Pool(pgPoolConfig) } = {}) {
49
+ this.pgPool = pgPool;
50
+ this.callbacks = [];
51
+ this._closed = false;
52
+ const closeOnce = once(async () => {
53
+ console.log('closing pg pool manager...');
54
+ await this.close();
55
+ }, this);
56
+ SYS_EVENTS.forEach((event) => {
57
+ process.on(event, closeOnce);
58
+ });
59
+ }
60
+ onClose(fn, context, args = []) {
61
+ this.callbacks.push([fn, context, args]);
62
+ }
63
+ getPool() {
64
+ return this.pgPool;
65
+ }
66
+ async close() {
67
+ if (this._closed)
68
+ return;
69
+ for (const [fn, context, args] of this.callbacks) {
70
+ console.log('closing fn', fn.name);
71
+ await fn.apply(context, args);
72
+ }
73
+ end(this.pgPool);
74
+ this._closed = true;
75
+ }
76
+ }
77
+ const mngr = new PoolManager();
78
+ exports.default = mngr;
package/jest.config.js ADDED
@@ -0,0 +1,18 @@
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/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@constructive-io/job-pg",
3
+ "version": "0.3.5",
4
+ "description": "job pg",
5
+ "author": "Constructive <developers@constructive.io>",
6
+ "homepage": "https://github.com/constructive-io/jobs/tree/master/packages/job-pg#readme",
7
+ "license": "SEE LICENSE IN LICENSE",
8
+ "main": "dist/index.js",
9
+ "directories": {
10
+ "lib": "src",
11
+ "test": "__tests__"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/constructive-io/jobs"
19
+ },
20
+ "scripts": {
21
+ "test": "jest",
22
+ "test:watch": "jest --watch",
23
+ "test:debug": "node --inspect node_modules/.bin/jest --runInBand",
24
+ "build": "tsc -p tsconfig.json",
25
+ "build:watch": "tsc -p tsconfig.json -w"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/constructive-io/jobs/issues"
29
+ },
30
+ "dependencies": {
31
+ "@constructive-io/job-utils": "^0.5.0",
32
+ "pg": "8.16.3"
33
+ },
34
+ "gitHead": "86d74dc4fce9051df0d2b5bcc163607aba42f009"
35
+ }
package/src/index.ts ADDED
@@ -0,0 +1,103 @@
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;
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist/esm",
5
+ "module": "es2022",
6
+ "rootDir": "src/",
7
+ "declaration": false
8
+ }
9
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src/"
6
+ },
7
+ "include": ["src/**/*.ts", "../../types/**/*.d.ts"],
8
+ "exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"]
9
+ }