@prairielearn/postgres 3.0.0 → 4.0.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/CHANGELOG.md +12 -0
- package/README.md +9 -19
- package/dist/default-pool.d.ts +136 -21
- package/dist/default-pool.js +138 -20
- package/dist/default-pool.js.map +1 -1
- package/dist/default-pool.test.js +2 -1
- package/dist/default-pool.test.js.map +1 -1
- package/dist/pool.d.ts +13 -102
- package/dist/pool.js +40 -116
- package/dist/pool.js.map +1 -1
- package/dist/pool.test.js +11 -11
- package/dist/pool.test.js.map +1 -1
- package/package.json +3 -3
- package/src/default-pool.test.ts +2 -1
- package/src/default-pool.ts +143 -21
- package/src/pool.test.ts +14 -11
- package/src/pool.ts +43 -150
package/src/default-pool.ts
CHANGED
|
@@ -11,55 +11,177 @@ export { defaultPool, type CursorIterator, type QueryParams };
|
|
|
11
11
|
// that they'll be invoked with the correct `this` context, specifically when
|
|
12
12
|
// this module is imported as `import * as db from '...'` and that import is
|
|
13
13
|
// subsequently transformed by Babel to `interopRequireWildcard(...)`.
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
// VSCode currently doesn't allow for us to use `inheritDoc` to inherit the
|
|
16
|
+
// documentation from the `PostgresPool` class. We mirror the documentation
|
|
17
|
+
// here for *async methods* in VSCode intellisense.
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new connection pool and attempts to connect to the database.
|
|
21
|
+
*/
|
|
15
22
|
export const initAsync = defaultPool.initAsync.bind(defaultPool);
|
|
16
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Closes the connection pool.
|
|
25
|
+
*/
|
|
17
26
|
export const closeAsync = defaultPool.closeAsync.bind(defaultPool);
|
|
27
|
+
/**
|
|
28
|
+
* Gets a new client from the connection pool. The caller MUST call `release()` to
|
|
29
|
+
* release the client, whether or not errors occurred while using
|
|
30
|
+
* `client`. The client can call `done(truthy_value)` to force
|
|
31
|
+
* destruction of the client, but this should not be used except in
|
|
32
|
+
* unusual circumstances.
|
|
33
|
+
*/
|
|
18
34
|
export const getClientAsync = defaultPool.getClientAsync.bind(defaultPool);
|
|
19
|
-
|
|
20
|
-
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Performs a query with the given client.
|
|
38
|
+
*/
|
|
21
39
|
export const queryWithClientAsync = defaultPool.queryWithClientAsync.bind(defaultPool);
|
|
22
|
-
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Performs a query with the given client. Errors if the query returns more
|
|
43
|
+
* than one row.
|
|
44
|
+
*/
|
|
23
45
|
export const queryWithClientOneRowAsync = defaultPool.queryWithClientOneRowAsync.bind(defaultPool);
|
|
24
|
-
|
|
25
|
-
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Performs a query with the given client. Errors if the query returns more
|
|
49
|
+
* than one row.
|
|
50
|
+
*/
|
|
26
51
|
export const queryWithClientZeroOrOneRowAsync =
|
|
27
52
|
defaultPool.queryWithClientZeroOrOneRowAsync.bind(defaultPool);
|
|
53
|
+
/**
|
|
54
|
+
* Rolls back the current transaction for the given client.
|
|
55
|
+
*/
|
|
28
56
|
export const rollbackWithClientAsync = defaultPool.rollbackWithClientAsync.bind(defaultPool);
|
|
29
|
-
export const rollbackWithClient = defaultPool.rollbackWithClient.bind(defaultPool);
|
|
30
57
|
export const beginTransactionAsync = defaultPool.beginTransactionAsync.bind(defaultPool);
|
|
58
|
+
/**
|
|
59
|
+
* Commits the transaction if err is null, otherwise rollbacks the transaction.
|
|
60
|
+
* Also releases the client.
|
|
61
|
+
*/
|
|
31
62
|
export const endTransactionAsync = defaultPool.endTransactionAsync.bind(defaultPool);
|
|
32
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Runs the specified function inside of a transaction. The function will
|
|
65
|
+
* receive a database client as an argument, but it can also make queries
|
|
66
|
+
* as usual, and the correct client will be used automatically.
|
|
67
|
+
*
|
|
68
|
+
* The transaction will be rolled back if the function throws an error, and
|
|
69
|
+
* will be committed otherwise.
|
|
70
|
+
*/
|
|
33
71
|
export const runInTransactionAsync = defaultPool.runInTransactionAsync.bind(defaultPool);
|
|
34
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Executes a query with the specified parameters.
|
|
74
|
+
*
|
|
75
|
+
* Using the return value of this function directly is not recommended. Instead, use
|
|
76
|
+
* {@link queryRows}, {@link queryRow}, or {@link queryOptionalRow}.
|
|
77
|
+
*/
|
|
35
78
|
export const queryAsync = defaultPool.queryAsync.bind(defaultPool);
|
|
36
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Executes a query with the specified parameters. Errors if the query does
|
|
81
|
+
* not return exactly one row.
|
|
82
|
+
*
|
|
83
|
+
* @deprecated Use {@link queryRow} instead.
|
|
84
|
+
*/
|
|
37
85
|
export const queryOneRowAsync = defaultPool.queryOneRowAsync.bind(defaultPool);
|
|
38
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Executes a query with the specified parameters. Errors if the query
|
|
88
|
+
* returns more than one row.
|
|
89
|
+
*
|
|
90
|
+
* @deprecated Use {@link queryOptionalRow} instead.
|
|
91
|
+
*/
|
|
39
92
|
export const queryZeroOrOneRowAsync = defaultPool.queryZeroOrOneRowAsync.bind(defaultPool);
|
|
40
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Calls the given sproc with the specified parameters.
|
|
95
|
+
*
|
|
96
|
+
* @deprecated Use {@link callRows} instead.
|
|
97
|
+
*/
|
|
41
98
|
export const callAsync = defaultPool.callAsync.bind(defaultPool);
|
|
42
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Calls the given sproc with the specified parameters. Errors if the
|
|
101
|
+
* sproc does not return exactly one row.
|
|
102
|
+
*
|
|
103
|
+
* @deprecated Use {@link callRow} instead.
|
|
104
|
+
*/
|
|
43
105
|
export const callOneRowAsync = defaultPool.callOneRowAsync.bind(defaultPool);
|
|
44
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Calls the given sproc with the specified parameters. Errors if the
|
|
108
|
+
* sproc returns more than one row.
|
|
109
|
+
*
|
|
110
|
+
* @deprecated Use {@link callOptionalRow} instead.
|
|
111
|
+
*/
|
|
45
112
|
export const callZeroOrOneRowAsync = defaultPool.callZeroOrOneRowAsync.bind(defaultPool);
|
|
46
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Calls a sproc with the specified parameters using a specific client.
|
|
115
|
+
*/
|
|
47
116
|
export const callWithClientAsync = defaultPool.callWithClientAsync.bind(defaultPool);
|
|
48
|
-
|
|
117
|
+
/**
|
|
118
|
+
* Calls a sproc with the specified parameters using a specific client.
|
|
119
|
+
* Errors if the sproc does not return exactly one row.
|
|
120
|
+
*/
|
|
49
121
|
export const callWithClientOneRowAsync = defaultPool.callWithClientOneRowAsync.bind(defaultPool);
|
|
50
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Calls a sproc with the specified parameters using a specific client.
|
|
124
|
+
* Errors if the sproc returns more than one row.
|
|
125
|
+
*/
|
|
51
126
|
export const callWithClientZeroOrOneRowAsync =
|
|
52
127
|
defaultPool.callWithClientZeroOrOneRowAsync.bind(defaultPool);
|
|
128
|
+
/**
|
|
129
|
+
* Executes a query with the specified parameters. Returns an array of rows
|
|
130
|
+
* that conform to the given Zod schema.
|
|
131
|
+
*
|
|
132
|
+
* If the query returns a single column, the return value will be a list of column values.
|
|
133
|
+
*/
|
|
53
134
|
export const queryRows = defaultPool.queryRows.bind(defaultPool);
|
|
135
|
+
/**
|
|
136
|
+
* Executes a query with the specified parameters. Returns exactly one row that conforms to the given Zod schema.
|
|
137
|
+
*
|
|
138
|
+
* If the query returns a single column, the return value will be the column value itself.
|
|
139
|
+
*/
|
|
54
140
|
export const queryRow = defaultPool.queryRow.bind(defaultPool);
|
|
141
|
+
/**
|
|
142
|
+
* Executes a query with the specified parameters. Returns either null or a
|
|
143
|
+
* single row that conforms to the given Zod schema, and errors otherwise.
|
|
144
|
+
*
|
|
145
|
+
* If the query returns a single column, the return value will be the column value itself.
|
|
146
|
+
*/
|
|
55
147
|
export const queryOptionalRow = defaultPool.queryOptionalRow.bind(defaultPool);
|
|
148
|
+
/**
|
|
149
|
+
* Calls the given sproc with the specified parameters.
|
|
150
|
+
* Errors if the sproc does not return anything.
|
|
151
|
+
*/
|
|
56
152
|
export const callRows = defaultPool.callRows.bind(defaultPool);
|
|
153
|
+
/**
|
|
154
|
+
* Calls the given sproc with the specified parameters.
|
|
155
|
+
* Returns exactly one row from the sproc that conforms to the given Zod schema.
|
|
156
|
+
*/
|
|
57
157
|
export const callRow = defaultPool.callRow.bind(defaultPool);
|
|
158
|
+
/**
|
|
159
|
+
* Calls the given sproc with the specified parameters. Returns either null
|
|
160
|
+
* or a single row that conforms to the given Zod schema.
|
|
161
|
+
*/
|
|
58
162
|
export const callOptionalRow = defaultPool.callOptionalRow.bind(defaultPool);
|
|
59
|
-
|
|
163
|
+
/**
|
|
164
|
+
* Returns an {@link CursorIterator} that can be used to iterate over the
|
|
165
|
+
* results of the query in batches, which is useful for large result sets.
|
|
166
|
+
* Each row will be parsed by the given Zod schema.
|
|
167
|
+
*/
|
|
60
168
|
export const queryCursor = defaultPool.queryCursor.bind(defaultPool);
|
|
61
|
-
|
|
169
|
+
/**
|
|
170
|
+
* Set the schema to use for the search path.
|
|
171
|
+
*
|
|
172
|
+
* @param schema The schema name to use (can be "null" to unset the search path)
|
|
173
|
+
*/
|
|
62
174
|
export const setSearchSchema = defaultPool.setSearchSchema.bind(defaultPool);
|
|
175
|
+
/**
|
|
176
|
+
* Get the schema that is currently used for the search path.
|
|
177
|
+
*
|
|
178
|
+
* @return schema in use (may be `null` to indicate no schema)
|
|
179
|
+
*/
|
|
63
180
|
export const getSearchSchema = defaultPool.getSearchSchema.bind(defaultPool);
|
|
64
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Generate, set, and return a random schema name.
|
|
183
|
+
*
|
|
184
|
+
* @param prefix The prefix of the new schema, only the first 28 characters will be used (after lowercasing).
|
|
185
|
+
* @returns The randomly-generated search schema.
|
|
186
|
+
*/
|
|
65
187
|
export const setRandomSearchSchemaAsync = defaultPool.setRandomSearchSchemaAsync.bind(defaultPool);
|
package/src/pool.test.ts
CHANGED
|
@@ -13,7 +13,6 @@ import {
|
|
|
13
13
|
queryOptionalRow,
|
|
14
14
|
queryRow,
|
|
15
15
|
queryRows,
|
|
16
|
-
queryValidatedCursor,
|
|
17
16
|
} from './default-pool.js';
|
|
18
17
|
import { makePostgresTestUtils } from './test-utils.js';
|
|
19
18
|
|
|
@@ -256,7 +255,11 @@ describe('@prairielearn/postgres', function () {
|
|
|
256
255
|
|
|
257
256
|
describe('queryCursor', () => {
|
|
258
257
|
it('returns zero rows', async () => {
|
|
259
|
-
const cursor = await queryCursor(
|
|
258
|
+
const cursor = await queryCursor(
|
|
259
|
+
'SELECT * FROM workspaces WHERE id = 10000;',
|
|
260
|
+
{},
|
|
261
|
+
z.unknown(),
|
|
262
|
+
);
|
|
260
263
|
const rowBatches = [];
|
|
261
264
|
for await (const rows of cursor.iterate(10)) {
|
|
262
265
|
rowBatches.push(rows);
|
|
@@ -265,7 +268,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
265
268
|
});
|
|
266
269
|
|
|
267
270
|
it('returns one row at a time', async () => {
|
|
268
|
-
const cursor = await queryCursor('SELECT * FROM workspaces WHERE id <= 2;', {});
|
|
271
|
+
const cursor = await queryCursor('SELECT * FROM workspaces WHERE id <= 2;', {}, z.unknown());
|
|
269
272
|
const rowBatches = [];
|
|
270
273
|
for await (const rows of cursor.iterate(1)) {
|
|
271
274
|
rowBatches.push(rows);
|
|
@@ -276,7 +279,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
276
279
|
});
|
|
277
280
|
|
|
278
281
|
it('returns all rows at once', async () => {
|
|
279
|
-
const cursor = queryCursor('SELECT * FROM workspaces WHERE id <= 10;', {});
|
|
282
|
+
const cursor = queryCursor('SELECT * FROM workspaces WHERE id <= 10;', {}, z.unknown());
|
|
280
283
|
const rowBatches = [];
|
|
281
284
|
for await (const rows of (await cursor).iterate(10)) {
|
|
282
285
|
rowBatches.push(rows);
|
|
@@ -286,7 +289,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
286
289
|
});
|
|
287
290
|
|
|
288
291
|
it('handles errors', async () => {
|
|
289
|
-
const cursor = await queryCursor('NOT VALID SQL', {});
|
|
292
|
+
const cursor = await queryCursor('NOT VALID SQL', {}, z.unknown());
|
|
290
293
|
|
|
291
294
|
async function readAllRows() {
|
|
292
295
|
const allRows = [];
|
|
@@ -307,7 +310,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
307
310
|
});
|
|
308
311
|
});
|
|
309
312
|
|
|
310
|
-
describe('
|
|
313
|
+
describe('queryCursor', () => {
|
|
311
314
|
const WorkspaceSchema = z.object({
|
|
312
315
|
id: z.string(),
|
|
313
316
|
});
|
|
@@ -318,7 +321,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
318
321
|
|
|
319
322
|
describe('iterator', () => {
|
|
320
323
|
it('validates with provided schema', async () => {
|
|
321
|
-
const cursor = await
|
|
324
|
+
const cursor = await queryCursor(
|
|
322
325
|
'SELECT * FROM workspaces WHERE id <= 10 ORDER BY id ASC;',
|
|
323
326
|
{},
|
|
324
327
|
WorkspaceSchema,
|
|
@@ -334,7 +337,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
334
337
|
});
|
|
335
338
|
|
|
336
339
|
it('throws error when validation fails', async () => {
|
|
337
|
-
const cursor = await
|
|
340
|
+
const cursor = await queryCursor(
|
|
338
341
|
'SELECT * FROM workspaces WHERE id <= 10 ORDER BY id ASC;',
|
|
339
342
|
{},
|
|
340
343
|
BadWorkspaceSchema,
|
|
@@ -356,7 +359,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
356
359
|
|
|
357
360
|
describe('stream', () => {
|
|
358
361
|
it('validates with provided schema', async () => {
|
|
359
|
-
const cursor = await
|
|
362
|
+
const cursor = await queryCursor(
|
|
360
363
|
'SELECT * FROM workspaces WHERE id <= 10 ORDER BY id ASC;',
|
|
361
364
|
{},
|
|
362
365
|
WorkspaceSchema,
|
|
@@ -371,7 +374,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
371
374
|
});
|
|
372
375
|
|
|
373
376
|
it('emits an error when validation fails', async () => {
|
|
374
|
-
const cursor = await
|
|
377
|
+
const cursor = await queryCursor(
|
|
375
378
|
'SELECT * FROM workspaces ORDER BY id ASC;',
|
|
376
379
|
{},
|
|
377
380
|
BadWorkspaceSchema,
|
|
@@ -392,7 +395,7 @@ describe('@prairielearn/postgres', function () {
|
|
|
392
395
|
});
|
|
393
396
|
|
|
394
397
|
it('closes the cursor when the stream is closed', async () => {
|
|
395
|
-
const cursor = await
|
|
398
|
+
const cursor = await queryCursor('SELECT * FROM workspaces;', {}, WorkspaceSchema);
|
|
396
399
|
const stream = cursor.stream(1);
|
|
397
400
|
|
|
398
401
|
const rows: any[] = [];
|
package/src/pool.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
import { Readable, Transform } from 'node:stream';
|
|
3
|
-
import { callbackify } from 'node:util';
|
|
4
3
|
|
|
5
4
|
import debugfn from 'debug';
|
|
6
5
|
import _ from 'lodash';
|
|
@@ -220,11 +219,6 @@ export class PostgresPool {
|
|
|
220
219
|
}
|
|
221
220
|
}
|
|
222
221
|
|
|
223
|
-
/**
|
|
224
|
-
* Creates a new connection pool and attempts to connect to the database.
|
|
225
|
-
*/
|
|
226
|
-
init = callbackify(this.initAsync);
|
|
227
|
-
|
|
228
222
|
/**
|
|
229
223
|
* Closes the connection pool.
|
|
230
224
|
*/
|
|
@@ -235,14 +229,7 @@ export class PostgresPool {
|
|
|
235
229
|
}
|
|
236
230
|
|
|
237
231
|
/**
|
|
238
|
-
*
|
|
239
|
-
*/
|
|
240
|
-
close = callbackify(this.closeAsync);
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Gets a new client from the connection pool. If `err` is not null
|
|
244
|
-
* then `client` and `done` are undefined. If `err` is null then
|
|
245
|
-
* `client` is valid and can be used. The caller MUST call `done()` to
|
|
232
|
+
* Gets a new client from the connection pool. The caller MUST call `release()` to
|
|
246
233
|
* release the client, whether or not errors occurred while using
|
|
247
234
|
* `client`. The client can call `done(truthy_value)` to force
|
|
248
235
|
* destruction of the client, but this should not be used except in
|
|
@@ -290,15 +277,6 @@ export class PostgresPool {
|
|
|
290
277
|
return client;
|
|
291
278
|
}
|
|
292
279
|
|
|
293
|
-
/**
|
|
294
|
-
* Gets a new client from the connection pool.
|
|
295
|
-
*/
|
|
296
|
-
getClient(callback: (error: Error | null, client?: pg.PoolClient, done?: () => void) => void) {
|
|
297
|
-
this.getClientAsync()
|
|
298
|
-
.then((client) => callback(null, client, client.release))
|
|
299
|
-
.catch((err) => callback(err));
|
|
300
|
-
}
|
|
301
|
-
|
|
302
280
|
/**
|
|
303
281
|
* Performs a query with the given client.
|
|
304
282
|
*/
|
|
@@ -321,11 +299,6 @@ export class PostgresPool {
|
|
|
321
299
|
}
|
|
322
300
|
}
|
|
323
301
|
|
|
324
|
-
/**
|
|
325
|
-
* Performs a query with the given client.
|
|
326
|
-
*/
|
|
327
|
-
queryWithClient = callbackify(this.queryWithClientAsync);
|
|
328
|
-
|
|
329
302
|
/**
|
|
330
303
|
* Performs a query with the given client. Errors if the query returns more
|
|
331
304
|
* than one row.
|
|
@@ -349,12 +322,6 @@ export class PostgresPool {
|
|
|
349
322
|
return result;
|
|
350
323
|
}
|
|
351
324
|
|
|
352
|
-
/**
|
|
353
|
-
* Performs a query with the given client. Errors if the query returns more
|
|
354
|
-
* than one row.
|
|
355
|
-
*/
|
|
356
|
-
queryWithClientOneRow = callbackify(this.queryWithClientOneRowAsync);
|
|
357
|
-
|
|
358
325
|
/**
|
|
359
326
|
* Performs a query with the given client. Errors if the query returns more
|
|
360
327
|
* than one row.
|
|
@@ -378,12 +345,6 @@ export class PostgresPool {
|
|
|
378
345
|
return result;
|
|
379
346
|
}
|
|
380
347
|
|
|
381
|
-
/**
|
|
382
|
-
* Performs a query with the given client. Errors if the query returns more
|
|
383
|
-
* than one row.
|
|
384
|
-
*/
|
|
385
|
-
queryWithClientZeroOrOneRow = callbackify(this.queryWithClientZeroOrOneRowAsync);
|
|
386
|
-
|
|
387
348
|
/**
|
|
388
349
|
* Rolls back the current transaction for the given client.
|
|
389
350
|
*/
|
|
@@ -406,21 +367,6 @@ export class PostgresPool {
|
|
|
406
367
|
}
|
|
407
368
|
}
|
|
408
369
|
|
|
409
|
-
/**
|
|
410
|
-
* Rolls back the current transaction for the given client.
|
|
411
|
-
*/
|
|
412
|
-
rollbackWithClient(
|
|
413
|
-
client: pg.PoolClient,
|
|
414
|
-
_done: (release?: any) => void,
|
|
415
|
-
callback: (err: Error | null) => void,
|
|
416
|
-
) {
|
|
417
|
-
// Note that we can't use `util.callbackify` here because this function
|
|
418
|
-
// has an additional unused `done` parameter for backwards compatibility.
|
|
419
|
-
this.rollbackWithClientAsync(client)
|
|
420
|
-
.then(() => callback(null))
|
|
421
|
-
.catch((err) => callback(err));
|
|
422
|
-
}
|
|
423
|
-
|
|
424
370
|
/**
|
|
425
371
|
* Begins a new transaction.
|
|
426
372
|
*/
|
|
@@ -465,21 +411,6 @@ export class PostgresPool {
|
|
|
465
411
|
}
|
|
466
412
|
}
|
|
467
413
|
|
|
468
|
-
/**
|
|
469
|
-
* Commits the transaction if err is null, otherwise rollbacks the transaction.
|
|
470
|
-
* Also releases the client.
|
|
471
|
-
*/
|
|
472
|
-
endTransaction(
|
|
473
|
-
client: pg.PoolClient,
|
|
474
|
-
_done: (rollback?: any) => void,
|
|
475
|
-
err: Error | null | undefined,
|
|
476
|
-
callback: (error: Error | null) => void,
|
|
477
|
-
): void {
|
|
478
|
-
this.endTransactionAsync(client, err)
|
|
479
|
-
.then(() => callback(null))
|
|
480
|
-
.catch((error) => callback(error));
|
|
481
|
-
}
|
|
482
|
-
|
|
483
414
|
/**
|
|
484
415
|
* Runs the specified function inside of a transaction. The function will
|
|
485
416
|
* receive a database client as an argument, but it can also make queries
|
|
@@ -537,11 +468,6 @@ export class PostgresPool {
|
|
|
537
468
|
}
|
|
538
469
|
}
|
|
539
470
|
|
|
540
|
-
/**
|
|
541
|
-
* Executes a query with the specified parameters.
|
|
542
|
-
*/
|
|
543
|
-
query = callbackify(this.queryAsync);
|
|
544
|
-
|
|
545
471
|
/**
|
|
546
472
|
* Executes a query with the specified parameters. Errors if the query does
|
|
547
473
|
* not return exactly one row.
|
|
@@ -560,12 +486,6 @@ export class PostgresPool {
|
|
|
560
486
|
return result;
|
|
561
487
|
}
|
|
562
488
|
|
|
563
|
-
/**
|
|
564
|
-
* Executes a query with the specified parameters. Errors if the query does
|
|
565
|
-
* not return exactly one row.
|
|
566
|
-
*/
|
|
567
|
-
queryOneRow = callbackify(this.queryOneRowAsync);
|
|
568
|
-
|
|
569
489
|
/**
|
|
570
490
|
* Executes a query with the specified parameters. Errors if the query
|
|
571
491
|
* returns more than one row.
|
|
@@ -585,13 +505,7 @@ export class PostgresPool {
|
|
|
585
505
|
}
|
|
586
506
|
|
|
587
507
|
/**
|
|
588
|
-
*
|
|
589
|
-
* returns more than one row.
|
|
590
|
-
*/
|
|
591
|
-
queryZeroOrOneRow = callbackify(this.queryZeroOrOneRowAsync);
|
|
592
|
-
|
|
593
|
-
/**
|
|
594
|
-
* Calls the given function with the specified parameters.
|
|
508
|
+
* Calls the given sproc with the specified parameters.
|
|
595
509
|
*/
|
|
596
510
|
async callAsync(functionName: string, params: any[]): Promise<pg.QueryResult> {
|
|
597
511
|
debug('call()', 'function:', functionName);
|
|
@@ -604,13 +518,8 @@ export class PostgresPool {
|
|
|
604
518
|
}
|
|
605
519
|
|
|
606
520
|
/**
|
|
607
|
-
* Calls the given
|
|
608
|
-
|
|
609
|
-
call = callbackify(this.callAsync);
|
|
610
|
-
|
|
611
|
-
/**
|
|
612
|
-
* Calls the given function with the specified parameters. Errors if the
|
|
613
|
-
* function does not return exactly one row.
|
|
521
|
+
* Calls the given sproc with the specified parameters. Errors if the
|
|
522
|
+
* sproc does not return exactly one row.
|
|
614
523
|
*/
|
|
615
524
|
async callOneRowAsync(functionName: string, params: any[]): Promise<pg.QueryResult> {
|
|
616
525
|
debug('callOneRow()', 'function:', functionName);
|
|
@@ -627,14 +536,8 @@ export class PostgresPool {
|
|
|
627
536
|
}
|
|
628
537
|
|
|
629
538
|
/**
|
|
630
|
-
* Calls the given
|
|
631
|
-
*
|
|
632
|
-
*/
|
|
633
|
-
callOneRow = callbackify(this.callOneRowAsync);
|
|
634
|
-
|
|
635
|
-
/**
|
|
636
|
-
* Calls the given function with the specified parameters. Errors if the
|
|
637
|
-
* function returns more than one row.
|
|
539
|
+
* Calls the given sproc with the specified parameters. Errors if the
|
|
540
|
+
* sproc returns more than one row.
|
|
638
541
|
*/
|
|
639
542
|
async callZeroOrOneRowAsync(functionName: string, params: any[]): Promise<pg.QueryResult> {
|
|
640
543
|
debug('callZeroOrOneRow()', 'function:', functionName);
|
|
@@ -651,13 +554,7 @@ export class PostgresPool {
|
|
|
651
554
|
}
|
|
652
555
|
|
|
653
556
|
/**
|
|
654
|
-
* Calls
|
|
655
|
-
* function returns more than one row.
|
|
656
|
-
*/
|
|
657
|
-
callZeroOrOneRow = callbackify(this.callZeroOrOneRowAsync);
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* Calls a function with the specified parameters using a specific client.
|
|
557
|
+
* Calls a sproc with the specified parameters using a specific client.
|
|
661
558
|
*/
|
|
662
559
|
async callWithClientAsync(
|
|
663
560
|
client: pg.PoolClient,
|
|
@@ -674,13 +571,8 @@ export class PostgresPool {
|
|
|
674
571
|
}
|
|
675
572
|
|
|
676
573
|
/**
|
|
677
|
-
* Calls a
|
|
678
|
-
|
|
679
|
-
callWithClient = callbackify(this.callWithClientAsync);
|
|
680
|
-
|
|
681
|
-
/**
|
|
682
|
-
* Calls a function with the specified parameters using a specific client.
|
|
683
|
-
* Errors if the function does not return exactly one row.
|
|
574
|
+
* Calls a sproc with the specified parameters using a specific client.
|
|
575
|
+
* Errors if the sproc does not return exactly one row.
|
|
684
576
|
*/
|
|
685
577
|
async callWithClientOneRowAsync(
|
|
686
578
|
client: pg.PoolClient,
|
|
@@ -702,13 +594,7 @@ export class PostgresPool {
|
|
|
702
594
|
|
|
703
595
|
/**
|
|
704
596
|
* Calls a function with the specified parameters using a specific client.
|
|
705
|
-
* Errors if the
|
|
706
|
-
*/
|
|
707
|
-
callWithClientOneRow = callbackify(this.callWithClientOneRowAsync);
|
|
708
|
-
|
|
709
|
-
/**
|
|
710
|
-
* Calls a function with the specified parameters using a specific client.
|
|
711
|
-
* Errors if the function returns more than one row.
|
|
597
|
+
* Errors if the sproc returns more than one row.
|
|
712
598
|
*/
|
|
713
599
|
async callWithClientZeroOrOneRowAsync(
|
|
714
600
|
client: pg.PoolClient,
|
|
@@ -728,18 +614,18 @@ export class PostgresPool {
|
|
|
728
614
|
return result;
|
|
729
615
|
}
|
|
730
616
|
|
|
731
|
-
/**
|
|
732
|
-
* Calls a function with the specified parameters using a specific client.
|
|
733
|
-
* Errors if the function returns more than one row.
|
|
734
|
-
*/
|
|
735
|
-
callWithClientZeroOrOneRow = callbackify(this.callWithClientZeroOrOneRowAsync);
|
|
736
|
-
|
|
737
617
|
async queryRows<Model extends z.ZodTypeAny>(sql: string, model: Model): Promise<z.infer<Model>[]>;
|
|
738
618
|
async queryRows<Model extends z.ZodTypeAny>(
|
|
739
619
|
sql: string,
|
|
740
620
|
params: QueryParams,
|
|
741
621
|
model: Model,
|
|
742
622
|
): Promise<z.infer<Model>[]>;
|
|
623
|
+
/**
|
|
624
|
+
* Executes a query with the specified parameters. Returns an array of rows
|
|
625
|
+
* that conform to the given Zod schema.
|
|
626
|
+
*
|
|
627
|
+
* If the query returns a single column, the return value will be a list of column values.
|
|
628
|
+
*/
|
|
743
629
|
async queryRows<Model extends z.ZodTypeAny>(
|
|
744
630
|
sql: string,
|
|
745
631
|
paramsOrSchema: QueryParams | Model,
|
|
@@ -763,6 +649,11 @@ export class PostgresPool {
|
|
|
763
649
|
params: QueryParams,
|
|
764
650
|
model: Model,
|
|
765
651
|
): Promise<z.infer<Model>>;
|
|
652
|
+
/**
|
|
653
|
+
* Executes a query with the specified parameters. Returns exactly one row that conforms to the given Zod schema.
|
|
654
|
+
*
|
|
655
|
+
* If the query returns a single column, the return value will be the column value itself.
|
|
656
|
+
*/
|
|
766
657
|
async queryRow<Model extends z.ZodTypeAny>(
|
|
767
658
|
sql: string,
|
|
768
659
|
paramsOrSchema: QueryParams | Model,
|
|
@@ -788,6 +679,12 @@ export class PostgresPool {
|
|
|
788
679
|
params: QueryParams,
|
|
789
680
|
model: Model,
|
|
790
681
|
): Promise<z.infer<Model> | null>;
|
|
682
|
+
/**
|
|
683
|
+
* Executes a query with the specified parameters. Returns either null or a
|
|
684
|
+
* single row that conforms to the given Zod schema, and errors otherwise.
|
|
685
|
+
*
|
|
686
|
+
* If the query returns a single column, the return value will be the column value itself.
|
|
687
|
+
*/
|
|
791
688
|
async queryOptionalRow<Model extends z.ZodTypeAny>(
|
|
792
689
|
sql: string,
|
|
793
690
|
paramsOrSchema: QueryParams | Model,
|
|
@@ -812,6 +709,10 @@ export class PostgresPool {
|
|
|
812
709
|
params: any[],
|
|
813
710
|
model: Model,
|
|
814
711
|
): Promise<z.infer<Model>[]>;
|
|
712
|
+
/**
|
|
713
|
+
* Calls the given sproc with the specified parameters.
|
|
714
|
+
* Errors if the sproc does not return anything.
|
|
715
|
+
*/
|
|
815
716
|
async callRows<Model extends z.ZodTypeAny>(
|
|
816
717
|
sql: string,
|
|
817
718
|
paramsOrSchema: any[] | Model,
|
|
@@ -835,6 +736,10 @@ export class PostgresPool {
|
|
|
835
736
|
params: any[],
|
|
836
737
|
model: Model,
|
|
837
738
|
): Promise<z.infer<Model>>;
|
|
739
|
+
/**
|
|
740
|
+
* Calls the given sproc with the specified parameters.
|
|
741
|
+
* Returns exactly one row from the sproc that conforms to the given Zod schema.
|
|
742
|
+
*/
|
|
838
743
|
async callRow<Model extends z.ZodTypeAny>(
|
|
839
744
|
sql: string,
|
|
840
745
|
paramsOrSchema: any[] | Model,
|
|
@@ -860,6 +765,10 @@ export class PostgresPool {
|
|
|
860
765
|
params: any[],
|
|
861
766
|
model: Model,
|
|
862
767
|
): Promise<z.infer<Model> | null>;
|
|
768
|
+
/**
|
|
769
|
+
* Calls the given sproc with the specified parameters. Returns either null
|
|
770
|
+
* or a single row that conforms to the given Zod schema.
|
|
771
|
+
*/
|
|
863
772
|
async callOptionalRow<Model extends z.ZodTypeAny>(
|
|
864
773
|
sql: string,
|
|
865
774
|
paramsOrSchema: any[] | Model,
|
|
@@ -882,7 +791,7 @@ export class PostgresPool {
|
|
|
882
791
|
* Returns a {@link Cursor} for the given query. The cursor can be used to
|
|
883
792
|
* read results in batches, which is useful for large result sets.
|
|
884
793
|
*/
|
|
885
|
-
async queryCursorWithClient(
|
|
794
|
+
private async queryCursorWithClient(
|
|
886
795
|
client: pg.PoolClient,
|
|
887
796
|
sql: string,
|
|
888
797
|
params: QueryParams,
|
|
@@ -895,31 +804,20 @@ export class PostgresPool {
|
|
|
895
804
|
return client.query(new Cursor(processedSql, paramsArray));
|
|
896
805
|
}
|
|
897
806
|
|
|
898
|
-
/**
|
|
899
|
-
* Returns an {@link CursorIterator} that can be used to iterate over the
|
|
900
|
-
* results of the query in batches, which is useful for large result sets.
|
|
901
|
-
*/
|
|
902
|
-
async queryCursor<Model extends z.ZodTypeAny>(
|
|
903
|
-
sql: string,
|
|
904
|
-
params: QueryParams,
|
|
905
|
-
): Promise<CursorIterator<z.infer<Model>>> {
|
|
906
|
-
return this.queryValidatedCursorInternal(sql, params);
|
|
907
|
-
}
|
|
908
|
-
|
|
909
807
|
/**
|
|
910
808
|
* Returns an {@link CursorIterator} that can be used to iterate over the
|
|
911
809
|
* results of the query in batches, which is useful for large result sets.
|
|
912
810
|
* Each row will be parsed by the given Zod schema.
|
|
913
811
|
*/
|
|
914
|
-
async
|
|
812
|
+
async queryCursor<Model extends z.ZodTypeAny>(
|
|
915
813
|
sql: string,
|
|
916
814
|
params: QueryParams,
|
|
917
815
|
model: Model,
|
|
918
816
|
): Promise<CursorIterator<z.infer<Model>>> {
|
|
919
|
-
return this.
|
|
817
|
+
return this.queryCursorInternal(sql, params, model);
|
|
920
818
|
}
|
|
921
819
|
|
|
922
|
-
private async
|
|
820
|
+
private async queryCursorInternal<Model extends z.ZodTypeAny>(
|
|
923
821
|
sql: string,
|
|
924
822
|
params: QueryParams,
|
|
925
823
|
model?: Model,
|
|
@@ -1042,11 +940,6 @@ export class PostgresPool {
|
|
|
1042
940
|
return schema;
|
|
1043
941
|
}
|
|
1044
942
|
|
|
1045
|
-
/**
|
|
1046
|
-
* Generate, set, and return a random schema name.
|
|
1047
|
-
*/
|
|
1048
|
-
setRandomSearchSchema = callbackify(this.setRandomSearchSchemaAsync);
|
|
1049
|
-
|
|
1050
943
|
/** The number of established connections. */
|
|
1051
944
|
get totalCount() {
|
|
1052
945
|
return this.pool?.totalCount ?? 0;
|