pqb 0.9.24 → 0.9.26
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/index.d.ts +4 -4
- package/dist/index.js +57 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +57 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -271,7 +271,7 @@ declare const getRawSql: (raw: RawExpression) => string;
|
|
|
271
271
|
declare type CommonQueryData = {
|
|
272
272
|
adapter: Adapter;
|
|
273
273
|
shape: ColumnsShapeBase;
|
|
274
|
-
handleResult(q: Query, result: QueryResult): Promise<unknown>;
|
|
274
|
+
handleResult(q: Query, result: QueryResult, isSubQuery?: true): Promise<unknown>;
|
|
275
275
|
returnType: QueryReturnType;
|
|
276
276
|
[relationQueryKey]?: RelationQueryData;
|
|
277
277
|
inTransaction?: boolean;
|
|
@@ -467,7 +467,7 @@ declare type JoinItem = {
|
|
|
467
467
|
type: string;
|
|
468
468
|
args: [relation: string] | [
|
|
469
469
|
arg: string | QueryWithTable,
|
|
470
|
-
conditions: Record<string, string | RawExpression> | RawExpression | ((q: unknown) => QueryBase)
|
|
470
|
+
conditions: Record<string, string | RawExpression> | RawExpression | ((q: unknown) => QueryBase) | true
|
|
471
471
|
] | [
|
|
472
472
|
arg: string | QueryWithTable,
|
|
473
473
|
leftColumn: string | RawExpression,
|
|
@@ -599,7 +599,7 @@ declare class Then {
|
|
|
599
599
|
}
|
|
600
600
|
declare const handleResult: CommonQueryData['handleResult'];
|
|
601
601
|
declare function maybeWrappedThen(this: Query, resolve?: Resolve, reject?: Reject): Promise<any>;
|
|
602
|
-
declare const parseResult: (q: Query, returnType: QueryReturnType | undefined, result: QueryResult) => unknown;
|
|
602
|
+
declare const parseResult: (q: Query, returnType: QueryReturnType | undefined, result: QueryResult, isSubQuery?: boolean) => unknown;
|
|
603
603
|
declare const parseRecord: (parsers: ColumnsParsers, row: any) => any;
|
|
604
604
|
|
|
605
605
|
declare type GetArg<T extends QueryBase> = StringKey<keyof T['selectable']> | RawExpression;
|
|
@@ -792,7 +792,7 @@ declare type JoinArgs<T extends QueryBase, Q extends Query = Query, R extends ke
|
|
|
792
792
|
leftColumn: WithSelectable<T, W> | RawExpression,
|
|
793
793
|
op: string,
|
|
794
794
|
rightColumn: Selectable<T> | RawExpression
|
|
795
|
-
];
|
|
795
|
+
] | [query: Q, conditions: true];
|
|
796
796
|
declare type JoinResult<T extends Query, Args extends JoinArgs<T>, A extends Query | keyof T['relations'] = Args[0]> = AddQueryJoinedTable<T, A extends Query ? A : T['relations'] extends Record<string, Relation> ? A extends keyof T['relations'] ? T['relations'][A]['table'] : A extends keyof T['withData'] ? T['withData'][A] extends WithDataItem ? {
|
|
797
797
|
table: T['withData'][A]['table'];
|
|
798
798
|
result: T['withData'][A]['shape'];
|
package/dist/index.js
CHANGED
|
@@ -285,13 +285,17 @@ const processJoinItem = (ctx, table, args, quotedAs) => {
|
|
|
285
285
|
const query = first.query;
|
|
286
286
|
const quotedFrom = typeof query.from === "string" ? q(query.from) : void 0;
|
|
287
287
|
target = quotedFrom || quoteSchemaAndTable(query.schema, first.table);
|
|
288
|
+
const subQuery = first.toSql({
|
|
289
|
+
values: ctx.values
|
|
290
|
+
});
|
|
288
291
|
let joinAs = quotedFrom || q(first.table);
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
292
|
+
const qAs = query.as ? q(query.as) : void 0;
|
|
293
|
+
const addAs = qAs && qAs !== joinAs;
|
|
294
|
+
if (subQuery.text !== `SELECT * FROM ${target}${addAs ? ` AS ${qAs}` : ""}`) {
|
|
295
|
+
target = `(${subQuery.text}) ${qAs || joinAs}`;
|
|
296
|
+
} else if (addAs) {
|
|
297
|
+
joinAs = qAs;
|
|
298
|
+
target += ` AS ${qAs}`;
|
|
295
299
|
}
|
|
296
300
|
conditions = processArgs(args, ctx, table, first, joinAs, quotedAs);
|
|
297
301
|
const whereSql = whereToSql(ctx, table, query, joinAs);
|
|
@@ -340,7 +344,9 @@ const getConditionsFor3Or4LengthItem = (target, values, quotedAs, args) => {
|
|
|
340
344
|
return `${typeof leftColumn === "string" ? quoteFullColumn(leftColumn, target) : getRaw(leftColumn, values)} ${op} ${typeof rightColumn === "string" ? quoteFullColumn(rightColumn, quotedAs) : getRaw(rightColumn, values)}`;
|
|
341
345
|
};
|
|
342
346
|
const getObjectOrRawConditions = (data, values, quotedAs, joinAs) => {
|
|
343
|
-
if (
|
|
347
|
+
if (data === true) {
|
|
348
|
+
return "true";
|
|
349
|
+
} else if (isRaw(data)) {
|
|
344
350
|
return getRaw(data, values);
|
|
345
351
|
} else {
|
|
346
352
|
const pairs = [];
|
|
@@ -395,6 +401,7 @@ const processAnds = (and, ctx, table, quotedAs, not) => {
|
|
|
395
401
|
return ands.join(" AND ");
|
|
396
402
|
};
|
|
397
403
|
const processWhere = (ands, ctx, table, data, quotedAs, not) => {
|
|
404
|
+
var _a, _b;
|
|
398
405
|
const prefix = not ? "NOT " : "";
|
|
399
406
|
if (typeof data === "function") {
|
|
400
407
|
const qb = data(new ctx.whereQueryBuilder(table, table.query.shape));
|
|
@@ -489,9 +496,23 @@ const processWhere = (ands, ctx, table, data, quotedAs, not) => {
|
|
|
489
496
|
)}`
|
|
490
497
|
);
|
|
491
498
|
} else {
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
499
|
+
let column = table.query.shape[key];
|
|
500
|
+
let quotedColumn;
|
|
501
|
+
if (column) {
|
|
502
|
+
quotedColumn = qc(key, quotedAs);
|
|
503
|
+
} else if (!column) {
|
|
504
|
+
const index = key.indexOf(".");
|
|
505
|
+
if (index !== -1) {
|
|
506
|
+
const joinedTable = key.slice(0, index);
|
|
507
|
+
const joinedColumn = key.slice(index + 1);
|
|
508
|
+
column = (_b = (_a = table.query.joinedShapes) == null ? void 0 : _a[joinedTable]) == null ? void 0 : _b[joinedColumn];
|
|
509
|
+
quotedColumn = qc(joinedColumn, q(joinedTable));
|
|
510
|
+
} else {
|
|
511
|
+
quotedColumn = void 0;
|
|
512
|
+
}
|
|
513
|
+
if (!column || !quotedColumn) {
|
|
514
|
+
throw new Error(`Unknown column ${key} provided to condition`);
|
|
515
|
+
}
|
|
495
516
|
}
|
|
496
517
|
for (const op in value) {
|
|
497
518
|
const operator = column.operators[op];
|
|
@@ -500,7 +521,7 @@ const processWhere = (ands, ctx, table, data, quotedAs, not) => {
|
|
|
500
521
|
}
|
|
501
522
|
ands.push(
|
|
502
523
|
`${prefix}${operator(
|
|
503
|
-
|
|
524
|
+
quotedColumn,
|
|
504
525
|
value[op],
|
|
505
526
|
ctx.values
|
|
506
527
|
)}`
|
|
@@ -4346,6 +4367,9 @@ class QueryGet {
|
|
|
4346
4367
|
}
|
|
4347
4368
|
}
|
|
4348
4369
|
|
|
4370
|
+
class UnknownColumn extends VirtualColumn {
|
|
4371
|
+
}
|
|
4372
|
+
|
|
4349
4373
|
const queryMethodByReturnType = {
|
|
4350
4374
|
all: "query",
|
|
4351
4375
|
rows: "arrays",
|
|
@@ -4367,8 +4391,8 @@ class Then {
|
|
|
4367
4391
|
return this.then(void 0, fn);
|
|
4368
4392
|
}
|
|
4369
4393
|
}
|
|
4370
|
-
const handleResult = async (q, result) => {
|
|
4371
|
-
return parseResult(q, q.query.returnType || "all", result);
|
|
4394
|
+
const handleResult = async (q, result, isSubQuery) => {
|
|
4395
|
+
return parseResult(q, q.query.returnType || "all", result, isSubQuery);
|
|
4372
4396
|
};
|
|
4373
4397
|
function maybeWrappedThen(resolve, reject) {
|
|
4374
4398
|
if (this.query.wrapInTransaction && !this.query.inTransaction) {
|
|
@@ -4464,7 +4488,7 @@ const assignError = (to, from) => {
|
|
|
4464
4488
|
to.routine = from.routine;
|
|
4465
4489
|
return to;
|
|
4466
4490
|
};
|
|
4467
|
-
const parseResult = (q, returnType = "all", result) => {
|
|
4491
|
+
const parseResult = (q, returnType = "all", result, isSubQuery) => {
|
|
4468
4492
|
var _a, _b;
|
|
4469
4493
|
switch (returnType) {
|
|
4470
4494
|
case "all": {
|
|
@@ -4498,7 +4522,12 @@ const parseResult = (q, returnType = "all", result) => {
|
|
|
4498
4522
|
case "pluck": {
|
|
4499
4523
|
const { parsers } = q.query;
|
|
4500
4524
|
if (parsers == null ? void 0 : parsers.pluck) {
|
|
4525
|
+
if (isSubQuery) {
|
|
4526
|
+
return result.rows.map((row) => parsers.pluck(row));
|
|
4527
|
+
}
|
|
4501
4528
|
return result.rows.map((row) => parsers.pluck(row[0]));
|
|
4529
|
+
} else if (isSubQuery) {
|
|
4530
|
+
return result.rows;
|
|
4502
4531
|
}
|
|
4503
4532
|
return result.rows.map((row) => row[0]);
|
|
4504
4533
|
}
|
|
@@ -4556,9 +4585,6 @@ const getCallbacks = (first, second) => {
|
|
|
4556
4585
|
return first && second ? [...first, ...second] : first ? first : second;
|
|
4557
4586
|
};
|
|
4558
4587
|
|
|
4559
|
-
class UnknownColumn extends VirtualColumn {
|
|
4560
|
-
}
|
|
4561
|
-
|
|
4562
4588
|
const addParserForRawExpression = (q, key, raw) => {
|
|
4563
4589
|
var _a;
|
|
4564
4590
|
const parser = (_a = raw.__column) == null ? void 0 : _a.parseFn;
|
|
@@ -4583,7 +4609,7 @@ const addParserForSelectItem = (q, as, key, arg) => {
|
|
|
4583
4609
|
addParserToQuery(q.query, key, (item) => {
|
|
4584
4610
|
const t = rel.query.returnType || "all";
|
|
4585
4611
|
subQueryResult.rows = t === "all" || t === "rows" || t === "pluck" ? item : [item];
|
|
4586
|
-
return parseResult(rel, t, subQueryResult);
|
|
4612
|
+
return parseResult(rel, t, subQueryResult, true);
|
|
4587
4613
|
});
|
|
4588
4614
|
}
|
|
4589
4615
|
return rel;
|
|
@@ -5461,11 +5487,11 @@ const insert = (self, {
|
|
|
5461
5487
|
}
|
|
5462
5488
|
if (returnType === "oneOrThrow" || ((_a = q.query.fromQuery) == null ? void 0 : _a.query.returnType) === "oneOrThrow") {
|
|
5463
5489
|
const { handleResult } = q.query;
|
|
5464
|
-
q.query.handleResult = async (q2, r) => {
|
|
5490
|
+
q.query.handleResult = async (q2, r, i) => {
|
|
5465
5491
|
if (r.rowCount === 0) {
|
|
5466
5492
|
throw new NotFoundError();
|
|
5467
5493
|
}
|
|
5468
|
-
return await handleResult(q2, r);
|
|
5494
|
+
return await handleResult(q2, r, i);
|
|
5469
5495
|
};
|
|
5470
5496
|
}
|
|
5471
5497
|
const requiredColumns = Object.keys(ctx.requiredReturning);
|
|
@@ -5481,11 +5507,11 @@ const insert = (self, {
|
|
|
5481
5507
|
if (ctx.returnTypeAll) {
|
|
5482
5508
|
q.query.returnType = "all";
|
|
5483
5509
|
const { handleResult } = q.query;
|
|
5484
|
-
q.query.handleResult = async (q2, queryResult) => {
|
|
5485
|
-
ctx.resultAll = await handleResult(q2, queryResult);
|
|
5510
|
+
q.query.handleResult = async (q2, queryResult, i) => {
|
|
5511
|
+
ctx.resultAll = await handleResult(q2, queryResult, i);
|
|
5486
5512
|
if (queryMethodByReturnType[returnType] === "arrays") {
|
|
5487
5513
|
queryResult.rows.forEach(
|
|
5488
|
-
(row,
|
|
5514
|
+
(row, i2) => queryResult.rows[i2] = Object.values(row)
|
|
5489
5515
|
);
|
|
5490
5516
|
}
|
|
5491
5517
|
return parseResult(q2, returnType, queryResult);
|
|
@@ -5873,6 +5899,9 @@ class OnQueryBuilder extends WhereQueryBuilder {
|
|
|
5873
5899
|
constructor(q, shape, joinTo) {
|
|
5874
5900
|
super(q, shape);
|
|
5875
5901
|
this.joinTo = joinTo;
|
|
5902
|
+
this.query.joinedShapes = {
|
|
5903
|
+
[joinTo.query.as || joinTo.table]: joinTo.shape
|
|
5904
|
+
};
|
|
5876
5905
|
}
|
|
5877
5906
|
on(...args) {
|
|
5878
5907
|
return this.clone()._on(...args);
|
|
@@ -6304,8 +6333,8 @@ class Update {
|
|
|
6304
6333
|
if (ctx.updateLater || ctx.returnTypeAll) {
|
|
6305
6334
|
query.returnType = "all";
|
|
6306
6335
|
const { handleResult } = query;
|
|
6307
|
-
query.handleResult = async (q, queryResult) => {
|
|
6308
|
-
ctx.resultAll = await handleResult(q, queryResult);
|
|
6336
|
+
query.handleResult = async (q, queryResult, i) => {
|
|
6337
|
+
ctx.resultAll = await handleResult(q, queryResult, i);
|
|
6309
6338
|
if (ctx.updateLater) {
|
|
6310
6339
|
await Promise.all(ctx.updateLaterPromises);
|
|
6311
6340
|
const t = this.baseQuery.clone().transacting(q);
|
|
@@ -6319,7 +6348,7 @@ class Update {
|
|
|
6319
6348
|
}
|
|
6320
6349
|
if (queryMethodByReturnType[originalReturnType] === "arrays") {
|
|
6321
6350
|
queryResult.rows.forEach(
|
|
6322
|
-
(row,
|
|
6351
|
+
(row, i2) => queryResult.rows[i2] = Object.values(row)
|
|
6323
6352
|
);
|
|
6324
6353
|
}
|
|
6325
6354
|
q.query.returnType = originalReturnType;
|
|
@@ -6475,7 +6504,7 @@ class QueryUpsertOrCreate {
|
|
|
6475
6504
|
this.query.returnType = "one";
|
|
6476
6505
|
this.query.wrapInTransaction = true;
|
|
6477
6506
|
const { handleResult } = this.query;
|
|
6478
|
-
this.query.handleResult = async (q, queryResult) => {
|
|
6507
|
+
this.query.handleResult = async (q, queryResult, i) => {
|
|
6479
6508
|
if (queryResult.rowCount === 0) {
|
|
6480
6509
|
return q.create(data);
|
|
6481
6510
|
} else if (queryResult.rowCount > 1) {
|
|
@@ -6483,7 +6512,7 @@ class QueryUpsertOrCreate {
|
|
|
6483
6512
|
`Only one row was expected to find, found ${queryResult.rowCount} rows.`
|
|
6484
6513
|
);
|
|
6485
6514
|
}
|
|
6486
|
-
return handleResult(q, queryResult);
|
|
6515
|
+
return handleResult(q, queryResult, i);
|
|
6487
6516
|
};
|
|
6488
6517
|
return this;
|
|
6489
6518
|
}
|