@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prairielearn/postgres",
3
- "version": "3.0.0",
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.69"
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.15.34",
30
+ "@types/node": "^22.17.0",
31
31
  "@vitest/coverage-v8": "^3.2.4",
32
32
  "tsx": "^4.20.3",
33
- "typescript": "^5.8.3",
33
+ "typescript": "^5.9.2",
34
34
  "vitest": "^3.2.4"
35
35
  }
36
36
  }
@@ -14,7 +14,8 @@ const HIDDEN_PROPERTIES = new Set([
14
14
  'alsClient',
15
15
  'searchSchema',
16
16
  '_queryCount',
17
- 'queryValidatedCursorInternal',
17
+ 'queryCursorWithClient',
18
+ 'queryCursorInternal',
18
19
  'errorOnUnusedParameters',
19
20
  // Getters
20
21
  'totalCount',
@@ -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
- export const init = defaultPool.init.bind(defaultPool);
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
- export const close = defaultPool.close.bind(defaultPool);
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
- export const getClient = defaultPool.getClient.bind(defaultPool);
20
- export const queryWithClient = defaultPool.queryWithClient.bind(defaultPool);
35
+
36
+ /**
37
+ * Performs a query with the given client.
38
+ */
21
39
  export const queryWithClientAsync = defaultPool.queryWithClientAsync.bind(defaultPool);
22
- export const queryWithClientOneRow = defaultPool.queryWithClientOneRow.bind(defaultPool);
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
- export const queryWithClientZeroOrOneRow =
25
- defaultPool.queryWithClientZeroOrOneRow.bind(defaultPool);
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
- export const endTransaction = defaultPool.endTransaction.bind(defaultPool);
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
- export const query = defaultPool.query.bind(defaultPool);
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
- export const queryOneRow = defaultPool.queryOneRow.bind(defaultPool);
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
- export const queryZeroOrOneRow = defaultPool.queryZeroOrOneRow.bind(defaultPool);
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
- export const call = defaultPool.call.bind(defaultPool);
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
- export const callOneRow = defaultPool.callOneRow.bind(defaultPool);
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
- export const callZeroOrOneRow = defaultPool.callZeroOrOneRow.bind(defaultPool);
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
- export const callWithClient = defaultPool.callWithClient.bind(defaultPool);
113
+ /**
114
+ * Calls a sproc with the specified parameters using a specific client.
115
+ */
47
116
  export const callWithClientAsync = defaultPool.callWithClientAsync.bind(defaultPool);
48
- export const callWithClientOneRow = defaultPool.callWithClientOneRow.bind(defaultPool);
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
- export const callWithClientZeroOrOneRow = defaultPool.callWithClientZeroOrOneRow.bind(defaultPool);
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
- export const queryCursorWithClient = defaultPool.queryCursorWithClient.bind(defaultPool);
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
- export const queryValidatedCursor = defaultPool.queryValidatedCursor.bind(defaultPool);
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
- export const setRandomSearchSchema = defaultPool.setRandomSearchSchema.bind(defaultPool);
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('SELECT * FROM workspaces WHERE id = 10000;', {});
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('queryValidatedCursor', () => {
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 queryValidatedCursor(
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 queryValidatedCursor(
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 queryValidatedCursor(
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 queryValidatedCursor(
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 queryValidatedCursor('SELECT * FROM workspaces;', {}, WorkspaceSchema);
398
+ const cursor = await queryCursor('SELECT * FROM workspaces;', {}, WorkspaceSchema);
396
399
  const stream = cursor.stream(1);
397
400
 
398
401
  const rows: any[] = [];