pqb 0.64.0 → 0.65.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/dist/index.d.ts +9015 -9539
- package/dist/index.js +13487 -15711
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +13462 -15709
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.ts +2 -1
- package/dist/internal.js +668 -335
- package/dist/internal.mjs +2 -2
- package/dist/node-postgres.d.ts +12 -9794
- package/dist/node-postgres.js +139 -594
- package/dist/node-postgres.js.map +1 -1
- package/dist/node-postgres.mjs +117 -591
- package/dist/node-postgres.mjs.map +1 -1
- package/dist/postgres-js.d.ts +10 -9793
- package/dist/postgres-js.js +150 -549
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs +129 -547
- package/dist/postgres-js.mjs.map +1 -1
- package/dist/public.d.ts +2 -1
- package/dist/public.js +34 -23
- package/dist/public.mjs +2 -2
- package/package.json +35 -15
- package/dist/internal.js.map +0 -1
- package/dist/internal.mjs.map +0 -1
- package/dist/public.js.map +0 -1
- package/dist/public.mjs.map +0 -1
package/dist/node-postgres.js
CHANGED
|
@@ -1,607 +1,152 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
return transactionArgs;
|
|
19
|
-
};
|
|
20
|
-
const mergeLocals = (locals, options) => options?.locals ? { ...locals, ...options.locals } : locals;
|
|
21
|
-
const getSetLocalsSql = (options) => {
|
|
22
|
-
if (!options?.locals) return;
|
|
23
|
-
return Object.entries(options.locals).map(([key, value]) => `SET LOCAL ${key}=${value}`).join("; ");
|
|
24
|
-
};
|
|
25
|
-
const getResetLocalsSql = (parentLocals, options) => {
|
|
26
|
-
if (!options?.locals) return;
|
|
27
|
-
return Object.entries(options.locals).reduce((acc, [key, value]) => {
|
|
28
|
-
if (parentLocals[key] !== value) {
|
|
29
|
-
acc.push(`SET LOCAL ${key}=${parentLocals[key]}`);
|
|
30
|
-
}
|
|
31
|
-
return acc;
|
|
32
|
-
}, []).join("; ");
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const quoteRoleIdentifier = (role) => {
|
|
36
|
-
return `"${role.replace(/"/g, '""')}"`;
|
|
37
|
-
};
|
|
38
|
-
const buildConfigRestoreExpression = (key, value) => {
|
|
39
|
-
const escapedKey = key.replace(/'/g, "''");
|
|
40
|
-
if (value === null || value === void 0) {
|
|
41
|
-
value = "";
|
|
42
|
-
}
|
|
43
|
-
return `set_config('${escapedKey}', '${value.replace(/'/g, "''")}', false) as "${key}"`;
|
|
44
|
-
};
|
|
45
|
-
const sqlSessionContextComputeSetup = (desired) => {
|
|
46
|
-
if (!desired) return void 0;
|
|
47
|
-
const role = desired.role;
|
|
48
|
-
const hasRole = role !== void 0;
|
|
49
|
-
const { setConfig } = desired;
|
|
50
|
-
const configKeys = setConfig && Object.keys(setConfig);
|
|
51
|
-
const hasConfig = configKeys && configKeys.length > 0;
|
|
52
|
-
if (!hasRole && !hasConfig) return void 0;
|
|
53
|
-
const result = {};
|
|
54
|
-
if (hasRole) {
|
|
55
|
-
result.roleSetupSql = `SET ROLE ${quoteRoleIdentifier(role)}`;
|
|
56
|
-
result.captureRoleSql = "SELECT current_user";
|
|
57
|
-
}
|
|
58
|
-
if (hasConfig && setConfig) {
|
|
59
|
-
result.captureConfigValues = configKeys;
|
|
60
|
-
const captureColumns = configKeys.map((key, i) => `current_setting($${i + 1}, true) as "${key}"`).join(", ");
|
|
61
|
-
result.captureConfigSql = `SELECT ${captureColumns}`;
|
|
62
|
-
const setColumns = configKeys.map((key) => {
|
|
63
|
-
const value = setConfig[key];
|
|
64
|
-
return `set_config('${key.replace(/'/g, "''")}', '${typeof value === "string" ? value.replace(/'/g, "''") : value}', false) as "${key}"`;
|
|
65
|
-
}).join(", ");
|
|
66
|
-
result.configSetupSql = `SELECT ${setColumns}`;
|
|
67
|
-
}
|
|
68
|
-
return result;
|
|
69
|
-
};
|
|
70
|
-
const sqlSessionContextBuildConfigRestoreBatchSql = (configs) => {
|
|
71
|
-
const keys = Object.keys(configs);
|
|
72
|
-
if (keys.length === 0) return void 0;
|
|
73
|
-
const expressions = keys.map((key) => buildConfigRestoreExpression(key, configs[key])).join(", ");
|
|
74
|
-
return `SELECT ${expressions}`;
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
75
17
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
})
|
|
94
|
-
);
|
|
95
|
-
setupPromises.push(query(roleSetupSql));
|
|
96
|
-
}
|
|
97
|
-
if (captureConfigSql && captureConfigValues && configSetupSql) {
|
|
98
|
-
captured.previousConfigs = {};
|
|
99
|
-
const previousConfigs = captured.previousConfigs;
|
|
100
|
-
setupPromises.push(
|
|
101
|
-
query(captureConfigSql, captureConfigValues).then((res) => {
|
|
102
|
-
const row = res.rows[0];
|
|
103
|
-
captureConfigValues.forEach((key, i) => {
|
|
104
|
-
previousConfigs[key] = row[i];
|
|
105
|
-
});
|
|
106
|
-
})
|
|
107
|
-
);
|
|
108
|
-
setupPromises.push(query(configSetupSql));
|
|
109
|
-
}
|
|
110
|
-
try {
|
|
111
|
-
await Promise.all(setupPromises);
|
|
112
|
-
return await mainQuery();
|
|
113
|
-
} finally {
|
|
114
|
-
try {
|
|
115
|
-
const cleanupPromises = [];
|
|
116
|
-
if (roleSetupSql && captured.previousRole !== void 0) {
|
|
117
|
-
cleanupPromises.push(
|
|
118
|
-
query(`SET ROLE ${quoteRoleIdentifier(captured.previousRole)}`)
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
if (captured.previousConfigs) {
|
|
122
|
-
const restoreSql = sqlSessionContextBuildConfigRestoreBatchSql(
|
|
123
|
-
captured.previousConfigs
|
|
124
|
-
);
|
|
125
|
-
if (restoreSql) {
|
|
126
|
-
cleanupPromises.push(query(restoreSql));
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
await Promise.all(cleanupPromises);
|
|
130
|
-
} finally {
|
|
131
|
-
if (release) {
|
|
132
|
-
await release();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
let pg = require("pg");
|
|
23
|
+
pg = __toESM(pg);
|
|
24
|
+
let pqb_internal = require("pqb/internal");
|
|
25
|
+
let pqb = require("pqb");
|
|
26
|
+
const createDb = ({ log, ...options }) => {
|
|
27
|
+
return (0, pqb.createDbWithAdapter)({
|
|
28
|
+
...options,
|
|
29
|
+
log,
|
|
30
|
+
adapter: new pqb_internal.AdapterClass({
|
|
31
|
+
driverAdapter: NodePostgresAdapter,
|
|
32
|
+
config: options
|
|
33
|
+
})
|
|
34
|
+
});
|
|
136
35
|
};
|
|
137
|
-
|
|
138
|
-
const createDb = ({
|
|
139
|
-
log,
|
|
140
|
-
...options
|
|
141
|
-
}) => {
|
|
142
|
-
return pqb.createDbWithAdapter({
|
|
143
|
-
...options,
|
|
144
|
-
log,
|
|
145
|
-
adapter: new NodePostgresAdapter(options)
|
|
146
|
-
});
|
|
147
|
-
};
|
|
148
|
-
const { types } = pg;
|
|
36
|
+
const { types } = pg.default;
|
|
149
37
|
const defaultTypeParsers = {};
|
|
150
38
|
for (const key in types.builtins) {
|
|
151
|
-
|
|
152
|
-
|
|
39
|
+
const id = types.builtins[key];
|
|
40
|
+
defaultTypeParsers[id] = types.getTypeParser(id);
|
|
153
41
|
}
|
|
154
42
|
[
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
43
|
+
types.builtins.DATE,
|
|
44
|
+
types.builtins.TIMESTAMP,
|
|
45
|
+
types.builtins.TIMESTAMPTZ,
|
|
46
|
+
types.builtins.CIRCLE,
|
|
47
|
+
types.builtins.BYTEA
|
|
160
48
|
].forEach((id) => {
|
|
161
|
-
|
|
49
|
+
delete defaultTypeParsers[id];
|
|
162
50
|
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const url = this.getURL();
|
|
247
|
-
return url ? url.hostname : this.config.host;
|
|
248
|
-
}
|
|
249
|
-
getSchema() {
|
|
250
|
-
return this.config.schema;
|
|
251
|
-
}
|
|
252
|
-
connect() {
|
|
253
|
-
return this.pool.connect();
|
|
254
|
-
}
|
|
255
|
-
query(text, values, startingSavepoint, releasingSavepoint, sqlSessionState) {
|
|
256
|
-
return queryWithSqlSession(
|
|
257
|
-
this,
|
|
258
|
-
void 0,
|
|
259
|
-
text,
|
|
260
|
-
values,
|
|
261
|
-
startingSavepoint,
|
|
262
|
-
releasingSavepoint,
|
|
263
|
-
false,
|
|
264
|
-
sqlSessionState,
|
|
265
|
-
true
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
269
|
-
arrays(text, values, startingSavepoint, releasingSavepoint, sqlSessionState) {
|
|
270
|
-
return queryWithSqlSession(
|
|
271
|
-
this,
|
|
272
|
-
void 0,
|
|
273
|
-
text,
|
|
274
|
-
values,
|
|
275
|
-
startingSavepoint,
|
|
276
|
-
releasingSavepoint,
|
|
277
|
-
true,
|
|
278
|
-
sqlSessionState,
|
|
279
|
-
true
|
|
280
|
-
);
|
|
281
|
-
}
|
|
282
|
-
async transaction(...args) {
|
|
283
|
-
const client = await this.connect();
|
|
284
|
-
const { cb, options } = getTransactionArgs(args);
|
|
285
|
-
try {
|
|
286
|
-
await performQueryOnClient(
|
|
287
|
-
client,
|
|
288
|
-
options?.options ? "BEGIN " + options.options : "BEGIN"
|
|
289
|
-
);
|
|
290
|
-
if (options?.sqlSessionState) {
|
|
291
|
-
const { role, setConfig } = options.sqlSessionState;
|
|
292
|
-
if (role) {
|
|
293
|
-
await performQueryOnClient(client, `SET ROLE ${role}`);
|
|
294
|
-
}
|
|
295
|
-
if (setConfig && Object.keys(setConfig).length > 0) {
|
|
296
|
-
const setColumns = Object.entries(setConfig).map(
|
|
297
|
-
([key, value]) => `set_config('${key.replace(/'/g, "''")}', '${typeof value === "string" ? value.replace(/'/g, "''") : value}', true)`
|
|
298
|
-
).join(", ");
|
|
299
|
-
await performQueryOnClient(client, `SELECT ${setColumns}`);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
const localsSql = getSetLocalsSql(options);
|
|
303
|
-
if (localsSql) {
|
|
304
|
-
await client.query(localsSql);
|
|
305
|
-
}
|
|
306
|
-
const locals = mergeLocals(this.locals, options);
|
|
307
|
-
let result;
|
|
308
|
-
try {
|
|
309
|
-
result = await cb(
|
|
310
|
-
new NodePostgresTransactionAdapter(this, client, this, locals)
|
|
311
|
-
);
|
|
312
|
-
} catch (err) {
|
|
313
|
-
await performQueryOnClient(client, "ROLLBACK");
|
|
314
|
-
throw err;
|
|
315
|
-
}
|
|
316
|
-
await performQueryOnClient(client, "COMMIT");
|
|
317
|
-
return result;
|
|
318
|
-
} finally {
|
|
319
|
-
client.release();
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
close() {
|
|
323
|
-
const { pool } = this;
|
|
324
|
-
this.pool = new pg.Pool(this.config);
|
|
325
|
-
return pool.end();
|
|
326
|
-
}
|
|
327
|
-
assignError(to, dbError) {
|
|
328
|
-
const from = dbError;
|
|
329
|
-
to.message = from.message;
|
|
330
|
-
to.length = from.length;
|
|
331
|
-
to.name = from.name;
|
|
332
|
-
to.severity = from.severity;
|
|
333
|
-
to.code = from.code;
|
|
334
|
-
to.detail = from.detail;
|
|
335
|
-
to.hint = from.hint;
|
|
336
|
-
to.position = from.position;
|
|
337
|
-
to.internalPosition = from.internalPosition;
|
|
338
|
-
to.internalQuery = from.internalQuery;
|
|
339
|
-
to.where = from.where;
|
|
340
|
-
to.schema = from.schema;
|
|
341
|
-
to.table = from.table;
|
|
342
|
-
to.column = from.column;
|
|
343
|
-
to.dataType = from.dataType;
|
|
344
|
-
to.constraint = from.constraint;
|
|
345
|
-
to.file = from.file;
|
|
346
|
-
to.line = from.line;
|
|
347
|
-
to.routine = from.routine;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
const defaultTypesConfig = {
|
|
351
|
-
getTypeParser(id) {
|
|
352
|
-
return defaultTypeParsers[id] || internal.returnArg;
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
const setSearchPath = (client, searchPath) => {
|
|
356
|
-
if (client.connection.searchPath !== searchPath) {
|
|
357
|
-
client.connection.searchPath = searchPath;
|
|
358
|
-
return client.query(`SET search_path = ${searchPath || "public"}`);
|
|
359
|
-
}
|
|
360
|
-
return;
|
|
361
|
-
};
|
|
362
|
-
const queryWithSqlSession = async (adapter, client, text, values, startingSavepoint, releasingSavepoint, arraysMode, sessionState, borrowConnection = false) => {
|
|
363
|
-
const setup = sqlSessionContextComputeSetup(sessionState);
|
|
364
|
-
if (!setup) {
|
|
365
|
-
if (borrowConnection) {
|
|
366
|
-
const conn2 = await adapter.connect();
|
|
367
|
-
try {
|
|
368
|
-
await setSearchPath(conn2, adapter.searchPath);
|
|
369
|
-
return await performQueryOnClient(
|
|
370
|
-
conn2,
|
|
371
|
-
text,
|
|
372
|
-
values,
|
|
373
|
-
arraysMode ? "array" : void 0,
|
|
374
|
-
startingSavepoint,
|
|
375
|
-
releasingSavepoint
|
|
376
|
-
);
|
|
377
|
-
} finally {
|
|
378
|
-
conn2.release();
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
return performQueryOnClient(
|
|
382
|
-
client,
|
|
383
|
-
text,
|
|
384
|
-
values,
|
|
385
|
-
arraysMode ? "array" : void 0,
|
|
386
|
-
startingSavepoint,
|
|
387
|
-
releasingSavepoint
|
|
388
|
-
);
|
|
389
|
-
}
|
|
390
|
-
const conn = borrowConnection ? await adapter.connect() : client;
|
|
391
|
-
const queryFn = (sql, vals) => {
|
|
392
|
-
return conn.query({ text: sql, values: vals, rowMode: "array" }).then((res) => ({
|
|
393
|
-
rows: res.rows,
|
|
394
|
-
rowCount: res.rowCount ?? 0,
|
|
395
|
-
fields: res.fields.map((f) => ({ name: f.name }))
|
|
396
|
-
}));
|
|
397
|
-
};
|
|
398
|
-
const releaseFn = borrowConnection ? async () => {
|
|
399
|
-
conn.release();
|
|
400
|
-
} : void 0;
|
|
401
|
-
const mainQuery = () => performQueryOnClient(
|
|
402
|
-
conn,
|
|
403
|
-
text,
|
|
404
|
-
values,
|
|
405
|
-
arraysMode ? "array" : void 0,
|
|
406
|
-
startingSavepoint,
|
|
407
|
-
releasingSavepoint
|
|
408
|
-
);
|
|
409
|
-
if (borrowConnection) {
|
|
410
|
-
try {
|
|
411
|
-
await setSearchPath(conn, adapter.searchPath);
|
|
412
|
-
return await sqlSessionContextExecute(
|
|
413
|
-
queryFn,
|
|
414
|
-
setup,
|
|
415
|
-
mainQuery,
|
|
416
|
-
releaseFn
|
|
417
|
-
);
|
|
418
|
-
} catch (err) {
|
|
419
|
-
conn.release();
|
|
420
|
-
throw err;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return sqlSessionContextExecute(queryFn, setup, mainQuery);
|
|
424
|
-
};
|
|
425
|
-
const performQueryOnClient = async (client, text, values, rowMode, startingSavepoint, releasingSavepoint) => {
|
|
426
|
-
const params = {
|
|
427
|
-
text,
|
|
428
|
-
values,
|
|
429
|
-
rowMode,
|
|
430
|
-
types: defaultTypesConfig
|
|
431
|
-
};
|
|
432
|
-
const { __lock } = client;
|
|
433
|
-
if (__lock) {
|
|
434
|
-
let resolve;
|
|
435
|
-
client.__lock = new Promise((res) => {
|
|
436
|
-
resolve = () => {
|
|
437
|
-
res();
|
|
438
|
-
};
|
|
439
|
-
});
|
|
440
|
-
return __lock.then(() => {
|
|
441
|
-
const promise2 = startingSavepoint || releasingSavepoint ? performQueryOnClientWithSavepoint(
|
|
442
|
-
client,
|
|
443
|
-
params,
|
|
444
|
-
startingSavepoint,
|
|
445
|
-
releasingSavepoint
|
|
446
|
-
) : client.query(params);
|
|
447
|
-
promise2.then(resolve, resolve);
|
|
448
|
-
return promise2;
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
const promise = startingSavepoint || releasingSavepoint ? performQueryOnClientWithSavepoint(
|
|
452
|
-
client,
|
|
453
|
-
params,
|
|
454
|
-
startingSavepoint,
|
|
455
|
-
releasingSavepoint
|
|
456
|
-
) : client.query(params);
|
|
457
|
-
client.__lock = promise.catch(internal.noop);
|
|
458
|
-
return promise;
|
|
51
|
+
const NodePostgresAdapter = {
|
|
52
|
+
manualPool: true,
|
|
53
|
+
errorClass: pg.DatabaseError,
|
|
54
|
+
errorFields: {
|
|
55
|
+
message: "message",
|
|
56
|
+
length: "length",
|
|
57
|
+
name: "name",
|
|
58
|
+
severity: "severity",
|
|
59
|
+
code: "code",
|
|
60
|
+
detail: "detail",
|
|
61
|
+
hint: "hint",
|
|
62
|
+
position: "position",
|
|
63
|
+
internalPosition: "internalPosition",
|
|
64
|
+
internalQuery: "internalQuery",
|
|
65
|
+
where: "where",
|
|
66
|
+
schema: "schema",
|
|
67
|
+
table: "table",
|
|
68
|
+
column: "column",
|
|
69
|
+
dataType: "dataType",
|
|
70
|
+
constraint: "constraint",
|
|
71
|
+
file: "file",
|
|
72
|
+
line: "line",
|
|
73
|
+
routine: "routine"
|
|
74
|
+
},
|
|
75
|
+
configure(config) {
|
|
76
|
+
if (config.databaseURL) config.connectionString = config.databaseURL;
|
|
77
|
+
if (config.locals?.search_path) config = {
|
|
78
|
+
...config,
|
|
79
|
+
options: `${config.options ? `${config.options} ` : ""}-c search_path="${config.locals.search_path}"`
|
|
80
|
+
};
|
|
81
|
+
return new pg.default.Pool(config);
|
|
82
|
+
},
|
|
83
|
+
queryClient(client, text, values, startingSavepoint, releasingSavepoint, arraysMode) {
|
|
84
|
+
const params = {
|
|
85
|
+
text,
|
|
86
|
+
values,
|
|
87
|
+
rowMode: arraysMode ? "array" : void 0,
|
|
88
|
+
types: defaultTypesConfig
|
|
89
|
+
};
|
|
90
|
+
const { __lock } = client;
|
|
91
|
+
if (__lock) {
|
|
92
|
+
let resolve;
|
|
93
|
+
client.__lock = new Promise((res) => {
|
|
94
|
+
resolve = () => {
|
|
95
|
+
res();
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
return __lock.then(() => {
|
|
99
|
+
const promise = startingSavepoint || releasingSavepoint ? performQueryOnClientWithSavepoint(client, params, startingSavepoint, releasingSavepoint) : client.query(params);
|
|
100
|
+
promise.then(resolve, resolve);
|
|
101
|
+
return promise;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const promise = startingSavepoint || releasingSavepoint ? performQueryOnClientWithSavepoint(client, params, startingSavepoint, releasingSavepoint) : client.query(params);
|
|
105
|
+
client.__lock = promise.catch(pqb_internal.noop);
|
|
106
|
+
return promise;
|
|
107
|
+
},
|
|
108
|
+
borrow(pool) {
|
|
109
|
+
return pool.connect();
|
|
110
|
+
},
|
|
111
|
+
release(client) {
|
|
112
|
+
client.release();
|
|
113
|
+
},
|
|
114
|
+
async begin(pool, cb, options) {
|
|
115
|
+
const client = await pool.connect();
|
|
116
|
+
try {
|
|
117
|
+
await this.queryClient(client, options ? "BEGIN " + options : "BEGIN");
|
|
118
|
+
let result;
|
|
119
|
+
try {
|
|
120
|
+
result = await cb(client);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
await this.queryClient(client, "ROLLBACK");
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
await this.queryClient(client, "COMMIT");
|
|
126
|
+
return result;
|
|
127
|
+
} finally {
|
|
128
|
+
client.release();
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
close(pool) {
|
|
132
|
+
return pool.end();
|
|
133
|
+
}
|
|
459
134
|
};
|
|
135
|
+
const defaultTypesConfig = { getTypeParser(id) {
|
|
136
|
+
return defaultTypeParsers[id] || pqb_internal.returnArg;
|
|
137
|
+
} };
|
|
460
138
|
const performQueryOnClientWithSavepoint = (client, params, startingSavepoint, releasingSavepoint) => {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
throw err;
|
|
471
|
-
}
|
|
472
|
-
);
|
|
473
|
-
}
|
|
474
|
-
return promise;
|
|
139
|
+
let promise = startingSavepoint ? client.query(`SAVEPOINT "${startingSavepoint}"`).then(() => client.query(params)) : client.query(params);
|
|
140
|
+
if (releasingSavepoint) promise = promise.then(async (res) => {
|
|
141
|
+
await client.query(`RELEASE SAVEPOINT "${releasingSavepoint}"`);
|
|
142
|
+
return res;
|
|
143
|
+
}, async (err) => {
|
|
144
|
+
await client.query(`ROLLBACK TO SAVEPOINT "${releasingSavepoint}"`);
|
|
145
|
+
throw err;
|
|
146
|
+
});
|
|
147
|
+
return promise;
|
|
475
148
|
};
|
|
476
|
-
class NodePostgresTransactionAdapter {
|
|
477
|
-
constructor(adapter, client, parent, locals) {
|
|
478
|
-
this.adapter = adapter;
|
|
479
|
-
this.client = client;
|
|
480
|
-
this.parent = parent;
|
|
481
|
-
this.locals = locals;
|
|
482
|
-
this.errorClass = pg.DatabaseError;
|
|
483
|
-
this.pool = adapter.pool;
|
|
484
|
-
this.config = adapter.config;
|
|
485
|
-
this.searchPath = adapter.searchPath;
|
|
486
|
-
}
|
|
487
|
-
isInTransaction() {
|
|
488
|
-
return true;
|
|
489
|
-
}
|
|
490
|
-
updateConfig(config) {
|
|
491
|
-
return this.adapter.updateConfig(config);
|
|
492
|
-
}
|
|
493
|
-
reconfigure(params) {
|
|
494
|
-
return this.adapter.reconfigure(params);
|
|
495
|
-
}
|
|
496
|
-
getDatabase() {
|
|
497
|
-
return this.adapter.getDatabase();
|
|
498
|
-
}
|
|
499
|
-
getUser() {
|
|
500
|
-
return this.adapter.getUser();
|
|
501
|
-
}
|
|
502
|
-
getSearchPath() {
|
|
503
|
-
return this.adapter.getSearchPath();
|
|
504
|
-
}
|
|
505
|
-
getHost() {
|
|
506
|
-
return this.adapter.getHost();
|
|
507
|
-
}
|
|
508
|
-
getSchema() {
|
|
509
|
-
return this.adapter.getSchema();
|
|
510
|
-
}
|
|
511
|
-
connect() {
|
|
512
|
-
return Promise.resolve(this.client);
|
|
513
|
-
}
|
|
514
|
-
async query(text, values, startingSavepoint, releasingSavepoint, sqlSessionState) {
|
|
515
|
-
return queryWithSqlSession(
|
|
516
|
-
this.adapter,
|
|
517
|
-
this.client,
|
|
518
|
-
text,
|
|
519
|
-
values,
|
|
520
|
-
startingSavepoint,
|
|
521
|
-
releasingSavepoint,
|
|
522
|
-
false,
|
|
523
|
-
sqlSessionState,
|
|
524
|
-
false
|
|
525
|
-
);
|
|
526
|
-
}
|
|
527
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
528
|
-
async arrays(text, values, startingSavepoint, releasingSavepoint, sqlSessionState) {
|
|
529
|
-
return queryWithSqlSession(
|
|
530
|
-
this.adapter,
|
|
531
|
-
this.client,
|
|
532
|
-
text,
|
|
533
|
-
values,
|
|
534
|
-
startingSavepoint,
|
|
535
|
-
releasingSavepoint,
|
|
536
|
-
true,
|
|
537
|
-
sqlSessionState,
|
|
538
|
-
false
|
|
539
|
-
);
|
|
540
|
-
}
|
|
541
|
-
async transaction(...args) {
|
|
542
|
-
const { cb, options } = getTransactionArgs(args);
|
|
543
|
-
let capturedRole;
|
|
544
|
-
const capturedConfigs = {};
|
|
545
|
-
const sqlSession = options?.sqlSessionState;
|
|
546
|
-
if (sqlSession) {
|
|
547
|
-
if (sqlSession.role) {
|
|
548
|
-
const roleResult = await this.query(
|
|
549
|
-
"SELECT current_role as role"
|
|
550
|
-
);
|
|
551
|
-
capturedRole = roleResult.rows[0].role;
|
|
552
|
-
}
|
|
553
|
-
if (sqlSession.setConfig && Object.keys(sqlSession.setConfig).length > 0) {
|
|
554
|
-
for (const key of Object.keys(sqlSession.setConfig)) {
|
|
555
|
-
const configResult = await this.query(
|
|
556
|
-
`SELECT current_setting('${key.replace(/'/g, "''")}', true) as val`
|
|
557
|
-
);
|
|
558
|
-
capturedConfigs[key] = configResult.rows[0].val;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
const localsSql = getSetLocalsSql(options);
|
|
563
|
-
if (localsSql) {
|
|
564
|
-
await this.query(localsSql);
|
|
565
|
-
}
|
|
566
|
-
const locals = mergeLocals(this.locals, options);
|
|
567
|
-
let res;
|
|
568
|
-
try {
|
|
569
|
-
res = await cb(
|
|
570
|
-
new NodePostgresTransactionAdapter(
|
|
571
|
-
this.adapter,
|
|
572
|
-
this.client,
|
|
573
|
-
this,
|
|
574
|
-
locals
|
|
575
|
-
)
|
|
576
|
-
);
|
|
577
|
-
} finally {
|
|
578
|
-
if (sqlSession) {
|
|
579
|
-
if (capturedRole !== void 0) {
|
|
580
|
-
await this.query(`SET ROLE ${capturedRole}`);
|
|
581
|
-
}
|
|
582
|
-
for (const [key, value] of Object.entries(capturedConfigs)) {
|
|
583
|
-
const restoreValue = value === null ? "" : value;
|
|
584
|
-
await this.query(
|
|
585
|
-
`SELECT set_config('${key.replace(/'/g, "''")}', '${restoreValue.replace(/'/g, "''")}', true)`
|
|
586
|
-
);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
const resetLocalsSql = getResetLocalsSql(this.locals, options);
|
|
590
|
-
if (resetLocalsSql) {
|
|
591
|
-
await this.query(resetLocalsSql);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
return res;
|
|
595
|
-
}
|
|
596
|
-
close() {
|
|
597
|
-
return this.adapter.close();
|
|
598
|
-
}
|
|
599
|
-
assignError(to, from) {
|
|
600
|
-
return this.adapter.assignError(to, from);
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
|
|
604
149
|
exports.NodePostgresAdapter = NodePostgresAdapter;
|
|
605
|
-
exports.NodePostgresTransactionAdapter = NodePostgresTransactionAdapter;
|
|
606
150
|
exports.createDb = createDb;
|
|
607
|
-
|
|
151
|
+
|
|
152
|
+
//# sourceMappingURL=node-postgres.js.map
|