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