@devopsplaybook.io/common-utils 1.1.0 → 1.2.0-beta.7.84f3f67
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 +11 -11
- package/dist/src/PostgresDbUtils.d.ts +2 -1
- package/dist/src/PostgresDbUtils.js +7 -1
- package/package.json +1 -1
- package/src/PostgresDbUtils.ts +14 -19
package/README.md
CHANGED
|
@@ -206,17 +206,17 @@ await AuthDb.transaction(span, async (client) => {
|
|
|
206
206
|
await AuthDb.closeAll();
|
|
207
207
|
```
|
|
208
208
|
|
|
209
|
-
| Method
|
|
210
|
-
|
|
|
211
|
-
| `new PostgresSchemaDbUtils(schemaName)`
|
|
212
|
-
| `initOTel(tracer, logger)`
|
|
213
|
-
| `initSchema(context, config, sqlDir)`
|
|
214
|
-
| `initRuntimePool(config)`
|
|
215
|
-
| `execSQL(context, sql, params?, useSchemaPool?)`
|
|
216
|
-
| `execSQLFile(context, filename, useSchemaPool?)`
|
|
217
|
-
| `querySQL(context, sql, params?, useSchemaPool?)`
|
|
218
|
-
| `transaction(context, callback, useSchemaPool?)`
|
|
219
|
-
| `closeAll()`
|
|
209
|
+
| Method | Description |
|
|
210
|
+
| ------------------------------------------------- | --------------------------------------------------- |
|
|
211
|
+
| `new PostgresSchemaDbUtils(schemaName)` | Create instance for a specific schema |
|
|
212
|
+
| `initOTel(tracer, logger)` | Inject OTel instances (shared across all instances) |
|
|
213
|
+
| `initSchema(context, config, sqlDir)` | Create schema pool and run migrations |
|
|
214
|
+
| `initRuntimePool(config)` | Create shared runtime pool |
|
|
215
|
+
| `execSQL(context, sql, params?, useSchemaPool?)` | Execute write, returns `Promise<number>` |
|
|
216
|
+
| `execSQLFile(context, filename, useSchemaPool?)` | Execute an entire SQL file |
|
|
217
|
+
| `querySQL(context, sql, params?, useSchemaPool?)` | Execute read, returns `Promise<any[]>` |
|
|
218
|
+
| `transaction(context, callback, useSchemaPool?)` | Run callback inside a transaction |
|
|
219
|
+
| `closeAll()` | Close all pools managed by this instance |
|
|
220
220
|
|
|
221
221
|
`useSchemaPool` (default `true`) selects between the schema-specific pool (migrations) and the shared runtime pool (application queries).
|
|
222
222
|
|
|
@@ -24,8 +24,9 @@ export declare class PostgresSchemaDbUtils {
|
|
|
24
24
|
private schemaPool;
|
|
25
25
|
private runtimePool;
|
|
26
26
|
private readonly schemaName;
|
|
27
|
-
private
|
|
27
|
+
private _moduleLogger;
|
|
28
28
|
constructor(schemaName: string);
|
|
29
|
+
private get moduleLogger();
|
|
29
30
|
/**
|
|
30
31
|
* Create the schema-specific pool, ensure the schema exists, and apply
|
|
31
32
|
* any pending migration files from `sqlDir`.
|
|
@@ -68,8 +68,14 @@ class PostgresSchemaDbUtils {
|
|
|
68
68
|
constructor(schemaName) {
|
|
69
69
|
this.schemaPool = null;
|
|
70
70
|
this.runtimePool = null;
|
|
71
|
+
this._moduleLogger = null;
|
|
71
72
|
this.schemaName = schemaName;
|
|
72
|
-
|
|
73
|
+
}
|
|
74
|
+
get moduleLogger() {
|
|
75
|
+
if (!this._moduleLogger) {
|
|
76
|
+
this._moduleLogger = standardLogger.createModuleLogger(`PostgresSchemaDbUtils[${this.schemaName}]`);
|
|
77
|
+
}
|
|
78
|
+
return this._moduleLogger;
|
|
73
79
|
}
|
|
74
80
|
/**
|
|
75
81
|
* Create the schema-specific pool, ensure the schema exists, and apply
|
package/package.json
CHANGED
package/src/PostgresDbUtils.ts
CHANGED
|
@@ -45,13 +45,19 @@ export class PostgresSchemaDbUtils {
|
|
|
45
45
|
private schemaPool: Pool | null = null;
|
|
46
46
|
private runtimePool: Pool | null = null;
|
|
47
47
|
private readonly schemaName: string;
|
|
48
|
-
private
|
|
48
|
+
private _moduleLogger: ModuleLogger | null = null;
|
|
49
49
|
|
|
50
50
|
constructor(schemaName: string) {
|
|
51
51
|
this.schemaName = schemaName;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private get moduleLogger(): ModuleLogger {
|
|
55
|
+
if (!this._moduleLogger) {
|
|
56
|
+
this._moduleLogger = standardLogger.createModuleLogger(
|
|
57
|
+
`PostgresSchemaDbUtils[${this.schemaName}]`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return this._moduleLogger;
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
/**
|
|
@@ -93,10 +99,7 @@ export class PostgresSchemaDbUtils {
|
|
|
93
99
|
span,
|
|
94
100
|
`CREATE SCHEMA IF NOT EXISTS ${this.schemaName};`,
|
|
95
101
|
);
|
|
96
|
-
await this.execSQLForSchema(
|
|
97
|
-
span,
|
|
98
|
-
`SET search_path TO ${this.schemaName};`,
|
|
99
|
-
);
|
|
102
|
+
await this.execSQLForSchema(span, `SET search_path TO ${this.schemaName};`);
|
|
100
103
|
|
|
101
104
|
// Run init SQL files
|
|
102
105
|
await this.execSQLFileForSchema(span, `${sqlDir}/init-0000.sql`);
|
|
@@ -108,9 +111,7 @@ export class PostgresSchemaDbUtils {
|
|
|
108
111
|
span,
|
|
109
112
|
"SELECT MAX(value) as version FROM metadata WHERE type='db_version'",
|
|
110
113
|
);
|
|
111
|
-
if (
|
|
112
|
-
(dbVersionQuery[0] as Record<string, unknown>).version
|
|
113
|
-
) {
|
|
114
|
+
if ((dbVersionQuery[0] as Record<string, unknown>).version) {
|
|
114
115
|
dbVersionApplied = Number(
|
|
115
116
|
(dbVersionQuery[0] as Record<string, unknown>).version,
|
|
116
117
|
);
|
|
@@ -223,10 +224,7 @@ export class PostgresSchemaDbUtils {
|
|
|
223
224
|
filename: string,
|
|
224
225
|
useSchemaPool = false,
|
|
225
226
|
): Promise<void> {
|
|
226
|
-
const span = tracer.startSpan(
|
|
227
|
-
"PostgresSchemaDbUtilsExecSQLFile",
|
|
228
|
-
context,
|
|
229
|
-
);
|
|
227
|
+
const span = tracer.startSpan("PostgresSchemaDbUtilsExecSQLFile", context);
|
|
230
228
|
const sql = (await fs.readFile(filename)).toString();
|
|
231
229
|
const pool = useSchemaPool ? this.schemaPool : this.runtimePool;
|
|
232
230
|
|
|
@@ -306,10 +304,7 @@ export class PostgresSchemaDbUtils {
|
|
|
306
304
|
callback: (client: any) => Promise<void>,
|
|
307
305
|
useSchemaPool = false,
|
|
308
306
|
): Promise<void> {
|
|
309
|
-
const span = tracer.startSpan(
|
|
310
|
-
"PostgresSchemaDbUtilsTransaction",
|
|
311
|
-
context,
|
|
312
|
-
);
|
|
307
|
+
const span = tracer.startSpan("PostgresSchemaDbUtilsTransaction", context);
|
|
313
308
|
const pool = useSchemaPool ? this.schemaPool : this.runtimePool;
|
|
314
309
|
|
|
315
310
|
if (!pool) {
|