@prairielearn/postgres 3.0.0 → 4.0.1
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 +18 -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 +14 -103
- package/dist/pool.js +41 -117
- package/dist/pool.js.map +1 -1
- package/dist/pool.test.js +11 -11
- package/dist/pool.test.js.map +1 -1
- package/dist/test-utils.js +1 -0
- package/dist/test-utils.js.map +1 -1
- package/package.json +4 -4
- 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 +44 -151
- package/src/test-utils.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/postgres",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -22,15 +22,15 @@
|
|
|
22
22
|
"pg-cursor": "^2.15.3",
|
|
23
23
|
"pg-pool": "^3.10.1",
|
|
24
24
|
"pg-protocol": "^1.10.3",
|
|
25
|
-
"zod": "^3.25.
|
|
25
|
+
"zod": "^3.25.76"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@prairielearn/tsconfig": "^0.0.0",
|
|
29
29
|
"@types/multipipe": "^3.0.5",
|
|
30
|
-
"@types/node": "^22.
|
|
30
|
+
"@types/node": "^22.17.0",
|
|
31
31
|
"@vitest/coverage-v8": "^3.2.4",
|
|
32
32
|
"tsx": "^4.20.3",
|
|
33
|
-
"typescript": "^5.
|
|
33
|
+
"typescript": "^5.9.2",
|
|
34
34
|
"vitest": "^3.2.4"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/default-pool.test.ts
CHANGED
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
|
+
* @returns 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[] = [];
|