@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/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
- * Closes the connection pool.
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
- * Executes a query with the specified parameters. Errors if the query
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 function with the specified parameters.
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 function with the specified parameters. Errors if the
631
- * function does not return exactly one row.
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 the given function with the specified parameters. Errors if the
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 function with the specified parameters using a specific client.
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 function does not return exactly one row.
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,163 +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
- /**
738
- * Wrapper around {@link queryAsync} that parses the resulting rows with the
739
- * given Zod schema. Returns only the rows of the query.
740
- */
741
- async queryValidatedRows<Model extends z.ZodTypeAny>(
742
- query: string,
743
- params: QueryParams,
744
- model: Model,
745
- ): Promise<z.infer<Model>[]> {
746
- const results = await this.queryAsync(query, params);
747
- return z.array(model).parse(results.rows);
748
- }
749
-
750
- /**
751
- * Wrapper around {@link queryOneRowAsync} that parses the resulting row with
752
- * the given Zod schema. Returns only a single row of the query.
753
- */
754
- async queryValidatedOneRow<Model extends z.ZodTypeAny>(
755
- query: string,
756
- params: QueryParams,
757
- model: Model,
758
- ): Promise<z.infer<Model>> {
759
- const results = await this.queryOneRowAsync(query, params);
760
- return model.parse(results.rows[0]);
761
- }
762
-
763
- /**
764
- * Wrapper around {@link queryZeroOrOneRowAsync} that parses the resulting row
765
- * (if any) with the given Zod schema. Returns either a single row or `null`.
766
- */
767
- async queryValidatedZeroOrOneRow<Model extends z.ZodTypeAny>(
768
- query: string,
769
- params: QueryParams,
770
- model: Model,
771
- ): Promise<z.infer<Model> | null> {
772
- const results = await this.queryZeroOrOneRowAsync(query, params);
773
- if (results.rows.length === 0) {
774
- return null;
775
- } else {
776
- return model.parse(results.rows[0]);
777
- }
778
- }
779
-
780
- /**
781
- * Wrapper around {@link queryAsync} that validates that only one column is
782
- * returned and parses the data in it with the given Zod schema. Returns only
783
- * the single column of the query as an array.
784
- */
785
- async queryValidatedSingleColumnRows<Model extends z.ZodTypeAny>(
786
- query: string,
787
- params: QueryParams,
788
- model: Model,
789
- ): Promise<z.infer<Model>[]> {
790
- const results = await this.queryAsync(query, params);
791
- if (results.fields.length !== 1) {
792
- throw new Error(`Expected one column, got ${results.fields.length}`);
793
- }
794
- const columnName = results.fields[0].name;
795
- const rawData = results.rows.map((row) => row[columnName]);
796
- return z.array(model).parse(rawData);
797
- }
798
-
799
- /**
800
- * Wrapper around {@link queryOneRowAsync} that validates that only one column
801
- * is returned and parses the data in it with the given Zod schema. Returns
802
- * only the single entry.
803
- */
804
- async queryValidatedSingleColumnOneRow<Model extends z.ZodTypeAny>(
805
- query: string,
806
- params: QueryParams,
807
- model: Model,
808
- ): Promise<z.infer<Model>> {
809
- const results = await this.queryOneRowAsync(query, params);
810
- if (results.fields.length !== 1) {
811
- throw new Error(`Expected one column, got ${results.fields.length}`);
812
- }
813
- const columnName = results.fields[0].name;
814
- return model.parse(results.rows[0][columnName]);
815
- }
816
-
817
- /**
818
- * Wrapper around {@link queryZeroOrOneRowAsync} that validates that only one
819
- * column is returned and parses the data in it (if any) with the given Zod
820
- * schema. Returns either the single row of the query or `null`.
821
- */
822
- async queryValidatedSingleColumnZeroOrOneRow<Model extends z.ZodTypeAny>(
823
- query: string,
824
- params: QueryParams,
825
- model: Model,
826
- ): Promise<z.infer<Model> | null> {
827
- const results = await this.queryZeroOrOneRowAsync(query, params);
828
- if (results.fields.length !== 1) {
829
- throw new Error(`Expected one column, got ${results.fields.length}`);
830
- }
831
- if (results.rows.length === 0) {
832
- return null;
833
- } else {
834
- const columnName = results.fields[0].name;
835
- return model.parse(results.rows[0][columnName]);
836
- }
837
- }
838
-
839
- /**
840
- * Wrapper around {@link callAsync} that parses the resulting rows with the
841
- * given Zod schema. Returns only the rows.
842
- */
843
- async callValidatedRows<Model extends z.ZodTypeAny>(
844
- sprocName: string,
845
- params: any[],
846
- model: Model,
847
- ): Promise<z.infer<Model>[]> {
848
- const results = await this.callAsync(sprocName, params);
849
- return z.array(model).parse(results.rows);
850
- }
851
-
852
- /**
853
- * Wrapper around {@link callOneRowAsync} that parses the resulting rows with
854
- * the given Zod schema. Returns only a single row.
855
- */
856
- async callValidatedOneRow<Model extends z.ZodTypeAny>(
857
- sprocName: string,
858
- params: any[],
859
- model: Model,
860
- ): Promise<z.infer<Model>> {
861
- const results = await this.callOneRowAsync(sprocName, params);
862
- return model.parse(results.rows[0]);
863
- }
864
-
865
- /**
866
- * Wrapper around {@link callZeroOrOneRowAsync} that parses the resulting row
867
- * (if any) with the given Zod schema. Returns at most a single row.
868
- */
869
- async callValidatedZeroOrOneRow<Model extends z.ZodTypeAny>(
870
- sprocName: string,
871
- params: any[],
872
- model: Model,
873
- ): Promise<z.infer<Model> | null> {
874
- const results = await this.callZeroOrOneRowAsync(sprocName, params);
875
- if (results.rows.length === 0) {
876
- return null;
877
- } else {
878
- return model.parse(results.rows[0]);
879
- }
880
- }
881
-
882
617
  async queryRows<Model extends z.ZodTypeAny>(sql: string, model: Model): Promise<z.infer<Model>[]>;
883
618
  async queryRows<Model extends z.ZodTypeAny>(
884
619
  sql: string,
885
620
  params: QueryParams,
886
621
  model: Model,
887
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
+ */
888
629
  async queryRows<Model extends z.ZodTypeAny>(
889
630
  sql: string,
890
631
  paramsOrSchema: QueryParams | Model,
@@ -908,6 +649,11 @@ export class PostgresPool {
908
649
  params: QueryParams,
909
650
  model: Model,
910
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
+ */
911
657
  async queryRow<Model extends z.ZodTypeAny>(
912
658
  sql: string,
913
659
  paramsOrSchema: QueryParams | Model,
@@ -933,6 +679,12 @@ export class PostgresPool {
933
679
  params: QueryParams,
934
680
  model: Model,
935
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
+ */
936
688
  async queryOptionalRow<Model extends z.ZodTypeAny>(
937
689
  sql: string,
938
690
  paramsOrSchema: QueryParams | Model,
@@ -957,6 +709,10 @@ export class PostgresPool {
957
709
  params: any[],
958
710
  model: Model,
959
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
+ */
960
716
  async callRows<Model extends z.ZodTypeAny>(
961
717
  sql: string,
962
718
  paramsOrSchema: any[] | Model,
@@ -980,6 +736,10 @@ export class PostgresPool {
980
736
  params: any[],
981
737
  model: Model,
982
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
+ */
983
743
  async callRow<Model extends z.ZodTypeAny>(
984
744
  sql: string,
985
745
  paramsOrSchema: any[] | Model,
@@ -1005,6 +765,10 @@ export class PostgresPool {
1005
765
  params: any[],
1006
766
  model: Model,
1007
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
+ */
1008
772
  async callOptionalRow<Model extends z.ZodTypeAny>(
1009
773
  sql: string,
1010
774
  paramsOrSchema: any[] | Model,
@@ -1027,7 +791,7 @@ export class PostgresPool {
1027
791
  * Returns a {@link Cursor} for the given query. The cursor can be used to
1028
792
  * read results in batches, which is useful for large result sets.
1029
793
  */
1030
- async queryCursorWithClient(
794
+ private async queryCursorWithClient(
1031
795
  client: pg.PoolClient,
1032
796
  sql: string,
1033
797
  params: QueryParams,
@@ -1040,31 +804,20 @@ export class PostgresPool {
1040
804
  return client.query(new Cursor(processedSql, paramsArray));
1041
805
  }
1042
806
 
1043
- /**
1044
- * Returns an {@link CursorIterator} that can be used to iterate over the
1045
- * results of the query in batches, which is useful for large result sets.
1046
- */
1047
- async queryCursor<Model extends z.ZodTypeAny>(
1048
- sql: string,
1049
- params: QueryParams,
1050
- ): Promise<CursorIterator<z.infer<Model>>> {
1051
- return this.queryValidatedCursorInternal(sql, params);
1052
- }
1053
-
1054
807
  /**
1055
808
  * Returns an {@link CursorIterator} that can be used to iterate over the
1056
809
  * results of the query in batches, which is useful for large result sets.
1057
810
  * Each row will be parsed by the given Zod schema.
1058
811
  */
1059
- async queryValidatedCursor<Model extends z.ZodTypeAny>(
812
+ async queryCursor<Model extends z.ZodTypeAny>(
1060
813
  sql: string,
1061
814
  params: QueryParams,
1062
815
  model: Model,
1063
816
  ): Promise<CursorIterator<z.infer<Model>>> {
1064
- return this.queryValidatedCursorInternal(sql, params, model);
817
+ return this.queryCursorInternal(sql, params, model);
1065
818
  }
1066
819
 
1067
- private async queryValidatedCursorInternal<Model extends z.ZodTypeAny>(
820
+ private async queryCursorInternal<Model extends z.ZodTypeAny>(
1068
821
  sql: string,
1069
822
  params: QueryParams,
1070
823
  model?: Model,
@@ -1187,11 +940,6 @@ export class PostgresPool {
1187
940
  return schema;
1188
941
  }
1189
942
 
1190
- /**
1191
- * Generate, set, and return a random schema name.
1192
- */
1193
- setRandomSearchSchema = callbackify(this.setRandomSearchSchemaAsync);
1194
-
1195
943
  /** The number of established connections. */
1196
944
  get totalCount() {
1197
945
  return this.pool?.totalCount ?? 0;