@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/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,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 queryValidatedCursor<Model extends z.ZodTypeAny>(
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.queryValidatedCursorInternal(sql, params, model);
817
+ return this.queryCursorInternal(sql, params, model);
920
818
  }
921
819
 
922
- private async queryValidatedCursorInternal<Model extends z.ZodTypeAny>(
820
+ private async queryCursorInternal<Model extends z.ZodTypeAny>(
923
821
  sql: string,
924
822
  params: QueryParams,
925
823
  model?: Model,
@@ -1011,7 +909,7 @@ export class PostgresPool {
1011
909
  /**
1012
910
  * Get the schema that is currently used for the search path.
1013
911
  *
1014
- * @return schema in use (may be `null` to indicate no schema)
912
+ * @returns schema in use (may be `null` to indicate no schema)
1015
913
  */
1016
914
  getSearchSchema(): string | null {
1017
915
  return this.searchSchema;
@@ -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;
package/src/test-utils.ts CHANGED
@@ -112,6 +112,7 @@ async function dropDatabase(
112
112
 
113
113
  const databaseName = database ?? getDatabaseNameForCurrentMochaWorker(options.database);
114
114
  if ('PL_KEEP_TEST_DB' in process.env && !force) {
115
+ // eslint-disable-next-line no-console
115
116
  console.log(`PL_KEEP_TEST_DB environment variable set, not dropping database ${databaseName}`);
116
117
  return;
117
118
  }