@stacks/api-toolkit 1.12.0 → 1.12.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/package.json +1 -1
- package/dist/fastify/cache.d.ts +0 -31
- package/dist/fastify/cache.js +0 -63
- package/dist/fastify/cache.js.map +0 -1
- package/dist/fastify/fastify.d.ts +0 -16
- package/dist/fastify/fastify.js +0 -46
- package/dist/fastify/fastify.js.map +0 -1
- package/dist/fastify/index.d.ts +0 -4
- package/dist/fastify/index.js +0 -21
- package/dist/fastify/index.js.map +0 -1
- package/dist/fastify/openapi.d.ts +0 -13
- package/dist/fastify/openapi.js +0 -23
- package/dist/fastify/openapi.js.map +0 -1
- package/dist/fastify/schemas.d.ts +0 -9
- package/dist/fastify/schemas.js +0 -16
- package/dist/fastify/schemas.js.map +0 -1
- package/dist/helpers/events.d.ts +0 -52
- package/dist/helpers/events.js +0 -93
- package/dist/helpers/events.js.map +0 -1
- package/dist/helpers/index.d.ts +0 -7
- package/dist/helpers/index.js +0 -25
- package/dist/helpers/index.js.map +0 -1
- package/dist/helpers/is-debugging.d.ts +0 -1
- package/dist/helpers/is-debugging.js +0 -15
- package/dist/helpers/is-debugging.js.map +0 -1
- package/dist/helpers/iterators.d.ts +0 -27
- package/dist/helpers/iterators.js +0 -74
- package/dist/helpers/iterators.js.map +0 -1
- package/dist/helpers/serialize-error.d.ts +0 -20
- package/dist/helpers/serialize-error.js +0 -135
- package/dist/helpers/serialize-error.js.map +0 -1
- package/dist/helpers/time.d.ts +0 -54
- package/dist/helpers/time.js +0 -121
- package/dist/helpers/time.js.map +0 -1
- package/dist/helpers/values.d.ts +0 -68
- package/dist/helpers/values.js +0 -165
- package/dist/helpers/values.js.map +0 -1
- package/dist/helpers/worker-thread-init.d.ts +0 -1
- package/dist/helpers/worker-thread-init.js +0 -67
- package/dist/helpers/worker-thread-init.js.map +0 -1
- package/dist/helpers/worker-thread-manager.d.ts +0 -53
- package/dist/helpers/worker-thread-manager.js +0 -148
- package/dist/helpers/worker-thread-manager.js.map +0 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -24
- package/dist/index.js.map +0 -1
- package/dist/logger/index.d.ts +0 -20
- package/dist/logger/index.js +0 -14
- package/dist/logger/index.js.map +0 -1
- package/dist/postgres/base-pg-store.d.ts +0 -68
- package/dist/postgres/base-pg-store.js +0 -109
- package/dist/postgres/base-pg-store.js.map +0 -1
- package/dist/postgres/connection.d.ts +0 -62
- package/dist/postgres/connection.js +0 -126
- package/dist/postgres/connection.js.map +0 -1
- package/dist/postgres/errors.d.ts +0 -5
- package/dist/postgres/errors.js +0 -71
- package/dist/postgres/errors.js.map +0 -1
- package/dist/postgres/index.d.ts +0 -5
- package/dist/postgres/index.js +0 -22
- package/dist/postgres/index.js.map +0 -1
- package/dist/postgres/migrations.d.ts +0 -47
- package/dist/postgres/migrations.js +0 -134
- package/dist/postgres/migrations.js.map +0 -1
- package/dist/postgres/types.d.ts +0 -14
- package/dist/postgres/types.js +0 -48
- package/dist/postgres/types.js.map +0 -1
- package/dist/profiler/index.d.ts +0 -2
- package/dist/profiler/index.js +0 -19
- package/dist/profiler/index.js.map +0 -1
- package/dist/profiler/inspector-util.d.ts +0 -29
- package/dist/profiler/inspector-util.js +0 -268
- package/dist/profiler/inspector-util.js.map +0 -1
- package/dist/profiler/server.d.ts +0 -6
- package/dist/profiler/server.js +0 -186
- package/dist/profiler/server.js.map +0 -1
- package/dist/server-version/index.d.ts +0 -8
- package/dist/server-version/index.js +0 -33
- package/dist/server-version/index.js.map +0 -1
- package/dist/shutdown-handler/index.d.ts +0 -17
- package/dist/shutdown-handler/index.js +0 -82
- package/dist/shutdown-handler/index.js.map +0 -1
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
export type WorkerDataInterface = {
|
|
3
|
-
workerFile: string;
|
|
4
|
-
};
|
|
5
|
-
export type WorkerReqMsg<TArgs extends unknown[]> = {
|
|
6
|
-
msgId: number;
|
|
7
|
-
req: TArgs;
|
|
8
|
-
};
|
|
9
|
-
export type WorkerRespMsg<TResp, TErr> = {
|
|
10
|
-
msgId: number;
|
|
11
|
-
} & ({
|
|
12
|
-
resp: TResp;
|
|
13
|
-
error?: null;
|
|
14
|
-
} | {
|
|
15
|
-
resp?: null;
|
|
16
|
-
error: TErr;
|
|
17
|
-
});
|
|
18
|
-
export type WorkerPoolModuleInterface<TArgs extends unknown[], TResp> = {
|
|
19
|
-
workerModule: NodeJS.Module;
|
|
20
|
-
processTask: (...args: TArgs) => Promise<TResp> | TResp;
|
|
21
|
-
} | {
|
|
22
|
-
default: {
|
|
23
|
-
workerModule: NodeJS.Module;
|
|
24
|
-
processTask: (...args: TArgs) => Promise<TResp> | TResp;
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
export declare class WorkerThreadManager<TArgs extends unknown[], TResp> {
|
|
28
|
-
private readonly workers;
|
|
29
|
-
private readonly idleWorkers;
|
|
30
|
-
private readonly jobQueue;
|
|
31
|
-
private readonly msgRequests;
|
|
32
|
-
private lastMsgId;
|
|
33
|
-
readonly workerCount: number;
|
|
34
|
-
readonly workerFile: string;
|
|
35
|
-
private readonly abortControlller;
|
|
36
|
-
readonly events: EventEmitter<{
|
|
37
|
-
workersReady: [];
|
|
38
|
-
}>;
|
|
39
|
-
get idleWorkerCount(): number;
|
|
40
|
-
get busyWorkerCount(): number;
|
|
41
|
-
get queuedJobCount(): number;
|
|
42
|
-
static init<TArgs extends unknown[], TResp>(workerModule: WorkerPoolModuleInterface<TArgs, TResp>, opts?: {
|
|
43
|
-
workerCount?: number;
|
|
44
|
-
}): Promise<WorkerThreadManager<TArgs, TResp>>;
|
|
45
|
-
constructor(workerModule: WorkerPoolModuleInterface<TArgs, TResp>, opts?: {
|
|
46
|
-
workerCount?: number;
|
|
47
|
-
});
|
|
48
|
-
exec(...args: TArgs): Promise<TResp>;
|
|
49
|
-
createWorkerPool(): void;
|
|
50
|
-
private setupWorkerHandler;
|
|
51
|
-
private assignJobs;
|
|
52
|
-
close(): Promise<void>;
|
|
53
|
-
}
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WorkerThreadManager = void 0;
|
|
4
|
-
const WorkerThreads = require("node:worker_threads");
|
|
5
|
-
const os = require("node:os");
|
|
6
|
-
const node_events_1 = require("node:events");
|
|
7
|
-
const time_1 = require("./time");
|
|
8
|
-
const serialize_error_1 = require("./serialize-error");
|
|
9
|
-
const worker_thread_init_1 = require("./worker-thread-init");
|
|
10
|
-
class WorkerThreadManager {
|
|
11
|
-
workers = new Set();
|
|
12
|
-
idleWorkers = [];
|
|
13
|
-
jobQueue = [];
|
|
14
|
-
msgRequests = new Map();
|
|
15
|
-
lastMsgId = 0;
|
|
16
|
-
workerCount;
|
|
17
|
-
workerFile;
|
|
18
|
-
abortControlller = new AbortController();
|
|
19
|
-
events = new node_events_1.EventEmitter();
|
|
20
|
-
get idleWorkerCount() {
|
|
21
|
-
return this.idleWorkers.length;
|
|
22
|
-
}
|
|
23
|
-
get busyWorkerCount() {
|
|
24
|
-
return this.workerCount - this.idleWorkers.length;
|
|
25
|
-
}
|
|
26
|
-
get queuedJobCount() {
|
|
27
|
-
return this.jobQueue.length;
|
|
28
|
-
}
|
|
29
|
-
static init(workerModule, opts = {}) {
|
|
30
|
-
const workerManager = new WorkerThreadManager(workerModule, opts);
|
|
31
|
-
return new Promise(resolve => {
|
|
32
|
-
workerManager.events.once('workersReady', () => {
|
|
33
|
-
resolve(workerManager);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
constructor(workerModule, opts = {}) {
|
|
38
|
-
if (!WorkerThreads.isMainThread) {
|
|
39
|
-
throw new Error(`${this.constructor.name} must be instantiated in the main thread`);
|
|
40
|
-
}
|
|
41
|
-
if ('default' in workerModule) {
|
|
42
|
-
this.workerFile = workerModule.default.workerModule.filename;
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
this.workerFile = workerModule.workerModule.filename;
|
|
46
|
-
}
|
|
47
|
-
this.workerCount = opts.workerCount ?? os.cpus().length;
|
|
48
|
-
this.createWorkerPool();
|
|
49
|
-
}
|
|
50
|
-
exec(...args) {
|
|
51
|
-
this.abortControlller.signal.throwIfAborted();
|
|
52
|
-
if (this.lastMsgId >= Number.MAX_SAFE_INTEGER) {
|
|
53
|
-
this.lastMsgId = 0;
|
|
54
|
-
}
|
|
55
|
-
const msgId = this.lastMsgId++;
|
|
56
|
-
const replyWaiter = (0, time_1.waiter)();
|
|
57
|
-
this.msgRequests.set(msgId, replyWaiter);
|
|
58
|
-
const reqMsg = {
|
|
59
|
-
msgId,
|
|
60
|
-
req: args,
|
|
61
|
-
};
|
|
62
|
-
this.jobQueue.push(reqMsg);
|
|
63
|
-
this.assignJobs();
|
|
64
|
-
return replyWaiter;
|
|
65
|
-
}
|
|
66
|
-
createWorkerPool() {
|
|
67
|
-
let workersReady = 0;
|
|
68
|
-
for (let i = 0; i < this.workerCount; i++) {
|
|
69
|
-
const workerData = {
|
|
70
|
-
workerFile: this.workerFile,
|
|
71
|
-
};
|
|
72
|
-
const workerOpt = {
|
|
73
|
-
workerData,
|
|
74
|
-
};
|
|
75
|
-
const hasTsSource = worker_thread_init_1.filename.endsWith('.ts') || this.workerFile.endsWith('.ts');
|
|
76
|
-
if (hasTsSource) {
|
|
77
|
-
if (process.env.NODE_ENV !== 'test') {
|
|
78
|
-
throw new Error('Worker threads are being created with ts-node outside of a test environment');
|
|
79
|
-
}
|
|
80
|
-
workerOpt.execArgv = ['-r', 'ts-node/register/transpile-only'];
|
|
81
|
-
}
|
|
82
|
-
const worker = new WorkerThreads.Worker(worker_thread_init_1.filename, workerOpt);
|
|
83
|
-
worker.unref();
|
|
84
|
-
this.workers.add(worker);
|
|
85
|
-
worker.on('error', err => {
|
|
86
|
-
console.error(`Worker error`, err);
|
|
87
|
-
});
|
|
88
|
-
worker.on('messageerror', err => {
|
|
89
|
-
console.error(`Worker message error`, err);
|
|
90
|
-
});
|
|
91
|
-
worker.once('message', (message) => {
|
|
92
|
-
if (message !== 'ready') {
|
|
93
|
-
throw new Error(`Unexpected first msg from worker thread: ${JSON.stringify(message)}`);
|
|
94
|
-
}
|
|
95
|
-
this.setupWorkerHandler(worker);
|
|
96
|
-
this.idleWorkers.push(worker);
|
|
97
|
-
this.assignJobs();
|
|
98
|
-
workersReady++;
|
|
99
|
-
if (workersReady === this.workerCount) {
|
|
100
|
-
this.events.emit('workersReady');
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
(0, node_events_1.addAbortListener)(this.abortControlller.signal, () => {
|
|
105
|
-
for (const replyWaiter of this.msgRequests.values()) {
|
|
106
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
107
|
-
replyWaiter.reject(this.abortControlller.signal.reason);
|
|
108
|
-
}
|
|
109
|
-
this.msgRequests.clear();
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
setupWorkerHandler(worker) {
|
|
113
|
-
worker.on('message', (message) => {
|
|
114
|
-
this.idleWorkers.push(worker);
|
|
115
|
-
this.assignJobs();
|
|
116
|
-
const msg = message;
|
|
117
|
-
const replyWaiter = this.msgRequests.get(msg.msgId);
|
|
118
|
-
if (replyWaiter) {
|
|
119
|
-
if (msg.error) {
|
|
120
|
-
const error = (0, serialize_error_1.isErrorLike)(msg.error) ? (0, serialize_error_1.deserializeError)(msg.error) : msg.error;
|
|
121
|
-
replyWaiter.reject(error);
|
|
122
|
-
}
|
|
123
|
-
else if (msg.resp) {
|
|
124
|
-
replyWaiter.resolve(msg.resp);
|
|
125
|
-
}
|
|
126
|
-
this.msgRequests.delete(msg.msgId);
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
console.error('Received unexpected message from worker', msg);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
assignJobs() {
|
|
134
|
-
while (this.idleWorkers.length > 0 && this.jobQueue.length > 0) {
|
|
135
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
136
|
-
const worker = this.idleWorkers.shift();
|
|
137
|
-
const job = this.jobQueue.shift();
|
|
138
|
-
worker.postMessage(job);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
async close() {
|
|
142
|
-
this.abortControlller.abort();
|
|
143
|
-
await Promise.all([...this.workers].map(worker => worker.terminate()));
|
|
144
|
-
this.workers.clear();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
exports.WorkerThreadManager = WorkerThreadManager;
|
|
148
|
-
//# sourceMappingURL=worker-thread-manager.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"worker-thread-manager.js","sourceRoot":"","sources":["../../src/helpers/worker-thread-manager.ts"],"names":[],"mappings":";;;AAAA,qDAAqD;AACrD,8BAA8B;AAC9B,6CAA6D;AAC7D,iCAAwC;AACxC,uDAAkE;AAClE,6DAA4E;AAoC5E,MAAa,mBAAmB;IACb,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;IAC1C,WAAW,GAA2B,EAAE,CAAC;IAEzC,QAAQ,GAA0B,EAAE,CAAC;IACrC,WAAW,GAA+B,IAAI,GAAG,EAAE,CAAC;IAC7D,SAAS,GAAG,CAAC,CAAC;IAEb,WAAW,CAAS;IACpB,UAAU,CAAS;IAEX,gBAAgB,GAAG,IAAI,eAAe,EAAE,CAAC;IAEjD,MAAM,GAAG,IAAI,0BAAY,EAE9B,CAAC;IAEL,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;IACjC,CAAC;IAED,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;IACpD,CAAC;IAED,IAAI,cAAc;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;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,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;QACvE,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;CACF;AA/JD,kDA+JC"}
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./fastify"), exports);
|
|
18
|
-
__exportStar(require("./helpers"), exports);
|
|
19
|
-
__exportStar(require("./logger"), exports);
|
|
20
|
-
__exportStar(require("./postgres"), exports);
|
|
21
|
-
__exportStar(require("./profiler"), exports);
|
|
22
|
-
__exportStar(require("./server-version"), exports);
|
|
23
|
-
__exportStar(require("./shutdown-handler"), exports);
|
|
24
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,4CAA0B;AAC1B,2CAAyB;AACzB,6CAA2B;AAC3B,6CAA2B;AAC3B,mDAAiC;AACjC,qDAAmC"}
|
package/dist/logger/index.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export declare const PINO_LOGGER_CONFIG: {
|
|
2
|
-
name: string;
|
|
3
|
-
level: string;
|
|
4
|
-
timestamp: () => string;
|
|
5
|
-
formatters: {
|
|
6
|
-
level: (label: string, number: number) => {
|
|
7
|
-
level: string;
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
export declare const logger: import("pino").Logger<{
|
|
12
|
-
name: string;
|
|
13
|
-
level: string;
|
|
14
|
-
timestamp: () => string;
|
|
15
|
-
formatters: {
|
|
16
|
-
level: (label: string, number: number) => {
|
|
17
|
-
level: string;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
}>;
|
package/dist/logger/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.logger = exports.PINO_LOGGER_CONFIG = void 0;
|
|
4
|
-
const pino_1 = require("pino");
|
|
5
|
-
exports.PINO_LOGGER_CONFIG = {
|
|
6
|
-
name: process.env.APPLICATION_NAME ?? 'api',
|
|
7
|
-
level: process.env.LOG_LEVEL ?? (process.env.NODE_ENV === 'production' ? 'info' : 'debug'),
|
|
8
|
-
timestamp: pino_1.default.stdTimeFunctions.isoTime,
|
|
9
|
-
formatters: {
|
|
10
|
-
level: (label, number) => ({ level: label }),
|
|
11
|
-
},
|
|
12
|
-
};
|
|
13
|
-
exports.logger = (0, pino_1.default)(exports.PINO_LOGGER_CONFIG);
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
package/dist/logger/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":";;;AAAA,+BAAwB;AAEX,QAAA,kBAAkB,GAAG;IAChC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,KAAK;IAC3C,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;IAC1F,SAAS,EAAE,cAAI,CAAC,gBAAgB,CAAC,OAAO;IACxC,UAAU,EAAE;QACV,KAAK,EAAE,CAAC,KAAa,EAAE,MAAc,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;KAC7D;CACF,CAAC;AACW,QAAA,MAAM,GAAG,IAAA,cAAI,EAAC,0BAAkB,CAAC,CAAC"}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
-
import { PgSqlClient } from '.';
|
|
3
|
-
/**
|
|
4
|
-
* AsyncLocalStorage used to determine if the current async context is running inside a SQL
|
|
5
|
-
* transaction.
|
|
6
|
-
*/
|
|
7
|
-
export declare const sqlTransactionContext: AsyncLocalStorage<SqlTransactionContext>;
|
|
8
|
-
type SqlTransactionContext = {
|
|
9
|
-
[dbName: string]: PgSqlClient;
|
|
10
|
-
};
|
|
11
|
-
type UnwrapPromiseArray<T> = T extends any[] ? {
|
|
12
|
-
[k in keyof T]: T[k] extends Promise<infer R> ? R : T[k];
|
|
13
|
-
} : T;
|
|
14
|
-
/**
|
|
15
|
-
* Base class that provides access to a SQL client and SQL transaction management.
|
|
16
|
-
*/
|
|
17
|
-
export declare abstract class BasePgStore {
|
|
18
|
-
/**
|
|
19
|
-
* Getter for a SQL client. If used inside `sqlTransaction`, the scoped client within the current
|
|
20
|
-
* async context will be returned to guarantee transaction consistency.
|
|
21
|
-
*/
|
|
22
|
-
get sql(): PgSqlClient;
|
|
23
|
-
/** The raw SQL client instance. */
|
|
24
|
-
readonly _sql: PgSqlClient;
|
|
25
|
-
constructor(sql: PgSqlClient);
|
|
26
|
-
close(args?: {
|
|
27
|
-
timeout?: number;
|
|
28
|
-
}): Promise<void>;
|
|
29
|
-
/**
|
|
30
|
-
* Start a SQL transaction. If any SQL client used within the callback was already scoped inside a
|
|
31
|
-
* `BEGIN` transaction, no new transaction will be opened. This flexibility allows us to avoid
|
|
32
|
-
* repeating code while making sure we don't arrive at SQL errors such as
|
|
33
|
-
* `WARNING: there is already a transaction in progress` which may cause result inconsistencies.
|
|
34
|
-
* @param callback - Callback with a scoped SQL client
|
|
35
|
-
* @param readOnly - If a `BEGIN` transaction should be marked as `READ ONLY`
|
|
36
|
-
* @returns Transaction results
|
|
37
|
-
*/
|
|
38
|
-
sqlTransaction<T>(callback: (sql: PgSqlClient) => T | Promise<T>, readOnly?: boolean): Promise<UnwrapPromiseArray<T>>;
|
|
39
|
-
/**
|
|
40
|
-
* Start a SQL write transaction. See `sqlTransaction`.
|
|
41
|
-
* @param callback - Callback with a scoped SQL client
|
|
42
|
-
* @returns Transaction results
|
|
43
|
-
*/
|
|
44
|
-
sqlWriteTransaction<T>(callback: (sql: PgSqlClient) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;
|
|
45
|
-
/**
|
|
46
|
-
* Refreshes a materialized view concurrently depending on the current environment.
|
|
47
|
-
* @param viewName - Materialized view name
|
|
48
|
-
*/
|
|
49
|
-
refreshMaterializedView(viewName: string): Promise<void>;
|
|
50
|
-
/**
|
|
51
|
-
* Checks if the database connection is alive.
|
|
52
|
-
* @returns True if connected, false otherwise.
|
|
53
|
-
*/
|
|
54
|
-
isConnected(): Promise<boolean>;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Base module that extends PgStore functionality and allows organizing queries in separate files.
|
|
58
|
-
*/
|
|
59
|
-
export declare abstract class BasePgStoreModule {
|
|
60
|
-
private readonly parent;
|
|
61
|
-
constructor(db: BasePgStore);
|
|
62
|
-
protected get sql(): PgSqlClient;
|
|
63
|
-
sqlTransaction<T>(callback: (sql: PgSqlClient) => T | Promise<T>, readOnly?: boolean): Promise<UnwrapPromiseArray<T>>;
|
|
64
|
-
sqlWriteTransaction<T>(callback: (sql: PgSqlClient) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;
|
|
65
|
-
refreshMaterializedView(viewName: string): Promise<void>;
|
|
66
|
-
isConnected(): Promise<boolean>;
|
|
67
|
-
}
|
|
68
|
-
export {};
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BasePgStoreModule = exports.BasePgStore = exports.sqlTransactionContext = void 0;
|
|
4
|
-
const async_hooks_1 = require("async_hooks");
|
|
5
|
-
const values_1 = require("../helpers/values");
|
|
6
|
-
/**
|
|
7
|
-
* AsyncLocalStorage used to determine if the current async context is running inside a SQL
|
|
8
|
-
* transaction.
|
|
9
|
-
*/
|
|
10
|
-
exports.sqlTransactionContext = new async_hooks_1.AsyncLocalStorage();
|
|
11
|
-
/**
|
|
12
|
-
* Base class that provides access to a SQL client and SQL transaction management.
|
|
13
|
-
*/
|
|
14
|
-
class BasePgStore {
|
|
15
|
-
/**
|
|
16
|
-
* Getter for a SQL client. If used inside `sqlTransaction`, the scoped client within the current
|
|
17
|
-
* async context will be returned to guarantee transaction consistency.
|
|
18
|
-
*/
|
|
19
|
-
get sql() {
|
|
20
|
-
const dbName = this._sql.options.database?.toString() ?? 'default';
|
|
21
|
-
const sqlContext = exports.sqlTransactionContext.getStore();
|
|
22
|
-
return sqlContext ? sqlContext[dbName] : this._sql;
|
|
23
|
-
}
|
|
24
|
-
/** The raw SQL client instance. */
|
|
25
|
-
_sql;
|
|
26
|
-
constructor(sql) {
|
|
27
|
-
this._sql = sql;
|
|
28
|
-
}
|
|
29
|
-
async close(args) {
|
|
30
|
-
await this._sql.end({ timeout: args?.timeout });
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Start a SQL transaction. If any SQL client used within the callback was already scoped inside a
|
|
34
|
-
* `BEGIN` transaction, no new transaction will be opened. This flexibility allows us to avoid
|
|
35
|
-
* repeating code while making sure we don't arrive at SQL errors such as
|
|
36
|
-
* `WARNING: there is already a transaction in progress` which may cause result inconsistencies.
|
|
37
|
-
* @param callback - Callback with a scoped SQL client
|
|
38
|
-
* @param readOnly - If a `BEGIN` transaction should be marked as `READ ONLY`
|
|
39
|
-
* @returns Transaction results
|
|
40
|
-
*/
|
|
41
|
-
async sqlTransaction(callback, readOnly = true) {
|
|
42
|
-
// Do we have a scoped client already? Use it directly. Key is the database name.
|
|
43
|
-
const dbName = this._sql.options.database?.toString() ?? 'default';
|
|
44
|
-
const sql = exports.sqlTransactionContext.getStore()?.[dbName];
|
|
45
|
-
if (sql) {
|
|
46
|
-
return callback(sql);
|
|
47
|
-
}
|
|
48
|
-
// Otherwise, start a transaction and store the scoped connection in the current async context.
|
|
49
|
-
return this._sql.begin(readOnly ? 'read only' : 'read write', sql => {
|
|
50
|
-
const currentStore = exports.sqlTransactionContext.getStore() ?? {};
|
|
51
|
-
return exports.sqlTransactionContext.run({ ...currentStore, [dbName]: sql }, () => callback(sql));
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Start a SQL write transaction. See `sqlTransaction`.
|
|
56
|
-
* @param callback - Callback with a scoped SQL client
|
|
57
|
-
* @returns Transaction results
|
|
58
|
-
*/
|
|
59
|
-
async sqlWriteTransaction(callback) {
|
|
60
|
-
return this.sqlTransaction(callback, false);
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Refreshes a materialized view concurrently depending on the current environment.
|
|
64
|
-
* @param viewName - Materialized view name
|
|
65
|
-
*/
|
|
66
|
-
async refreshMaterializedView(viewName) {
|
|
67
|
-
await this.sql `REFRESH MATERIALIZED VIEW ${values_1.isProdEnv ? this.sql `CONCURRENTLY` : this.sql ``} ${this.sql(viewName)}`;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Checks if the database connection is alive.
|
|
71
|
-
* @returns True if connected, false otherwise.
|
|
72
|
-
*/
|
|
73
|
-
async isConnected() {
|
|
74
|
-
try {
|
|
75
|
-
await this.sql `SELECT NOW()`;
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
catch (error) {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
exports.BasePgStore = BasePgStore;
|
|
84
|
-
/**
|
|
85
|
-
* Base module that extends PgStore functionality and allows organizing queries in separate files.
|
|
86
|
-
*/
|
|
87
|
-
class BasePgStoreModule {
|
|
88
|
-
parent;
|
|
89
|
-
constructor(db) {
|
|
90
|
-
this.parent = db;
|
|
91
|
-
}
|
|
92
|
-
get sql() {
|
|
93
|
-
return this.parent.sql;
|
|
94
|
-
}
|
|
95
|
-
async sqlTransaction(callback, readOnly = true) {
|
|
96
|
-
return this.parent.sqlTransaction(callback, readOnly);
|
|
97
|
-
}
|
|
98
|
-
async sqlWriteTransaction(callback) {
|
|
99
|
-
return this.sqlTransaction(callback, false);
|
|
100
|
-
}
|
|
101
|
-
async refreshMaterializedView(viewName) {
|
|
102
|
-
return this.parent.refreshMaterializedView(viewName);
|
|
103
|
-
}
|
|
104
|
-
async isConnected() {
|
|
105
|
-
return this.parent.isConnected();
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
exports.BasePgStoreModule = BasePgStoreModule;
|
|
109
|
-
//# sourceMappingURL=base-pg-store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base-pg-store.js","sourceRoot":"","sources":["../../src/postgres/base-pg-store.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;AAEhD,8CAA8C;AAE9C;;;GAGG;AACU,QAAA,qBAAqB,GAAG,IAAI,+BAAiB,EAAyB,C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,CAAC,CAAC;IAC9C,CAAC;IACD,KAAK,CAAC,uBAAuB,CAAC,QAAgB;QAC5C,OAAO,IAAI,CAAC,MAAM,CAAC,uBAAuB,CAAC,QAAQ,CAAC,CAAC;IACvD,CAAC;IACD,KAAK,CAAC,WAAW;QACf,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;IACnC,CAAC;CACF;AA5BD,8CA4BC"}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import * as postgres from 'postgres';
|
|
2
|
-
/** Postgres client instance */
|
|
3
|
-
export type PgSqlClient = postgres.Sql<any> | postgres.TransactionSql<any>;
|
|
4
|
-
/** Postgres pending query or query fragment */
|
|
5
|
-
export type PgSqlQuery = postgres.PendingQuery<postgres.Row[]>;
|
|
6
|
-
export type PgSslMode = 'require' | 'allow' | 'prefer' | 'verify-full' | boolean | object;
|
|
7
|
-
/** Postgres connection URI string */
|
|
8
|
-
export type PgConnectionUri = string;
|
|
9
|
-
/** Postgres connection values */
|
|
10
|
-
export type PgConnectionVars = {
|
|
11
|
-
database?: string;
|
|
12
|
-
user?: string;
|
|
13
|
-
password?: string;
|
|
14
|
-
host?: string;
|
|
15
|
-
port?: number;
|
|
16
|
-
schema?: string;
|
|
17
|
-
ssl?: PgSslMode;
|
|
18
|
-
application_name?: string;
|
|
19
|
-
};
|
|
20
|
-
/** Postgres connection arguments */
|
|
21
|
-
export type PgConnectionArgs = PgConnectionUri | PgConnectionVars;
|
|
22
|
-
/** Postgres connection options */
|
|
23
|
-
export type PgConnectionOptions = {
|
|
24
|
-
/** Time to wait before automatically closing an idle connection (s) */
|
|
25
|
-
idleTimeout?: number;
|
|
26
|
-
/** Maximum allowed duration of any statement (ms) */
|
|
27
|
-
statementTimeout?: number;
|
|
28
|
-
/** Maximum time a connection can exist (s) */
|
|
29
|
-
maxLifetime?: number;
|
|
30
|
-
/** Max number of connections */
|
|
31
|
-
poolMax?: number;
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Takes in connection arguments provided via an object or a connection string and returns them with
|
|
35
|
-
* a standardized application name format. If no connection args are provided, they are built from
|
|
36
|
-
* standard postgres ENV vars.
|
|
37
|
-
* @param args - Connection arguments
|
|
38
|
-
* @param usage - Usage string
|
|
39
|
-
* @returns PgConnectionVars
|
|
40
|
-
*/
|
|
41
|
-
export declare function standardizedConnectionArgs(args?: PgConnectionArgs, usage?: string): PgConnectionArgs;
|
|
42
|
-
/**
|
|
43
|
-
* Connects to Postgres. This function will also test the connection first to make sure all
|
|
44
|
-
* connection parameters were specified correctly.
|
|
45
|
-
* @param args - Connection options
|
|
46
|
-
* @returns configured `Pool` object
|
|
47
|
-
*/
|
|
48
|
-
export declare function connectPostgres({ usageName, connectionArgs, connectionConfig, }: {
|
|
49
|
-
usageName: string;
|
|
50
|
-
connectionArgs?: PgConnectionArgs;
|
|
51
|
-
connectionConfig?: PgConnectionOptions;
|
|
52
|
-
}): Promise<PgSqlClient>;
|
|
53
|
-
/**
|
|
54
|
-
* Creates a Postgres client based on the provided connection arguments.
|
|
55
|
-
* @param args - Connection options
|
|
56
|
-
* @returns PgSqlClient
|
|
57
|
-
*/
|
|
58
|
-
export declare function getPostgres({ usageName, connectionArgs, connectionConfig, }: {
|
|
59
|
-
usageName: string;
|
|
60
|
-
connectionArgs?: PgConnectionArgs;
|
|
61
|
-
connectionConfig?: PgConnectionOptions;
|
|
62
|
-
}): PgSqlClient;
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.standardizedConnectionArgs = standardizedConnectionArgs;
|
|
4
|
-
exports.connectPostgres = connectPostgres;
|
|
5
|
-
exports.getPostgres = getPostgres;
|
|
6
|
-
const postgres = require("postgres");
|
|
7
|
-
const logger_1 = require("../logger");
|
|
8
|
-
const errors_1 = require("./errors");
|
|
9
|
-
const time_1 = require("../helpers/time");
|
|
10
|
-
const types_1 = require("./types");
|
|
11
|
-
/**
|
|
12
|
-
* Takes in connection arguments provided via an object or a connection string and returns them with
|
|
13
|
-
* a standardized application name format. If no connection args are provided, they are built from
|
|
14
|
-
* standard postgres ENV vars.
|
|
15
|
-
* @param args - Connection arguments
|
|
16
|
-
* @param usage - Usage string
|
|
17
|
-
* @returns PgConnectionVars
|
|
18
|
-
*/
|
|
19
|
-
function standardizedConnectionArgs(args, usage) {
|
|
20
|
-
const appName = process.env.APPLICATION_NAME ?? process.env.PGAPPNAME ?? 'postgres';
|
|
21
|
-
const appUsage = usage ?? 'query';
|
|
22
|
-
if (!args) {
|
|
23
|
-
return {
|
|
24
|
-
database: process.env.PGDATABASE,
|
|
25
|
-
user: process.env.PGUSER,
|
|
26
|
-
password: process.env.PGPASSWORD,
|
|
27
|
-
host: process.env.PGHOST,
|
|
28
|
-
port: parseInt(process.env.PGPORT ?? '5432'),
|
|
29
|
-
ssl: process.env.PGSSLMODE,
|
|
30
|
-
application_name: `${appName}:${appUsage}`,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
if (typeof args === 'string') {
|
|
34
|
-
const uri = new URL(args);
|
|
35
|
-
uri.searchParams.set('application_name', `${uri.searchParams.get('application_name') ?? appName}:${appUsage}`);
|
|
36
|
-
return uri.toString();
|
|
37
|
-
}
|
|
38
|
-
return args;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Connects to Postgres. This function will also test the connection first to make sure all
|
|
42
|
-
* connection parameters were specified correctly.
|
|
43
|
-
* @param args - Connection options
|
|
44
|
-
* @returns configured `Pool` object
|
|
45
|
-
*/
|
|
46
|
-
async function connectPostgres({ usageName, connectionArgs, connectionConfig, }) {
|
|
47
|
-
const initTimer = (0, time_1.stopwatch)();
|
|
48
|
-
let connectionError;
|
|
49
|
-
let connectionOkay = false;
|
|
50
|
-
let lastElapsedLog = 0;
|
|
51
|
-
do {
|
|
52
|
-
const testSql = getPostgres({
|
|
53
|
-
usageName: `${usageName};conn-poll`,
|
|
54
|
-
connectionArgs,
|
|
55
|
-
connectionConfig,
|
|
56
|
-
});
|
|
57
|
-
try {
|
|
58
|
-
await testSql `SELECT version()`;
|
|
59
|
-
connectionOkay = true;
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
if ((0, errors_1.isPgConnectionError)(error)) {
|
|
64
|
-
const timeElapsed = initTimer.getElapsed();
|
|
65
|
-
if (timeElapsed - lastElapsedLog > 2000) {
|
|
66
|
-
lastElapsedLog = timeElapsed;
|
|
67
|
-
logger_1.logger.error(error, 'Pg connection failed, retrying..');
|
|
68
|
-
}
|
|
69
|
-
connectionError = error;
|
|
70
|
-
await (0, time_1.timeout)(100);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
logger_1.logger.error(error, 'Cannot connect to pg');
|
|
74
|
-
throw error;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
finally {
|
|
78
|
-
await testSql.end();
|
|
79
|
-
}
|
|
80
|
-
} while (initTimer.getElapsed() < Number.MAX_SAFE_INTEGER);
|
|
81
|
-
if (!connectionOkay) {
|
|
82
|
-
connectionError = connectionError ?? new Error('Error connecting to database');
|
|
83
|
-
throw connectionError;
|
|
84
|
-
}
|
|
85
|
-
const sql = getPostgres({
|
|
86
|
-
usageName: `${usageName};datastore-crud`,
|
|
87
|
-
connectionArgs,
|
|
88
|
-
connectionConfig,
|
|
89
|
-
});
|
|
90
|
-
return sql;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Creates a Postgres client based on the provided connection arguments.
|
|
94
|
-
* @param args - Connection options
|
|
95
|
-
* @returns PgSqlClient
|
|
96
|
-
*/
|
|
97
|
-
function getPostgres({ usageName, connectionArgs, connectionConfig, }) {
|
|
98
|
-
const args = standardizedConnectionArgs(connectionArgs, usageName);
|
|
99
|
-
if (typeof args === 'string') {
|
|
100
|
-
return postgres(args, {
|
|
101
|
-
idle_timeout: connectionConfig?.idleTimeout,
|
|
102
|
-
max_lifetime: connectionConfig?.maxLifetime,
|
|
103
|
-
max: connectionConfig?.poolMax,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
return postgres({
|
|
108
|
-
database: args.database,
|
|
109
|
-
user: args.user,
|
|
110
|
-
password: args.password,
|
|
111
|
-
host: args.host,
|
|
112
|
-
port: args.port,
|
|
113
|
-
ssl: args.ssl,
|
|
114
|
-
idle_timeout: connectionConfig?.idleTimeout,
|
|
115
|
-
max_lifetime: connectionConfig?.maxLifetime,
|
|
116
|
-
max: connectionConfig?.poolMax,
|
|
117
|
-
types: types_1.PG_TYPE_MAPPINGS,
|
|
118
|
-
connection: {
|
|
119
|
-
application_name: args.application_name,
|
|
120
|
-
search_path: args.schema,
|
|
121
|
-
statement_timeout: connectionConfig?.statementTimeout?.toString(),
|
|
122
|
-
},
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
//# sourceMappingURL=connection.js.map
|