-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.ts
436 lines (367 loc) · 14.2 KB
/
core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import { ExprValueTuples, Expr, ExprValueObjects, QueryFirst, QueryFirstValue, QueryList, QueryJson, isArray, isString, isPlainObject } from '@typed-query-builder/builder';
import { DialectPgsql } from '@typed-query-builder/sql-pgsql';
import { Client, Pool, QueryConfig, QueryArrayConfig, QueryResult, PoolClient } from 'pg';
import Cursor from 'pg-cursor';
export type PgsqlConnection = Client | Pool | PoolClient;
/**
* Options
*/
export interface PgsqlOptions<P>
{
/**
* Named parameters to pass to the query.
*/
params?: P;
/**
* If errors should be thrown when building the SQL when an unsupported feature is being used.
*/
throwError?: boolean;
/**
* If constants should avoided being passed in SQL string and passed as parameters instead.
*/
constantsAsParams?: boolean;
/**
* If the SQL string generated should remove redudant table/source references to columns that are unique in their context.
*/
simplifyReferences?: boolean;
/**
* If the raw expression should be executed, otherwise if it's not a SELECT, INSERT, DELETE, or UPDATE it will be wrapped as a SELECT.
*/
raw?: boolean;
/**
* When calling stream, if the stream should take pauses between batch sizes to process a batch at once.
*/
batchSize?: number;
/**
* If true all strings in results will be inspected for JSON values and be automatically parsed.
*/
detectJson?: boolean;
/**
* If the affected count should be returned, transforming the result into { affected: number, result: any }.
*/
affectedCount?: boolean;
/**
* If any records to be returned should be arrays instead of objects.
*/
arrayMode?: boolean;
/**
* The name for the prepared statement. If not given, one will be generated.
*/
preparedName?: string;
/**
* Don't bother parsing any results, I don't care!
*/
ignoreResults?: boolean;
}
export type AffectedResult<R> = { affected: number, result: R };
/**
* Executes an expression and returns the result.
*
* **Example:**
* ```ts
* const result = await expr.run( exec(conn) );
* ```
*
* @param access The connection, transaction, or prepared statement to stream the expression results from.
* @param options Options that control how the query is built or the results returned.
*/
export function exec<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { affectedCount: true, arrayMode: true }): <R>(expr: Expr<R>) => Promise<AffectedResult<ExprValueTuples<R>>>
export function exec<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { affectedCount: true }): <R>(expr: Expr<R>) => Promise<AffectedResult<ExprValueObjects<R>>>
export function exec<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { arrayMode: true }): <R>(expr: Expr<R>) => Promise<ExprValueTuples<R>>
export function exec<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <R>(expr: Expr<R>) => Promise<ExprValueObjects<R>>
export function exec<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <R>(expr: Expr<R>) => Promise<any>
{
return async <R>(e: Expr<R>) =>
{
const outputFactory = DialectPgsql.output(options);
const output = outputFactory(e);
const query: QueryConfig | QueryArrayConfig = {
text: output.query,
values: options?.params ? output.getParams(options.params) : undefined,
rowMode: options?.arrayMode ? 'array' : undefined,
};
try
{
const results = await access.query<ExprValueObjects<R>>(query);
return parseResult(e, results, options);
}
catch (ex)
{
throw new Error(ex + '\n\nQuery: ' + output.query);
}
};
}
/**
* Executes an expression and returns the result.
*
* **Example:**
* ```ts
* const result = await expr.run( exec(conn) );
* ```
*
* @param access The connection, transaction, or prepared statement to stream the expression results from.
* @param options Options that control how the query is built or the results returned.
*/
export function execMany<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { affectedCount: true, arrayMode: true }): <E extends Expr<any>[]>(...exprs: E) => Promise<{ [I in keyof E]: E[I] extends Expr<infer R> ? AffectedResult<ExprValueTuples<R>> : unknown }>
export function execMany<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { affectedCount: true }): <E extends Expr<any>[]>(...exprs: E) => Promise<{ [I in keyof E]: E[I] extends Expr<infer R> ? AffectedResult<ExprValueObjects<R>> : unknown }>
export function execMany<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { arrayMode: true }): <E extends Expr<any>[]>(...exprs: E) => Promise<{ [I in keyof E]: E[I] extends Expr<infer R> ? ExprValueTuples<R> : unknown }>
export function execMany<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { ignoreResults: true }): <E extends Expr<any>[]>(...exprs: E) => Promise<void>
export function execMany<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <E extends Expr<any>[]>(...exprs: E) => Promise<{ [I in keyof E]: E[I] extends Expr<infer R> ? ExprValueObjects<R> : unknown }>
export function execMany<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <E extends Expr<any>[]>(...exprs: E) => Promise<any>
{
return async <E extends Expr<any>[]>(...exprs: E) =>
{
const outputFactory = DialectPgsql.output(options);
const outputs = exprs.map(outputFactory);
return await Promise.all(outputs.map(output => {
const query: QueryConfig | QueryArrayConfig = {
text: output.query,
values: output.getParams(options?.params),
rowMode: options?.arrayMode ? 'array' : undefined,
};
return access.query(query)
.then(results => parseResult(output.expr, results, options))
.catch(ex => new Error(ex + '\n\nQuery: ' + output.query))
}));
};
}
/**
* The result passed to a stream listener.
*/
export type StreamListenerResult<R> = R extends Array<infer E> ? E : R;
/**
* A listener to results.
*/
export type StreamListener<R, A> = (result: StreamListenerResult<R>, batchIndex: number, batchCount: number, batch: StreamListenerResult<R>[]) => A | Promise<A>;
/**
* A handler that triggers the query and each result is passed to the listener.
*/
export type StreamHandler<R> = <A>(listener: StreamListener<R, A>) => Promise<A[]>;
/**
* Generates a way to stream the results of a query for the processing of large datasets.
* When batch size is given > 1, the given number are collected in memory and once that size is
* reached or there are no more results the request is paused to avoid memory exhaustion and calls
* the function in quick succession with all collected results.
*
* **Example:**
* ```ts
* const streamer = expr.run( stream(conn, { streamBatchSize: 100 }) );
*
* const accumulated = await streamer(async (record) => {
* // handle record, can be async, can return a value to be returned in accumulated array
* });
* ```
*
* @param access The connection, transaction, or prepared statement to stream the expression results from.
* @param options Options that control how the query is built or the results returned.
* @returns A function which when invoked with another function will execute the expression and for each result returned will call the given function.
*/
export function stream<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { arrayMode: true }): <R>(expr: Expr<R>) => StreamHandler<ExprValueTuples<R>>
export function stream<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <R>(expr: Expr<R>) => StreamHandler<ExprValueObjects<R>>
export function stream<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <R>(expr: Expr<R>) => StreamHandler<any>
{
return <R>(e: Expr<R>) =>
{
return async (listener: StreamListener<any, any>) =>
{
const batchSize = options?.batchSize || 1;
const outputFactory = DialectPgsql.output(options);
const output = outputFactory(e);
const params = options?.params ? output.getParams(options.params) : [];
const cursor = new Cursor(output.query, params, {
rowMode: options?.arrayMode ? 'array' : undefined,
});
try
{
const batcher = access.query(cursor);
const accumulated: any[] = [];
let batchCount = 0;
const processBatch = async (batch: any[]) =>
{
for (let batchIndex = 0; batchIndex < batch.length; batchIndex++)
{
const accumulateResult = await listener(batch[batchIndex], batchIndex, batchCount, batch);
if (accumulateResult !== undefined)
{
accumulated.push(accumulateResult);
}
}
batchCount++;
};
return new Promise((resolve, reject) =>
{
const readBatch = () =>
{
batcher.read(batchSize, async (err, rows, result) => {
if (err) {
return reject(err);
}
if (rows.length === 0) {
resolve(accumulated);
} else {
const parsedRows = parseResult(e, result, options);
await processBatch(parsedRows);
readBatch();
}
});
};
readBatch();
});
}
catch (e)
{
throw new Error(e + '\n\nQuery: ' + output.query);
}
};
};
}
/**
* A prepared query that can be executed multiple times. It MUST be released, ideally in a try-finally block.
*/
export interface PreparedQuery<R, P = any>
{
exec(params: P): Promise<R>;
release(): Promise<void>;
}
/**
* Creates a prepared statement for the given expression. This is useful when you need to invoke the same query over and over
* with different parameters. A prepared statement MUST be released, ideally in a try-finally block.
*
* **Example:**
* ```ts
* const prepared = expr.run( prepare(conn) );
*
* try {
* await prepared.exec({ id: 12 });
* await prepared.exec({ id: 34 });
* } finally {
* await prepared.release();
* }
* ```
*
* @param access The connection, transaction, or prepared statement to stream the expression results from.
* @param options Options that control how the query is built or the results returned.
* @returns An object that can be executed multiple times, once finished it must be released.
*/
export function prepare<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { affectedCount: true }): <R>(expr: Expr<any>) => Promise<PreparedQuery<AffectedResult<ExprValueObjects<R>>, P>>
export function prepare<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { affectedCount: true, arrayMode: true }): <R>(expr: Expr<any>) => Promise<PreparedQuery<AffectedResult<ExprValueTuples<R>>, P>>
export function prepare<P = any>(access: PgsqlConnection, options: PgsqlOptions<P> & { arrayMode: true }): <R>(expr: Expr<R>) => Promise<PreparedQuery<ExprValueTuples<R>, P>>
export function prepare<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <R>(expr: Expr<R>) => Promise<PreparedQuery<ExprValueObjects<R>, P>>
export function prepare<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <R>(expr: Expr<R>) => Promise<PreparedQuery<any, P>>
{
return async <R>(e: Expr<R>) =>
{
const outputFactory = DialectPgsql.output(options);
const output = outputFactory(e);
const defaults: Partial<P> = {};
for (const paramName in output.paramIndices)
{
const paramIndex = output.paramIndices[paramName];
const paramValue = output.params[paramIndex];
defaults[paramName] = paramValue;
}
let preparedName = options?.preparedName || '';
if (!preparedName)
{
const N = output.query.length - 1;
const L = Math.min(32, N);
const J = N / L;
for (let i = 1; i <= L; i++)
{
const d = Math.min(N, Math.floor(i * J));
preparedName += output.query.charAt(d);
}
}
const preparedQuery: PreparedQuery<any, P> =
{
async exec(params): Promise<any>
{
const query: QueryConfig | QueryArrayConfig = {
name: preparedName,
text: output.query,
rowMode: options?.arrayMode ? 'array' : undefined,
values: output.getParams({
...defaults,
...(options?.params || {}),
...(params || {})
}),
};
const result = await access.query<ExprValueObjects<R>>(query);
return parseResult(e, result, options);
},
async release(): Promise<void>
{
},
};
return preparedQuery;
};
}
export function parseResult<R, P>(expr: Expr<R>, queryResult: QueryResult<ExprValueObjects<R>>, options?: PgsqlOptions<P>)
{
if (options?.ignoreResults)
{
return;
}
let result = DialectPgsql.getResult(expr, handleResult(expr, queryResult));
if (options && options.detectJson)
{
const traverse = (value: any, onValue: (value: any) => any) =>
{
value = onValue(value);
if (isArray(value))
{
for (let i = 0; i < value.length; i++)
{
value[i] = traverse(value[i], onValue);
}
}
else if (isPlainObject(value))
{
for (const prop in value)
{
value[prop] = traverse(value[prop], onValue);
}
}
return value;
};
if (options.detectJson)
{
result = traverse(result, (value) =>
{
if (isString(value) && value.match(/^({|true$|false$|\[|null$|[+-]?\d|")/))
{
try {
return JSON.parse(value);
} catch (e) {}
}
return value;
});
}
}
return options?.affectedCount
? { affected: queryResult.rowCount, result }
: result;
}
export function handleResult<R, P>(expr: Expr<R>, result: QueryResult<ExprValueObjects<R>>, resultIndex: number = 0): any
{
if (expr instanceof QueryFirst)
{
return result.rows[resultIndex] || null;
}
if (expr instanceof QueryFirstValue)
{
for (const prop in result.rows[resultIndex])
{
return result.rows[resultIndex][prop];
}
}
if (expr instanceof QueryList)
{
return result.rows.map( r => (r as any).item );
}
if (expr instanceof QueryJson)
{
return result.rows[resultIndex];
}
return result.rows;
}