@prairielearn/postgres 2.1.15 → 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/dist/pool.js 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
  import debugfn from 'debug';
5
4
  import _ from 'lodash';
6
5
  import multipipe from 'multipipe';
@@ -188,10 +187,6 @@ export class PostgresPool {
188
187
  }
189
188
  }
190
189
  }
191
- /**
192
- * Creates a new connection pool and attempts to connect to the database.
193
- */
194
- init = callbackify(this.initAsync);
195
190
  /**
196
191
  * Closes the connection pool.
197
192
  */
@@ -202,13 +197,7 @@ export class PostgresPool {
202
197
  this.pool = null;
203
198
  }
204
199
  /**
205
- * Closes the connection pool.
206
- */
207
- close = callbackify(this.closeAsync);
208
- /**
209
- * Gets a new client from the connection pool. If `err` is not null
210
- * then `client` and `done` are undefined. If `err` is null then
211
- * `client` is valid and can be used. The caller MUST call `done()` to
200
+ * Gets a new client from the connection pool. The caller MUST call `release()` to
212
201
  * release the client, whether or not errors occurred while using
213
202
  * `client`. The client can call `done(truthy_value)` to force
214
203
  * destruction of the client, but this should not be used except in
@@ -253,14 +242,6 @@ export class PostgresPool {
253
242
  }
254
243
  return client;
255
244
  }
256
- /**
257
- * Gets a new client from the connection pool.
258
- */
259
- getClient(callback) {
260
- this.getClientAsync()
261
- .then((client) => callback(null, client, client.release))
262
- .catch((err) => callback(err));
263
- }
264
245
  /**
265
246
  * Performs a query with the given client.
266
247
  */
@@ -279,10 +260,6 @@ export class PostgresPool {
279
260
  throw enhanceError(err, sql, params);
280
261
  }
281
262
  }
282
- /**
283
- * Performs a query with the given client.
284
- */
285
- queryWithClient = callbackify(this.queryWithClientAsync);
286
263
  /**
287
264
  * Performs a query with the given client. Errors if the query returns more
288
265
  * than one row.
@@ -301,11 +278,6 @@ export class PostgresPool {
301
278
  debug('queryWithClientOneRow() success', 'rowCount:', result.rowCount);
302
279
  return result;
303
280
  }
304
- /**
305
- * Performs a query with the given client. Errors if the query returns more
306
- * than one row.
307
- */
308
- queryWithClientOneRow = callbackify(this.queryWithClientOneRowAsync);
309
281
  /**
310
282
  * Performs a query with the given client. Errors if the query returns more
311
283
  * than one row.
@@ -324,11 +296,6 @@ export class PostgresPool {
324
296
  debug('queryWithClientZeroOrOneRow() success', 'rowCount:', result.rowCount);
325
297
  return result;
326
298
  }
327
- /**
328
- * Performs a query with the given client. Errors if the query returns more
329
- * than one row.
330
- */
331
- queryWithClientZeroOrOneRow = callbackify(this.queryWithClientZeroOrOneRowAsync);
332
299
  /**
333
300
  * Rolls back the current transaction for the given client.
334
301
  */
@@ -351,16 +318,6 @@ export class PostgresPool {
351
318
  client.release(err);
352
319
  }
353
320
  }
354
- /**
355
- * Rolls back the current transaction for the given client.
356
- */
357
- rollbackWithClient(client, _done, callback) {
358
- // Note that we can't use `util.callbackify` here because this function
359
- // has an additional unused `done` parameter for backwards compatibility.
360
- this.rollbackWithClientAsync(client)
361
- .then(() => callback(null))
362
- .catch((err) => callback(err));
363
- }
364
321
  /**
365
322
  * Begins a new transaction.
366
323
  */
@@ -406,15 +363,6 @@ export class PostgresPool {
406
363
  }
407
364
  }
408
365
  }
409
- /**
410
- * Commits the transaction if err is null, otherwise rollbacks the transaction.
411
- * Also releases the client.
412
- */
413
- endTransaction(client, _done, err, callback) {
414
- this.endTransactionAsync(client, err)
415
- .then(() => callback(null))
416
- .catch((error) => callback(error));
417
- }
418
366
  /**
419
367
  * Runs the specified function inside of a transaction. The function will
420
368
  * receive a database client as an argument, but it can also make queries
@@ -469,10 +417,6 @@ export class PostgresPool {
469
417
  }
470
418
  }
471
419
  }
472
- /**
473
- * Executes a query with the specified parameters.
474
- */
475
- query = callbackify(this.queryAsync);
476
420
  /**
477
421
  * Executes a query with the specified parameters. Errors if the query does
478
422
  * not return exactly one row.
@@ -490,11 +434,6 @@ export class PostgresPool {
490
434
  debug('queryOneRow() success', 'rowCount:', result.rowCount);
491
435
  return result;
492
436
  }
493
- /**
494
- * Executes a query with the specified parameters. Errors if the query does
495
- * not return exactly one row.
496
- */
497
- queryOneRow = callbackify(this.queryOneRowAsync);
498
437
  /**
499
438
  * Executes a query with the specified parameters. Errors if the query
500
439
  * returns more than one row.
@@ -513,12 +452,7 @@ export class PostgresPool {
513
452
  return result;
514
453
  }
515
454
  /**
516
- * Executes a query with the specified parameters. Errors if the query
517
- * returns more than one row.
518
- */
519
- queryZeroOrOneRow = callbackify(this.queryZeroOrOneRowAsync);
520
- /**
521
- * Calls the given function with the specified parameters.
455
+ * Calls the given sproc with the specified parameters.
522
456
  */
523
457
  async callAsync(functionName, params) {
524
458
  debug('call()', 'function:', functionName);
@@ -530,12 +464,8 @@ export class PostgresPool {
530
464
  return result;
531
465
  }
532
466
  /**
533
- * Calls the given function with the specified parameters.
534
- */
535
- call = callbackify(this.callAsync);
536
- /**
537
- * Calls the given function with the specified parameters. Errors if the
538
- * function does not return exactly one row.
467
+ * Calls the given sproc with the specified parameters. Errors if the
468
+ * sproc does not return exactly one row.
539
469
  */
540
470
  async callOneRowAsync(functionName, params) {
541
471
  debug('callOneRow()', 'function:', functionName);
@@ -551,13 +481,8 @@ export class PostgresPool {
551
481
  return result;
552
482
  }
553
483
  /**
554
- * Calls the given function with the specified parameters. Errors if the
555
- * function does not return exactly one row.
556
- */
557
- callOneRow = callbackify(this.callOneRowAsync);
558
- /**
559
- * Calls the given function with the specified parameters. Errors if the
560
- * function returns more than one row.
484
+ * Calls the given sproc with the specified parameters. Errors if the
485
+ * sproc returns more than one row.
561
486
  */
562
487
  async callZeroOrOneRowAsync(functionName, params) {
563
488
  debug('callZeroOrOneRow()', 'function:', functionName);
@@ -573,12 +498,7 @@ export class PostgresPool {
573
498
  return result;
574
499
  }
575
500
  /**
576
- * Calls the given function with the specified parameters. Errors if the
577
- * function returns more than one row.
578
- */
579
- callZeroOrOneRow = callbackify(this.callZeroOrOneRowAsync);
580
- /**
581
- * Calls a function with the specified parameters using a specific client.
501
+ * Calls a sproc with the specified parameters using a specific client.
582
502
  */
583
503
  async callWithClientAsync(client, functionName, params) {
584
504
  debug('callWithClient()', 'function:', functionName);
@@ -590,12 +510,8 @@ export class PostgresPool {
590
510
  return result;
591
511
  }
592
512
  /**
593
- * Calls a function with the specified parameters using a specific client.
594
- */
595
- callWithClient = callbackify(this.callWithClientAsync);
596
- /**
597
- * Calls a function with the specified parameters using a specific client.
598
- * Errors if the function does not return exactly one row.
513
+ * Calls a sproc with the specified parameters using a specific client.
514
+ * Errors if the sproc does not return exactly one row.
599
515
  */
600
516
  async callWithClientOneRowAsync(client, functionName, params) {
601
517
  debug('callWithClientOneRow()', 'function:', functionName);
@@ -612,12 +528,7 @@ export class PostgresPool {
612
528
  }
613
529
  /**
614
530
  * Calls a function with the specified parameters using a specific client.
615
- * Errors if the function does not return exactly one row.
616
- */
617
- callWithClientOneRow = callbackify(this.callWithClientOneRowAsync);
618
- /**
619
- * Calls a function with the specified parameters using a specific client.
620
- * Errors if the function returns more than one row.
531
+ * Errors if the sproc returns more than one row.
621
532
  */
622
533
  async callWithClientZeroOrOneRowAsync(client, functionName, params) {
623
534
  debug('callWithClientZeroOrOneRow()', 'function:', functionName);
@@ -633,113 +544,11 @@ export class PostgresPool {
633
544
  return result;
634
545
  }
635
546
  /**
636
- * Calls a function with the specified parameters using a specific client.
637
- * Errors if the function returns more than one row.
638
- */
639
- callWithClientZeroOrOneRow = callbackify(this.callWithClientZeroOrOneRowAsync);
640
- /**
641
- * Wrapper around {@link queryAsync} that parses the resulting rows with the
642
- * given Zod schema. Returns only the rows of the query.
643
- */
644
- async queryValidatedRows(query, params, model) {
645
- const results = await this.queryAsync(query, params);
646
- return z.array(model).parse(results.rows);
647
- }
648
- /**
649
- * Wrapper around {@link queryOneRowAsync} that parses the resulting row with
650
- * the given Zod schema. Returns only a single row of the query.
651
- */
652
- async queryValidatedOneRow(query, params, model) {
653
- const results = await this.queryOneRowAsync(query, params);
654
- return model.parse(results.rows[0]);
655
- }
656
- /**
657
- * Wrapper around {@link queryZeroOrOneRowAsync} that parses the resulting row
658
- * (if any) with the given Zod schema. Returns either a single row or `null`.
659
- */
660
- async queryValidatedZeroOrOneRow(query, params, model) {
661
- const results = await this.queryZeroOrOneRowAsync(query, params);
662
- if (results.rows.length === 0) {
663
- return null;
664
- }
665
- else {
666
- return model.parse(results.rows[0]);
667
- }
668
- }
669
- /**
670
- * Wrapper around {@link queryAsync} that validates that only one column is
671
- * returned and parses the data in it with the given Zod schema. Returns only
672
- * the single column of the query as an array.
673
- */
674
- async queryValidatedSingleColumnRows(query, params, model) {
675
- const results = await this.queryAsync(query, params);
676
- if (results.fields.length !== 1) {
677
- throw new Error(`Expected one column, got ${results.fields.length}`);
678
- }
679
- const columnName = results.fields[0].name;
680
- const rawData = results.rows.map((row) => row[columnName]);
681
- return z.array(model).parse(rawData);
682
- }
683
- /**
684
- * Wrapper around {@link queryOneRowAsync} that validates that only one column
685
- * is returned and parses the data in it with the given Zod schema. Returns
686
- * only the single entry.
687
- */
688
- async queryValidatedSingleColumnOneRow(query, params, model) {
689
- const results = await this.queryOneRowAsync(query, params);
690
- if (results.fields.length !== 1) {
691
- throw new Error(`Expected one column, got ${results.fields.length}`);
692
- }
693
- const columnName = results.fields[0].name;
694
- return model.parse(results.rows[0][columnName]);
695
- }
696
- /**
697
- * Wrapper around {@link queryZeroOrOneRowAsync} that validates that only one
698
- * column is returned and parses the data in it (if any) with the given Zod
699
- * schema. Returns either the single row of the query or `null`.
700
- */
701
- async queryValidatedSingleColumnZeroOrOneRow(query, params, model) {
702
- const results = await this.queryZeroOrOneRowAsync(query, params);
703
- if (results.fields.length !== 1) {
704
- throw new Error(`Expected one column, got ${results.fields.length}`);
705
- }
706
- if (results.rows.length === 0) {
707
- return null;
708
- }
709
- else {
710
- const columnName = results.fields[0].name;
711
- return model.parse(results.rows[0][columnName]);
712
- }
713
- }
714
- /**
715
- * Wrapper around {@link callAsync} that parses the resulting rows with the
716
- * given Zod schema. Returns only the rows.
717
- */
718
- async callValidatedRows(sprocName, params, model) {
719
- const results = await this.callAsync(sprocName, params);
720
- return z.array(model).parse(results.rows);
721
- }
722
- /**
723
- * Wrapper around {@link callOneRowAsync} that parses the resulting rows with
724
- * the given Zod schema. Returns only a single row.
725
- */
726
- async callValidatedOneRow(sprocName, params, model) {
727
- const results = await this.callOneRowAsync(sprocName, params);
728
- return model.parse(results.rows[0]);
729
- }
730
- /**
731
- * Wrapper around {@link callZeroOrOneRowAsync} that parses the resulting row
732
- * (if any) with the given Zod schema. Returns at most a single row.
547
+ * Executes a query with the specified parameters. Returns an array of rows
548
+ * that conform to the given Zod schema.
549
+ *
550
+ * If the query returns a single column, the return value will be a list of column values.
733
551
  */
734
- async callValidatedZeroOrOneRow(sprocName, params, model) {
735
- const results = await this.callZeroOrOneRowAsync(sprocName, params);
736
- if (results.rows.length === 0) {
737
- return null;
738
- }
739
- else {
740
- return model.parse(results.rows[0]);
741
- }
742
- }
743
552
  async queryRows(sql, paramsOrSchema, maybeModel) {
744
553
  const params = maybeModel === undefined ? {} : paramsOrSchema;
745
554
  const model = maybeModel === undefined ? paramsOrSchema : maybeModel;
@@ -753,6 +562,11 @@ export class PostgresPool {
753
562
  return z.array(model).parse(results.rows);
754
563
  }
755
564
  }
565
+ /**
566
+ * Executes a query with the specified parameters. Returns exactly one row that conforms to the given Zod schema.
567
+ *
568
+ * If the query returns a single column, the return value will be the column value itself.
569
+ */
756
570
  async queryRow(sql, paramsOrSchema, maybeModel) {
757
571
  const params = maybeModel === undefined ? {} : paramsOrSchema;
758
572
  const model = maybeModel === undefined ? paramsOrSchema : maybeModel;
@@ -765,6 +579,12 @@ export class PostgresPool {
765
579
  return model.parse(results.rows[0]);
766
580
  }
767
581
  }
582
+ /**
583
+ * Executes a query with the specified parameters. Returns either null or a
584
+ * single row that conforms to the given Zod schema, and errors otherwise.
585
+ *
586
+ * If the query returns a single column, the return value will be the column value itself.
587
+ */
768
588
  async queryOptionalRow(sql, paramsOrSchema, maybeModel) {
769
589
  const params = maybeModel === undefined ? {} : paramsOrSchema;
770
590
  const model = maybeModel === undefined ? paramsOrSchema : maybeModel;
@@ -780,6 +600,10 @@ export class PostgresPool {
780
600
  return model.parse(results.rows[0]);
781
601
  }
782
602
  }
603
+ /**
604
+ * Calls the given sproc with the specified parameters.
605
+ * Errors if the sproc does not return anything.
606
+ */
783
607
  async callRows(sql, paramsOrSchema, maybeModel) {
784
608
  const params = maybeModel === undefined ? [] : paramsOrSchema;
785
609
  const model = maybeModel === undefined ? paramsOrSchema : maybeModel;
@@ -793,6 +617,10 @@ export class PostgresPool {
793
617
  return z.array(model).parse(results.rows);
794
618
  }
795
619
  }
620
+ /**
621
+ * Calls the given sproc with the specified parameters.
622
+ * Returns exactly one row from the sproc that conforms to the given Zod schema.
623
+ */
796
624
  async callRow(sql, paramsOrSchema, maybeModel) {
797
625
  const params = maybeModel === undefined ? [] : paramsOrSchema;
798
626
  const model = maybeModel === undefined ? paramsOrSchema : maybeModel;
@@ -805,6 +633,10 @@ export class PostgresPool {
805
633
  return model.parse(results.rows[0]);
806
634
  }
807
635
  }
636
+ /**
637
+ * Calls the given sproc with the specified parameters. Returns either null
638
+ * or a single row that conforms to the given Zod schema.
639
+ */
808
640
  async callOptionalRow(sql, paramsOrSchema, maybeModel) {
809
641
  const params = maybeModel === undefined ? [] : paramsOrSchema;
810
642
  const model = maybeModel === undefined ? paramsOrSchema : maybeModel;
@@ -832,22 +664,15 @@ export class PostgresPool {
832
664
  lastQueryMap.set(client, processedSql);
833
665
  return client.query(new Cursor(processedSql, paramsArray));
834
666
  }
835
- /**
836
- * Returns an {@link CursorIterator} that can be used to iterate over the
837
- * results of the query in batches, which is useful for large result sets.
838
- */
839
- async queryCursor(sql, params) {
840
- return this.queryValidatedCursorInternal(sql, params);
841
- }
842
667
  /**
843
668
  * Returns an {@link CursorIterator} that can be used to iterate over the
844
669
  * results of the query in batches, which is useful for large result sets.
845
670
  * Each row will be parsed by the given Zod schema.
846
671
  */
847
- async queryValidatedCursor(sql, params, model) {
848
- return this.queryValidatedCursorInternal(sql, params, model);
672
+ async queryCursor(sql, params, model) {
673
+ return this.queryCursorInternal(sql, params, model);
849
674
  }
850
- async queryValidatedCursorInternal(sql, params, model) {
675
+ async queryCursorInternal(sql, params, model) {
851
676
  const client = await this.getClientAsync();
852
677
  const cursor = await this.queryCursorWithClient(client, sql, params);
853
678
  let iterateCalled = false;
@@ -958,10 +783,6 @@ export class PostgresPool {
958
783
  await this.setSearchSchema(schema);
959
784
  return schema;
960
785
  }
961
- /**
962
- * Generate, set, and return a random schema name.
963
- */
964
- setRandomSearchSchema = callbackify(this.setRandomSearchSchemaAsync);
965
786
  /** The number of established connections. */
966
787
  get totalCount() {
967
788
  return this.pool?.totalCount ?? 0;