@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/CHANGELOG.md +20 -0
- package/README.md +9 -33
- package/dist/default-pool.d.ts +136 -30
- package/dist/default-pool.js +138 -29
- 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 +13 -150
- package/dist/pool.js +40 -219
- package/dist/pool.js.map +1 -1
- package/dist/pool.test.js +11 -11
- package/dist/pool.test.js.map +1 -1
- package/package.json +4 -4
- package/src/default-pool.test.ts +2 -1
- package/src/default-pool.ts +143 -33
- package/src/pool.test.ts +14 -11
- package/src/pool.ts +43 -295
package/dist/pool.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import pg, { type QueryResult } from 'pg';
|
|
2
|
-
import Cursor from 'pg-cursor';
|
|
3
2
|
import { z } from 'zod';
|
|
4
3
|
export type QueryParams = Record<string, any> | any[];
|
|
5
4
|
export interface CursorIterator<T> {
|
|
@@ -31,68 +30,36 @@ export declare class PostgresPool {
|
|
|
31
30
|
* Creates a new connection pool and attempts to connect to the database.
|
|
32
31
|
*/
|
|
33
32
|
initAsync(pgConfig: PostgresPoolConfig, idleErrorHandler: (error: Error, client: pg.PoolClient) => void): Promise<void>;
|
|
34
|
-
/**
|
|
35
|
-
* Creates a new connection pool and attempts to connect to the database.
|
|
36
|
-
*/
|
|
37
|
-
init: (arg1: PostgresPoolConfig, arg2: (error: Error, client: pg.PoolClient) => void, callback: (err: NodeJS.ErrnoException) => void) => void;
|
|
38
33
|
/**
|
|
39
34
|
* Closes the connection pool.
|
|
40
35
|
*/
|
|
41
36
|
closeAsync(): Promise<void>;
|
|
42
37
|
/**
|
|
43
|
-
*
|
|
44
|
-
*/
|
|
45
|
-
close: (callback: (err: NodeJS.ErrnoException) => void) => void;
|
|
46
|
-
/**
|
|
47
|
-
* Gets a new client from the connection pool. If `err` is not null
|
|
48
|
-
* then `client` and `done` are undefined. If `err` is null then
|
|
49
|
-
* `client` is valid and can be used. The caller MUST call `done()` to
|
|
38
|
+
* Gets a new client from the connection pool. The caller MUST call `release()` to
|
|
50
39
|
* release the client, whether or not errors occurred while using
|
|
51
40
|
* `client`. The client can call `done(truthy_value)` to force
|
|
52
41
|
* destruction of the client, but this should not be used except in
|
|
53
42
|
* unusual circumstances.
|
|
54
43
|
*/
|
|
55
44
|
getClientAsync(): Promise<pg.PoolClient>;
|
|
56
|
-
/**
|
|
57
|
-
* Gets a new client from the connection pool.
|
|
58
|
-
*/
|
|
59
|
-
getClient(callback: (error: Error | null, client?: pg.PoolClient, done?: () => void) => void): void;
|
|
60
45
|
/**
|
|
61
46
|
* Performs a query with the given client.
|
|
62
47
|
*/
|
|
63
48
|
queryWithClientAsync(client: pg.PoolClient, sql: string, params: QueryParams): Promise<pg.QueryResult>;
|
|
64
|
-
/**
|
|
65
|
-
* Performs a query with the given client.
|
|
66
|
-
*/
|
|
67
|
-
queryWithClient: (arg1: import("pg").PoolClient, arg2: string, arg3: QueryParams, callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
68
49
|
/**
|
|
69
50
|
* Performs a query with the given client. Errors if the query returns more
|
|
70
51
|
* than one row.
|
|
71
52
|
*/
|
|
72
53
|
queryWithClientOneRowAsync(client: pg.PoolClient, sql: string, params: QueryParams): Promise<pg.QueryResult>;
|
|
73
|
-
/**
|
|
74
|
-
* Performs a query with the given client. Errors if the query returns more
|
|
75
|
-
* than one row.
|
|
76
|
-
*/
|
|
77
|
-
queryWithClientOneRow: (arg1: import("pg").PoolClient, arg2: string, arg3: QueryParams, callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
78
54
|
/**
|
|
79
55
|
* Performs a query with the given client. Errors if the query returns more
|
|
80
56
|
* than one row.
|
|
81
57
|
*/
|
|
82
58
|
queryWithClientZeroOrOneRowAsync(client: pg.PoolClient, sql: string, params: QueryParams): Promise<QueryResult>;
|
|
83
|
-
/**
|
|
84
|
-
* Performs a query with the given client. Errors if the query returns more
|
|
85
|
-
* than one row.
|
|
86
|
-
*/
|
|
87
|
-
queryWithClientZeroOrOneRow: (arg1: import("pg").PoolClient, arg2: string, arg3: QueryParams, callback: (err: NodeJS.ErrnoException | null, result: QueryResult<any>) => void) => void;
|
|
88
59
|
/**
|
|
89
60
|
* Rolls back the current transaction for the given client.
|
|
90
61
|
*/
|
|
91
62
|
rollbackWithClientAsync(client: pg.PoolClient): Promise<void>;
|
|
92
|
-
/**
|
|
93
|
-
* Rolls back the current transaction for the given client.
|
|
94
|
-
*/
|
|
95
|
-
rollbackWithClient(client: pg.PoolClient, _done: (release?: any) => void, callback: (err: Error | null) => void): void;
|
|
96
63
|
/**
|
|
97
64
|
* Begins a new transaction.
|
|
98
65
|
*/
|
|
@@ -102,11 +69,6 @@ export declare class PostgresPool {
|
|
|
102
69
|
* Also releases the client.
|
|
103
70
|
*/
|
|
104
71
|
endTransactionAsync(client: pg.PoolClient, err: Error | null | undefined): Promise<void>;
|
|
105
|
-
/**
|
|
106
|
-
* Commits the transaction if err is null, otherwise rollbacks the transaction.
|
|
107
|
-
* Also releases the client.
|
|
108
|
-
*/
|
|
109
|
-
endTransaction(client: pg.PoolClient, _done: (rollback?: any) => void, err: Error | null | undefined, callback: (error: Error | null) => void): void;
|
|
110
72
|
/**
|
|
111
73
|
* Runs the specified function inside of a transaction. The function will
|
|
112
74
|
* receive a database client as an argument, but it can also make queries
|
|
@@ -120,134 +82,44 @@ export declare class PostgresPool {
|
|
|
120
82
|
* Executes a query with the specified parameters.
|
|
121
83
|
*/
|
|
122
84
|
queryAsync(sql: string, params: QueryParams): Promise<QueryResult>;
|
|
123
|
-
/**
|
|
124
|
-
* Executes a query with the specified parameters.
|
|
125
|
-
*/
|
|
126
|
-
query: (arg1: string, arg2: QueryParams, callback: (err: NodeJS.ErrnoException | null, result: QueryResult<any>) => void) => void;
|
|
127
85
|
/**
|
|
128
86
|
* Executes a query with the specified parameters. Errors if the query does
|
|
129
87
|
* not return exactly one row.
|
|
130
88
|
*/
|
|
131
89
|
queryOneRowAsync(sql: string, params: QueryParams): Promise<pg.QueryResult>;
|
|
132
|
-
/**
|
|
133
|
-
* Executes a query with the specified parameters. Errors if the query does
|
|
134
|
-
* not return exactly one row.
|
|
135
|
-
*/
|
|
136
|
-
queryOneRow: (arg1: string, arg2: QueryParams, callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
137
90
|
/**
|
|
138
91
|
* Executes a query with the specified parameters. Errors if the query
|
|
139
92
|
* returns more than one row.
|
|
140
93
|
*/
|
|
141
94
|
queryZeroOrOneRowAsync(sql: string, params: QueryParams): Promise<pg.QueryResult>;
|
|
142
95
|
/**
|
|
143
|
-
*
|
|
144
|
-
* returns more than one row.
|
|
145
|
-
*/
|
|
146
|
-
queryZeroOrOneRow: (arg1: string, arg2: QueryParams, callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
147
|
-
/**
|
|
148
|
-
* Calls the given function with the specified parameters.
|
|
96
|
+
* Calls the given sproc with the specified parameters.
|
|
149
97
|
*/
|
|
150
98
|
callAsync(functionName: string, params: any[]): Promise<pg.QueryResult>;
|
|
151
99
|
/**
|
|
152
|
-
* Calls the given
|
|
153
|
-
|
|
154
|
-
call: (arg1: string, arg2: any[], callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
155
|
-
/**
|
|
156
|
-
* Calls the given function with the specified parameters. Errors if the
|
|
157
|
-
* function does not return exactly one row.
|
|
100
|
+
* Calls the given sproc with the specified parameters. Errors if the
|
|
101
|
+
* sproc does not return exactly one row.
|
|
158
102
|
*/
|
|
159
103
|
callOneRowAsync(functionName: string, params: any[]): Promise<pg.QueryResult>;
|
|
160
104
|
/**
|
|
161
|
-
* Calls the given
|
|
162
|
-
*
|
|
163
|
-
*/
|
|
164
|
-
callOneRow: (arg1: string, arg2: any[], callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
165
|
-
/**
|
|
166
|
-
* Calls the given function with the specified parameters. Errors if the
|
|
167
|
-
* function returns more than one row.
|
|
105
|
+
* Calls the given sproc with the specified parameters. Errors if the
|
|
106
|
+
* sproc returns more than one row.
|
|
168
107
|
*/
|
|
169
108
|
callZeroOrOneRowAsync(functionName: string, params: any[]): Promise<pg.QueryResult>;
|
|
170
109
|
/**
|
|
171
|
-
* Calls
|
|
172
|
-
* function returns more than one row.
|
|
173
|
-
*/
|
|
174
|
-
callZeroOrOneRow: (arg1: string, arg2: any[], callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
175
|
-
/**
|
|
176
|
-
* Calls a function with the specified parameters using a specific client.
|
|
110
|
+
* Calls a sproc with the specified parameters using a specific client.
|
|
177
111
|
*/
|
|
178
112
|
callWithClientAsync(client: pg.PoolClient, functionName: string, params: any[]): Promise<pg.QueryResult>;
|
|
179
113
|
/**
|
|
180
|
-
* Calls a
|
|
181
|
-
|
|
182
|
-
callWithClient: (arg1: import("pg").PoolClient, arg2: string, arg3: any[], callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
183
|
-
/**
|
|
184
|
-
* Calls a function with the specified parameters using a specific client.
|
|
185
|
-
* Errors if the function does not return exactly one row.
|
|
114
|
+
* Calls a sproc with the specified parameters using a specific client.
|
|
115
|
+
* Errors if the sproc does not return exactly one row.
|
|
186
116
|
*/
|
|
187
117
|
callWithClientOneRowAsync(client: pg.PoolClient, functionName: string, params: any[]): Promise<pg.QueryResult>;
|
|
188
118
|
/**
|
|
189
119
|
* Calls a function with the specified parameters using a specific client.
|
|
190
|
-
* Errors if the
|
|
191
|
-
*/
|
|
192
|
-
callWithClientOneRow: (arg1: import("pg").PoolClient, arg2: string, arg3: any[], callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
193
|
-
/**
|
|
194
|
-
* Calls a function with the specified parameters using a specific client.
|
|
195
|
-
* Errors if the function returns more than one row.
|
|
120
|
+
* Errors if the sproc returns more than one row.
|
|
196
121
|
*/
|
|
197
122
|
callWithClientZeroOrOneRowAsync(client: pg.PoolClient, functionName: string, params: any[]): Promise<pg.QueryResult>;
|
|
198
|
-
/**
|
|
199
|
-
* Calls a function with the specified parameters using a specific client.
|
|
200
|
-
* Errors if the function returns more than one row.
|
|
201
|
-
*/
|
|
202
|
-
callWithClientZeroOrOneRow: (arg1: import("pg").PoolClient, arg2: string, arg3: any[], callback: (err: NodeJS.ErrnoException | null, result: pg.QueryResult<any>) => void) => void;
|
|
203
|
-
/**
|
|
204
|
-
* Wrapper around {@link queryAsync} that parses the resulting rows with the
|
|
205
|
-
* given Zod schema. Returns only the rows of the query.
|
|
206
|
-
*/
|
|
207
|
-
queryValidatedRows<Model extends z.ZodTypeAny>(query: string, params: QueryParams, model: Model): Promise<z.infer<Model>[]>;
|
|
208
|
-
/**
|
|
209
|
-
* Wrapper around {@link queryOneRowAsync} that parses the resulting row with
|
|
210
|
-
* the given Zod schema. Returns only a single row of the query.
|
|
211
|
-
*/
|
|
212
|
-
queryValidatedOneRow<Model extends z.ZodTypeAny>(query: string, params: QueryParams, model: Model): Promise<z.infer<Model>>;
|
|
213
|
-
/**
|
|
214
|
-
* Wrapper around {@link queryZeroOrOneRowAsync} that parses the resulting row
|
|
215
|
-
* (if any) with the given Zod schema. Returns either a single row or `null`.
|
|
216
|
-
*/
|
|
217
|
-
queryValidatedZeroOrOneRow<Model extends z.ZodTypeAny>(query: string, params: QueryParams, model: Model): Promise<z.infer<Model> | null>;
|
|
218
|
-
/**
|
|
219
|
-
* Wrapper around {@link queryAsync} that validates that only one column is
|
|
220
|
-
* returned and parses the data in it with the given Zod schema. Returns only
|
|
221
|
-
* the single column of the query as an array.
|
|
222
|
-
*/
|
|
223
|
-
queryValidatedSingleColumnRows<Model extends z.ZodTypeAny>(query: string, params: QueryParams, model: Model): Promise<z.infer<Model>[]>;
|
|
224
|
-
/**
|
|
225
|
-
* Wrapper around {@link queryOneRowAsync} that validates that only one column
|
|
226
|
-
* is returned and parses the data in it with the given Zod schema. Returns
|
|
227
|
-
* only the single entry.
|
|
228
|
-
*/
|
|
229
|
-
queryValidatedSingleColumnOneRow<Model extends z.ZodTypeAny>(query: string, params: QueryParams, model: Model): Promise<z.infer<Model>>;
|
|
230
|
-
/**
|
|
231
|
-
* Wrapper around {@link queryZeroOrOneRowAsync} that validates that only one
|
|
232
|
-
* column is returned and parses the data in it (if any) with the given Zod
|
|
233
|
-
* schema. Returns either the single row of the query or `null`.
|
|
234
|
-
*/
|
|
235
|
-
queryValidatedSingleColumnZeroOrOneRow<Model extends z.ZodTypeAny>(query: string, params: QueryParams, model: Model): Promise<z.infer<Model> | null>;
|
|
236
|
-
/**
|
|
237
|
-
* Wrapper around {@link callAsync} that parses the resulting rows with the
|
|
238
|
-
* given Zod schema. Returns only the rows.
|
|
239
|
-
*/
|
|
240
|
-
callValidatedRows<Model extends z.ZodTypeAny>(sprocName: string, params: any[], model: Model): Promise<z.infer<Model>[]>;
|
|
241
|
-
/**
|
|
242
|
-
* Wrapper around {@link callOneRowAsync} that parses the resulting rows with
|
|
243
|
-
* the given Zod schema. Returns only a single row.
|
|
244
|
-
*/
|
|
245
|
-
callValidatedOneRow<Model extends z.ZodTypeAny>(sprocName: string, params: any[], model: Model): Promise<z.infer<Model>>;
|
|
246
|
-
/**
|
|
247
|
-
* Wrapper around {@link callZeroOrOneRowAsync} that parses the resulting row
|
|
248
|
-
* (if any) with the given Zod schema. Returns at most a single row.
|
|
249
|
-
*/
|
|
250
|
-
callValidatedZeroOrOneRow<Model extends z.ZodTypeAny>(sprocName: string, params: any[], model: Model): Promise<z.infer<Model> | null>;
|
|
251
123
|
queryRows<Model extends z.ZodTypeAny>(sql: string, model: Model): Promise<z.infer<Model>[]>;
|
|
252
124
|
queryRows<Model extends z.ZodTypeAny>(sql: string, params: QueryParams, model: Model): Promise<z.infer<Model>[]>;
|
|
253
125
|
queryRow<Model extends z.ZodTypeAny>(sql: string, model: Model): Promise<z.infer<Model>>;
|
|
@@ -264,19 +136,14 @@ export declare class PostgresPool {
|
|
|
264
136
|
* Returns a {@link Cursor} for the given query. The cursor can be used to
|
|
265
137
|
* read results in batches, which is useful for large result sets.
|
|
266
138
|
*/
|
|
267
|
-
queryCursorWithClient
|
|
268
|
-
/**
|
|
269
|
-
* Returns an {@link CursorIterator} that can be used to iterate over the
|
|
270
|
-
* results of the query in batches, which is useful for large result sets.
|
|
271
|
-
*/
|
|
272
|
-
queryCursor<Model extends z.ZodTypeAny>(sql: string, params: QueryParams): Promise<CursorIterator<z.infer<Model>>>;
|
|
139
|
+
private queryCursorWithClient;
|
|
273
140
|
/**
|
|
274
141
|
* Returns an {@link CursorIterator} that can be used to iterate over the
|
|
275
142
|
* results of the query in batches, which is useful for large result sets.
|
|
276
143
|
* Each row will be parsed by the given Zod schema.
|
|
277
144
|
*/
|
|
278
|
-
|
|
279
|
-
private
|
|
145
|
+
queryCursor<Model extends z.ZodTypeAny>(sql: string, params: QueryParams, model: Model): Promise<CursorIterator<z.infer<Model>>>;
|
|
146
|
+
private queryCursorInternal;
|
|
280
147
|
/**
|
|
281
148
|
* Set the schema to use for the search path.
|
|
282
149
|
*
|
|
@@ -296,10 +163,6 @@ export declare class PostgresPool {
|
|
|
296
163
|
* @returns The randomly-generated search schema.
|
|
297
164
|
*/
|
|
298
165
|
setRandomSearchSchemaAsync(prefix: string): Promise<string>;
|
|
299
|
-
/**
|
|
300
|
-
* Generate, set, and return a random schema name.
|
|
301
|
-
*/
|
|
302
|
-
setRandomSearchSchema: (arg1: string, callback: (err: NodeJS.ErrnoException, result: string) => void) => void;
|
|
303
166
|
/** The number of established connections. */
|
|
304
167
|
get totalCount(): number;
|
|
305
168
|
/** The number of idle connections. */
|