@stacks/api-toolkit 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +74 -0
- package/bin/api-toolkit-git-info.js +25 -0
- package/dist/fastify/cache.d.ts +31 -0
- package/dist/fastify/cache.js +63 -0
- package/dist/fastify/cache.js.map +1 -0
- package/dist/fastify/fastify.d.ts +16 -0
- package/dist/fastify/fastify.js +46 -0
- package/dist/fastify/fastify.js.map +1 -0
- package/dist/fastify/index.d.ts +4 -0
- package/dist/fastify/index.js +21 -0
- package/dist/fastify/index.js.map +1 -0
- package/dist/fastify/openapi.d.ts +13 -0
- package/dist/fastify/openapi.js +23 -0
- package/dist/fastify/openapi.js.map +1 -0
- package/dist/fastify/schemas.d.ts +9 -0
- package/dist/fastify/schemas.js +16 -0
- package/dist/fastify/schemas.js.map +1 -0
- package/dist/helpers/events.d.ts +52 -0
- package/dist/helpers/events.js +93 -0
- package/dist/helpers/events.js.map +1 -0
- package/dist/helpers/index.d.ts +7 -0
- package/dist/helpers/index.js +25 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/helpers/is-debugging.d.ts +1 -0
- package/dist/helpers/is-debugging.js +15 -0
- package/dist/helpers/is-debugging.js.map +1 -0
- package/dist/helpers/iterators.d.ts +27 -0
- package/dist/helpers/iterators.js +74 -0
- package/dist/helpers/iterators.js.map +1 -0
- package/dist/helpers/serialize-error.d.ts +20 -0
- package/dist/helpers/serialize-error.js +135 -0
- package/dist/helpers/serialize-error.js.map +1 -0
- package/dist/helpers/time.d.ts +54 -0
- package/dist/helpers/time.js +121 -0
- package/dist/helpers/time.js.map +1 -0
- package/dist/helpers/values.d.ts +68 -0
- package/dist/helpers/values.js +165 -0
- package/dist/helpers/values.js.map +1 -0
- package/dist/helpers/worker-thread-init.d.ts +1 -0
- package/dist/helpers/worker-thread-init.js +67 -0
- package/dist/helpers/worker-thread-init.js.map +1 -0
- package/dist/helpers/worker-thread-manager.d.ts +53 -0
- package/dist/helpers/worker-thread-manager.js +148 -0
- package/dist/helpers/worker-thread-manager.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/index.d.ts +20 -0
- package/dist/logger/index.js +14 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/postgres/base-pg-store.d.ts +68 -0
- package/dist/postgres/base-pg-store.js +109 -0
- package/dist/postgres/base-pg-store.js.map +1 -0
- package/dist/postgres/connection.d.ts +62 -0
- package/dist/postgres/connection.js +126 -0
- package/dist/postgres/connection.js.map +1 -0
- package/dist/postgres/errors.d.ts +5 -0
- package/dist/postgres/errors.js +71 -0
- package/dist/postgres/errors.js.map +1 -0
- package/dist/postgres/index.d.ts +5 -0
- package/dist/postgres/index.js +22 -0
- package/dist/postgres/index.js.map +1 -0
- package/dist/postgres/migrations.d.ts +47 -0
- package/dist/postgres/migrations.js +134 -0
- package/dist/postgres/migrations.js.map +1 -0
- package/dist/postgres/types.d.ts +14 -0
- package/dist/postgres/types.js +48 -0
- package/dist/postgres/types.js.map +1 -0
- package/dist/profiler/index.d.ts +2 -0
- package/dist/profiler/index.js +19 -0
- package/dist/profiler/index.js.map +1 -0
- package/dist/profiler/inspector-util.d.ts +29 -0
- package/dist/profiler/inspector-util.js +268 -0
- package/dist/profiler/inspector-util.js.map +1 -0
- package/dist/profiler/server.d.ts +6 -0
- package/dist/profiler/server.js +186 -0
- package/dist/profiler/server.js.map +1 -0
- package/dist/server-version/index.d.ts +8 -0
- package/dist/server-version/index.js +33 -0
- package/dist/server-version/index.js.map +1 -0
- package/dist/shutdown-handler/index.d.ts +17 -0
- package/dist/shutdown-handler/index.js +82 -0
- package/dist/shutdown-handler/index.js.map +1 -0
- package/package.json +73 -0
|
@@ -0,0 +1,68 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,109 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,CAAC;AAUpF;;GAEG;AACH,MAAsB,WAAW;IAC/B;;;OAGG;IACH,IAAI,GAAG;QACL,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,SAAS,CAAC;QACnE,MAAM,UAAU,GAAG,6BAAqB,CAAC,QAAQ,EAAE,CAAC;QACpD,OAAO,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;IACrD,CAAC;IACD,mCAAmC;IAC1B,IAAI,CAAc;IAE3B,YAAY,GAAgB;QAC1B,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,IAA2B;QACrC,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IAClD,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,cAAc,CAClB,QAA8C,EAC9C,QAAQ,GAAG,IAAI;QAEf,iFAAiF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O,KAAK,EAAE,CAAC;YACf,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;CACF;AAhFD,kCAgFC;AAED;;GAEG;AACH,MAAsB,iBAAiB;IACpB,MAAM,CAAc;IAErC,YAAY,EAAe;QACzB,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACnB,CAAC;IAED,IAAc,GAAG;QACf,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,cAAc,CAClB,QAA8C,EAC9C,QAAQ,GAAG,IAAI;QAEf,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACxD,CAAC;IACD,KAAK,CAAC,mBAAmB,CACvB,QAA8C;QAE9C,OAAO,IAAI,CAAC,cAAc,CAAC,QAAQ,EAAE,KAAK,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"}
|
|
@@ -0,0 +1,62 @@
|
|
|
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;
|
|
@@ -0,0 +1,126 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/postgres/connection.ts"],"names":[],"mappings":";;AA+C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|
|
4
|
+
/**
|
|
5
|
+
* Checks if a given error from the pg lib is a connection error (i.e. the query is retryable).
|
|
6
|
+
* If true then returns a normalized error message, otherwise returns false.
|
|
7
|
+
*/
|
|
8
|
+
function isPgConnectionError(error) {
|
|
9
|
+
if (error.code === 'ECONNREFUSED') {
|
|
10
|
+
return 'Postgres connection ECONNREFUSED';
|
|
11
|
+
}
|
|
12
|
+
else if (error.code === 'ETIMEDOUT') {
|
|
13
|
+
return 'Postgres connection ETIMEDOUT';
|
|
14
|
+
}
|
|
15
|
+
else if (error.code === 'ENOTFOUND') {
|
|
16
|
+
return 'Postgres connection ENOTFOUND';
|
|
17
|
+
}
|
|
18
|
+
else if (error.code === 'ECONNRESET') {
|
|
19
|
+
return 'Postgres connection ECONNRESET';
|
|
20
|
+
}
|
|
21
|
+
else if (error.code === 'CONNECTION_CLOSED') {
|
|
22
|
+
return 'Postgres connection CONNECTION_CLOSED';
|
|
23
|
+
}
|
|
24
|
+
else if (error.code === 'CONNECTION_ENDED') {
|
|
25
|
+
return 'Postgres connection CONNECTION_ENDED';
|
|
26
|
+
}
|
|
27
|
+
else if (error.code === 'CONNECTION_DESTROYED') {
|
|
28
|
+
return 'Postgres connection CONNECTION_DESTROYED';
|
|
29
|
+
}
|
|
30
|
+
else if (error.code === 'CONNECTION_CONNECT_TIMEOUT') {
|
|
31
|
+
return 'Postgres connection CONNECTION_CONNECT_TIMEOUT';
|
|
32
|
+
}
|
|
33
|
+
else if (error.code === 'CONNECT_TIMEOUT') {
|
|
34
|
+
return 'Postgres connection CONNECT_TIMEOUT';
|
|
35
|
+
}
|
|
36
|
+
else if (error.message) {
|
|
37
|
+
const msg = error.message.toLowerCase();
|
|
38
|
+
if (msg.includes('database system is starting up')) {
|
|
39
|
+
return 'Postgres connection failed while database system is starting up';
|
|
40
|
+
}
|
|
41
|
+
else if (msg.includes('database system is shutting down')) {
|
|
42
|
+
return 'Postgres connection failed while database system is shutting down';
|
|
43
|
+
}
|
|
44
|
+
else if (msg.includes('connection terminated unexpectedly')) {
|
|
45
|
+
return 'Postgres connection terminated unexpectedly';
|
|
46
|
+
}
|
|
47
|
+
else if (msg.includes('connection terminated')) {
|
|
48
|
+
return 'Postgres connection terminated';
|
|
49
|
+
}
|
|
50
|
+
else if (msg.includes('connection error')) {
|
|
51
|
+
return 'Postgres client has encountered a connection error and is not queryable';
|
|
52
|
+
}
|
|
53
|
+
else if (msg.includes('terminating connection due to unexpected postmaster exit')) {
|
|
54
|
+
return 'Postgres connection terminating due to unexpected postmaster exit';
|
|
55
|
+
}
|
|
56
|
+
else if (msg.includes('getaddrinfo eai_again')) {
|
|
57
|
+
return 'Postgres connection failed due to a DNS lookup error';
|
|
58
|
+
}
|
|
59
|
+
else if (msg.includes('terminating connection due to administrator command')) {
|
|
60
|
+
return 'Postgres connection closed due to administrator command';
|
|
61
|
+
}
|
|
62
|
+
else if (msg.includes('password authentication failed')) {
|
|
63
|
+
return 'Postgres authentication failed';
|
|
64
|
+
}
|
|
65
|
+
else if (msg.includes('database system is not yet accepting connections')) {
|
|
66
|
+
return 'Postgres not yet accepting connections';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/postgres/errors.ts"],"names":[],"mappings":";;AAIA,kDA4CC;AAhDD;;;GAGG;AACH,SAAgB,mBAAmB,CAAC,KAAU;IAC5C,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;QAClC,OAAO,kCAAkC,CAAC;IAC5C,CAAC;SAAM,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QACtC,OAAO,+BAA+B,CAAC;IACzC,CAAC;SAAM,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QACtC,OAAO,+BAA+B,CAAC;IACzC,CAAC;SAAM,IAAI,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0D,CAAC,EAAE,CAAC;YACpF,OAAO,mEAAmE,CAAC;QAC7E,CAAC;aAAM,IAAI,GAAG,CAAC,QAAQ,CAAC,uBAAuB,CAAC,EAAE,CAAC;YACjD,OAAO,sDAAsD,CAAC;QAChE,CAAC;aAAM,IAAI,GAAG,CAAC,QAAQ,CAAC,qDAAqD,CAAC,EAAE,CAAC;YAC/E,OAAO,yDAAyD,CAAC;QACnE,CAAC;aAAM,IAAI,GAAG,CAAC,QAAQ,CAAC,gCAAgC,CAAC,EAAE,CAAC;YAC1D,OAAO,gCAAgC,CAAC;QAC1C,CAAC;aAAM,IAAI,GAAG,CAAC,QAAQ,CAAC,kDAAkD,CAAC,EAAE,CAAC;YAC5E,OAAO,wCAAwC,CAAC;QAClD,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
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("./base-pg-store"), exports);
|
|
18
|
+
__exportStar(require("./connection"), exports);
|
|
19
|
+
__exportStar(require("./errors"), exports);
|
|
20
|
+
__exportStar(require("./migrations"), exports);
|
|
21
|
+
__exportStar(require("./types"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/postgres/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,+CAA6B;AAC7B,2CAAyB;AACzB,+CAA6B;AAC7B,0CAAwB"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Logger as PgMigrateLogger, MigrationDirection } from 'node-pg-migrate/dist/types';
|
|
2
|
+
import { PgConnectionArgs } from './connection';
|
|
3
|
+
export interface MigrationOptions {
|
|
4
|
+
/** Bypass the NODE_ENV check when performing a "down" migration which irreversibly drops data. */
|
|
5
|
+
dangerousAllowDataLoss?: boolean;
|
|
6
|
+
/** Log all applied migrations */
|
|
7
|
+
logMigrations?: boolean;
|
|
8
|
+
/** Name of the table used for migrations. Defaults to `pgmigrations`. */
|
|
9
|
+
migrationsTable?: string;
|
|
10
|
+
/** Custom logging configuration */
|
|
11
|
+
logger?: PgMigrateLogger;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Run migrations in one direction.
|
|
15
|
+
* @param dir - Migrations directory
|
|
16
|
+
* @param direction - Migration direction (`'down'` or `'up'`)
|
|
17
|
+
* @param connectionArgs - Postgres connection args
|
|
18
|
+
* @param opts - Migration options
|
|
19
|
+
*/
|
|
20
|
+
export declare function runMigrations(dir: string, direction: MigrationDirection, connectionArgs?: PgConnectionArgs, opts?: MigrationOptions): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Cycle migrations down and up.
|
|
23
|
+
* @param dir - Migrations directory
|
|
24
|
+
* @param connectionArgs - Postgres connection args
|
|
25
|
+
* @param opts - Migration options
|
|
26
|
+
*/
|
|
27
|
+
export declare function cycleMigrations(dir: string, connectionArgs?: PgConnectionArgs, opts?: MigrationOptions & {
|
|
28
|
+
/** Validates if the database was cleared completely after all `down` migrations are done */
|
|
29
|
+
checkForEmptyData?: boolean;
|
|
30
|
+
}): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Check the `pg_class` table for any data structures contained in the database. We will consider
|
|
33
|
+
* any and all results here as "data" contained in the DB, since anything that is not a completely
|
|
34
|
+
* empty DB could lead to strange errors when running the API. See:
|
|
35
|
+
* https://www.postgresql.org/docs/current/catalog-pg-class.html
|
|
36
|
+
* @returns `boolean` if the DB has data
|
|
37
|
+
*/
|
|
38
|
+
export declare function databaseHasData(connectionArgs?: PgConnectionArgs, opts?: {
|
|
39
|
+
ignoreMigrationTables?: boolean;
|
|
40
|
+
migrationsTable?: string;
|
|
41
|
+
}): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Drops all tables from the Postgres DB. DANGEROUS!!!
|
|
44
|
+
*/
|
|
45
|
+
export declare function dangerousDropAllTables(connectionArgs?: PgConnectionArgs, opts?: {
|
|
46
|
+
acknowledgePotentialCatastrophicConsequences?: 'yes';
|
|
47
|
+
}): Promise<void>;
|