pqb 0.11.4 → 0.11.5
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 -3
- package/dist/index.js +283 -257
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +283 -257
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -291,7 +291,8 @@ type JoinedShapes = Record<string, ColumnsShapeBase>;
|
|
|
291
291
|
type CommonQueryData = {
|
|
292
292
|
adapter: Adapter;
|
|
293
293
|
shape: ColumnsShapeBase;
|
|
294
|
-
|
|
294
|
+
patchResult?(queryResult: QueryResult): Promise<void>;
|
|
295
|
+
handleResult(q: Query, result: QueryResult, isSubQuery?: true): unknown;
|
|
295
296
|
returnType: QueryReturnType;
|
|
296
297
|
[relationQueryKey]?: RelationQueryData;
|
|
297
298
|
wrapInTransaction?: boolean;
|
|
@@ -1119,10 +1120,10 @@ type UpdateResult<T extends Query> = T['meta']['hasSelect'] extends true ? T : S
|
|
|
1119
1120
|
type ChangeCountArg<T extends Query> = keyof T['shape'] | Partial<Record<keyof T['shape'], number>>;
|
|
1120
1121
|
type UpdateCtx = {
|
|
1121
1122
|
willSetKeys?: true;
|
|
1122
|
-
updateLater?: Record<string, unknown>;
|
|
1123
|
-
updateLaterPromises?: Promise<void>[];
|
|
1124
1123
|
returnTypeAll?: true;
|
|
1125
1124
|
resultAll: Record<string, unknown>[];
|
|
1125
|
+
queries?: ((queryResult: QueryResult) => Promise<void>)[];
|
|
1126
|
+
updateData?: Record<string, unknown>;
|
|
1126
1127
|
};
|
|
1127
1128
|
declare class Update {
|
|
1128
1129
|
update<T extends Query>(this: T, arg: UpdateArg<T>): UpdateResult<T>;
|
package/dist/index.js
CHANGED
|
@@ -3695,225 +3695,6 @@ class QueryGet {
|
|
|
3695
3695
|
}
|
|
3696
3696
|
}
|
|
3697
3697
|
|
|
3698
|
-
const queryMethodByReturnType = {
|
|
3699
|
-
all: "query",
|
|
3700
|
-
rows: "arrays",
|
|
3701
|
-
pluck: "arrays",
|
|
3702
|
-
one: "query",
|
|
3703
|
-
oneOrThrow: "query",
|
|
3704
|
-
value: "arrays",
|
|
3705
|
-
valueOrThrow: "arrays",
|
|
3706
|
-
rowCount: "arrays",
|
|
3707
|
-
void: "arrays"
|
|
3708
|
-
};
|
|
3709
|
-
let queryError = void 0;
|
|
3710
|
-
class Then {
|
|
3711
|
-
get then() {
|
|
3712
|
-
queryError = new Error();
|
|
3713
|
-
return maybeWrappedThen;
|
|
3714
|
-
}
|
|
3715
|
-
async catch(fn) {
|
|
3716
|
-
return this.then(void 0, fn);
|
|
3717
|
-
}
|
|
3718
|
-
}
|
|
3719
|
-
const handleResult = (q, result, isSubQuery) => {
|
|
3720
|
-
return parseResult(
|
|
3721
|
-
q,
|
|
3722
|
-
q.query.parsers,
|
|
3723
|
-
q.query.returnType || "all",
|
|
3724
|
-
result,
|
|
3725
|
-
isSubQuery
|
|
3726
|
-
);
|
|
3727
|
-
};
|
|
3728
|
-
function maybeWrappedThen(resolve, reject) {
|
|
3729
|
-
const adapter = this.internal.transactionStorage.getStore();
|
|
3730
|
-
if (this.query.wrapInTransaction && !adapter) {
|
|
3731
|
-
return this.transaction(
|
|
3732
|
-
() => new Promise((resolve2, reject2) => {
|
|
3733
|
-
const adapter2 = this.internal.transactionStorage.getStore();
|
|
3734
|
-
return then(this, adapter2, resolve2, reject2);
|
|
3735
|
-
})
|
|
3736
|
-
).then(resolve, reject);
|
|
3737
|
-
} else {
|
|
3738
|
-
return then(this, adapter || this.query.adapter, resolve, reject);
|
|
3739
|
-
}
|
|
3740
|
-
}
|
|
3741
|
-
const queriesNames = {};
|
|
3742
|
-
let nameI = 0;
|
|
3743
|
-
const then = async (q, adapter, resolve, reject) => {
|
|
3744
|
-
let sql;
|
|
3745
|
-
let logData;
|
|
3746
|
-
const localError = queryError;
|
|
3747
|
-
try {
|
|
3748
|
-
let beforeCallbacks;
|
|
3749
|
-
let afterCallbacks;
|
|
3750
|
-
if (q.query.type === "insert") {
|
|
3751
|
-
beforeCallbacks = q.query.beforeCreate;
|
|
3752
|
-
afterCallbacks = q.query.afterCreate;
|
|
3753
|
-
} else if (q.query.type === "update") {
|
|
3754
|
-
beforeCallbacks = q.query.beforeUpdate;
|
|
3755
|
-
afterCallbacks = q.query.afterUpdate;
|
|
3756
|
-
} else if (q.query.type === "delete") {
|
|
3757
|
-
beforeCallbacks = q.query.beforeDelete;
|
|
3758
|
-
afterCallbacks = q.query.afterDelete;
|
|
3759
|
-
}
|
|
3760
|
-
if (beforeCallbacks || q.query.beforeQuery) {
|
|
3761
|
-
await Promise.all(
|
|
3762
|
-
getCallbacks(beforeCallbacks, q.query.beforeQuery).map((cb) => cb(q))
|
|
3763
|
-
);
|
|
3764
|
-
}
|
|
3765
|
-
sql = q.toSql();
|
|
3766
|
-
if (q.query.autoPreparedStatements) {
|
|
3767
|
-
sql.name = queriesNames[sql.text] || (queriesNames[sql.text] = (nameI++).toString(36));
|
|
3768
|
-
}
|
|
3769
|
-
if (q.query.log) {
|
|
3770
|
-
logData = q.query.log.beforeQuery(sql);
|
|
3771
|
-
}
|
|
3772
|
-
const queryResult = await adapter[queryMethodByReturnType[q.query.returnType || "all"]](sql);
|
|
3773
|
-
if (q.query.log) {
|
|
3774
|
-
q.query.log.afterQuery(sql, logData);
|
|
3775
|
-
sql = void 0;
|
|
3776
|
-
}
|
|
3777
|
-
const result = await q.query.handleResult(q, queryResult);
|
|
3778
|
-
if (afterCallbacks || q.query.afterQuery) {
|
|
3779
|
-
await Promise.all(
|
|
3780
|
-
getCallbacks(q.query.afterQuery, afterCallbacks).map(
|
|
3781
|
-
(query) => query(q, result)
|
|
3782
|
-
)
|
|
3783
|
-
);
|
|
3784
|
-
}
|
|
3785
|
-
resolve == null ? void 0 : resolve(result);
|
|
3786
|
-
} catch (err) {
|
|
3787
|
-
let error;
|
|
3788
|
-
if (err instanceof pg.DatabaseError) {
|
|
3789
|
-
error = new q.error();
|
|
3790
|
-
assignError(error, err);
|
|
3791
|
-
error.cause = localError;
|
|
3792
|
-
} else {
|
|
3793
|
-
error = err;
|
|
3794
|
-
if (error instanceof Error) {
|
|
3795
|
-
error.cause = localError;
|
|
3796
|
-
}
|
|
3797
|
-
}
|
|
3798
|
-
if (q.query.log && sql && logData) {
|
|
3799
|
-
q.query.log.onError(error, sql, logData);
|
|
3800
|
-
}
|
|
3801
|
-
reject == null ? void 0 : reject(error);
|
|
3802
|
-
}
|
|
3803
|
-
};
|
|
3804
|
-
const assignError = (to, from) => {
|
|
3805
|
-
to.message = from.message;
|
|
3806
|
-
to.length = from.length;
|
|
3807
|
-
to.name = from.name;
|
|
3808
|
-
to.severity = from.severity;
|
|
3809
|
-
to.code = from.code;
|
|
3810
|
-
to.detail = from.detail;
|
|
3811
|
-
to.hint = from.hint;
|
|
3812
|
-
to.position = from.position;
|
|
3813
|
-
to.internalPosition = from.internalPosition;
|
|
3814
|
-
to.internalQuery = from.internalQuery;
|
|
3815
|
-
to.where = from.where;
|
|
3816
|
-
to.schema = from.schema;
|
|
3817
|
-
to.table = from.table;
|
|
3818
|
-
to.column = from.column;
|
|
3819
|
-
to.dataType = from.dataType;
|
|
3820
|
-
to.constraint = from.constraint;
|
|
3821
|
-
to.file = from.file;
|
|
3822
|
-
to.line = from.line;
|
|
3823
|
-
to.routine = from.routine;
|
|
3824
|
-
return to;
|
|
3825
|
-
};
|
|
3826
|
-
const parseResult = (q, parsers, returnType = "all", result, isSubQuery) => {
|
|
3827
|
-
var _a, _b;
|
|
3828
|
-
switch (returnType) {
|
|
3829
|
-
case "all": {
|
|
3830
|
-
if (q.query.throwOnNotFound && result.rows.length === 0)
|
|
3831
|
-
throw new NotFoundError(q);
|
|
3832
|
-
return parsers ? result.rows.map((row) => parseRecord(parsers, row)) : result.rows;
|
|
3833
|
-
}
|
|
3834
|
-
case "one": {
|
|
3835
|
-
const row = result.rows[0];
|
|
3836
|
-
if (!row)
|
|
3837
|
-
return;
|
|
3838
|
-
return parsers ? parseRecord(parsers, row) : row;
|
|
3839
|
-
}
|
|
3840
|
-
case "oneOrThrow": {
|
|
3841
|
-
const row = result.rows[0];
|
|
3842
|
-
if (!row)
|
|
3843
|
-
throw new NotFoundError(q);
|
|
3844
|
-
return parsers ? parseRecord(parsers, row) : row;
|
|
3845
|
-
}
|
|
3846
|
-
case "rows": {
|
|
3847
|
-
return parsers ? parseRows(
|
|
3848
|
-
parsers,
|
|
3849
|
-
result.fields,
|
|
3850
|
-
result.rows
|
|
3851
|
-
) : result.rows;
|
|
3852
|
-
}
|
|
3853
|
-
case "pluck": {
|
|
3854
|
-
if (parsers == null ? void 0 : parsers.pluck) {
|
|
3855
|
-
if (isSubQuery) {
|
|
3856
|
-
return result.rows.map((row) => parsers.pluck(row));
|
|
3857
|
-
}
|
|
3858
|
-
return result.rows.map((row) => parsers.pluck(row[0]));
|
|
3859
|
-
} else if (isSubQuery) {
|
|
3860
|
-
return result.rows;
|
|
3861
|
-
}
|
|
3862
|
-
return result.rows.map((row) => row[0]);
|
|
3863
|
-
}
|
|
3864
|
-
case "value": {
|
|
3865
|
-
const value = (_a = result.rows[0]) == null ? void 0 : _a[0];
|
|
3866
|
-
return value !== void 0 ? parseValue(value, parsers) : q.query.notFoundDefault;
|
|
3867
|
-
}
|
|
3868
|
-
case "valueOrThrow": {
|
|
3869
|
-
const value = (_b = result.rows[0]) == null ? void 0 : _b[0];
|
|
3870
|
-
if (value === void 0)
|
|
3871
|
-
throw new NotFoundError(q);
|
|
3872
|
-
return parseValue(value, parsers);
|
|
3873
|
-
}
|
|
3874
|
-
case "rowCount": {
|
|
3875
|
-
if (q.query.throwOnNotFound && result.rowCount === 0) {
|
|
3876
|
-
throw new NotFoundError(q);
|
|
3877
|
-
}
|
|
3878
|
-
return result.rowCount;
|
|
3879
|
-
}
|
|
3880
|
-
case "void": {
|
|
3881
|
-
return;
|
|
3882
|
-
}
|
|
3883
|
-
}
|
|
3884
|
-
};
|
|
3885
|
-
const parseRecord = (parsers, row) => {
|
|
3886
|
-
for (const key in parsers) {
|
|
3887
|
-
if (row[key] !== null && row[key] !== void 0) {
|
|
3888
|
-
row[key] = parsers[key](row[key]);
|
|
3889
|
-
}
|
|
3890
|
-
}
|
|
3891
|
-
return row;
|
|
3892
|
-
};
|
|
3893
|
-
const parseRows = (parsers, fields, rows) => {
|
|
3894
|
-
fields.forEach((field, i) => {
|
|
3895
|
-
const parser = parsers[field.name];
|
|
3896
|
-
if (parser) {
|
|
3897
|
-
rows.forEach((row) => {
|
|
3898
|
-
row[i] = parser(row[i]);
|
|
3899
|
-
});
|
|
3900
|
-
}
|
|
3901
|
-
});
|
|
3902
|
-
return rows;
|
|
3903
|
-
};
|
|
3904
|
-
const parseValue = (value, parsers) => {
|
|
3905
|
-
if (value !== null) {
|
|
3906
|
-
const parser = parsers == null ? void 0 : parsers[getValueKey];
|
|
3907
|
-
if (parser) {
|
|
3908
|
-
return parser(value);
|
|
3909
|
-
}
|
|
3910
|
-
}
|
|
3911
|
-
return value;
|
|
3912
|
-
};
|
|
3913
|
-
const getCallbacks = (first, second) => {
|
|
3914
|
-
return first && second ? [...first, ...second] : first ? first : second;
|
|
3915
|
-
};
|
|
3916
|
-
|
|
3917
3698
|
var __defProp$8 = Object.defineProperty;
|
|
3918
3699
|
var __getOwnPropSymbols$8 = Object.getOwnPropertySymbols;
|
|
3919
3700
|
var __hasOwnProp$8 = Object.prototype.hasOwnProperty;
|
|
@@ -3958,7 +3739,7 @@ const addParserForSelectItem = (q, as, key, arg) => {
|
|
|
3958
3739
|
addParserToQuery(q.query, key, (item) => {
|
|
3959
3740
|
const t = rel.query.returnType || "all";
|
|
3960
3741
|
subQueryResult.rows = t === "value" || t === "valueOrThrow" ? [[item]] : t === "one" || t === "oneOrThrow" ? [item] : item;
|
|
3961
|
-
return
|
|
3742
|
+
return rel.query.handleResult(rel, subQueryResult, true);
|
|
3962
3743
|
});
|
|
3963
3744
|
}
|
|
3964
3745
|
}
|
|
@@ -3999,7 +3780,11 @@ const processSelectArg = (q, as, arg, columnAs) => {
|
|
|
3999
3780
|
if (parsers) {
|
|
4000
3781
|
addParserToQuery(q.query, arg, (item) => {
|
|
4001
3782
|
subQueryResult.rows = [item];
|
|
4002
|
-
|
|
3783
|
+
const type = q.query.returnType;
|
|
3784
|
+
q.query.returnType = "one";
|
|
3785
|
+
const res = q.query.handleResult(q, subQueryResult, true);
|
|
3786
|
+
q.query.returnType = type;
|
|
3787
|
+
return res;
|
|
4003
3788
|
});
|
|
4004
3789
|
}
|
|
4005
3790
|
return arg;
|
|
@@ -4568,7 +4353,7 @@ class ColumnInfoMethods {
|
|
|
4568
4353
|
if (column) {
|
|
4569
4354
|
this.query.column = column;
|
|
4570
4355
|
}
|
|
4571
|
-
this.query.handleResult =
|
|
4356
|
+
this.query.handleResult = (_, result) => {
|
|
4572
4357
|
if (column) {
|
|
4573
4358
|
return rowToColumnInfo(result.rows[0]);
|
|
4574
4359
|
} else {
|
|
@@ -4758,6 +4543,228 @@ class Having {
|
|
|
4758
4543
|
}
|
|
4759
4544
|
}
|
|
4760
4545
|
|
|
4546
|
+
const queryMethodByReturnType = {
|
|
4547
|
+
all: "query",
|
|
4548
|
+
rows: "arrays",
|
|
4549
|
+
pluck: "arrays",
|
|
4550
|
+
one: "query",
|
|
4551
|
+
oneOrThrow: "query",
|
|
4552
|
+
value: "arrays",
|
|
4553
|
+
valueOrThrow: "arrays",
|
|
4554
|
+
rowCount: "arrays",
|
|
4555
|
+
void: "arrays"
|
|
4556
|
+
};
|
|
4557
|
+
let queryError = void 0;
|
|
4558
|
+
class Then {
|
|
4559
|
+
get then() {
|
|
4560
|
+
queryError = new Error();
|
|
4561
|
+
return maybeWrappedThen;
|
|
4562
|
+
}
|
|
4563
|
+
async catch(fn) {
|
|
4564
|
+
return this.then(void 0, fn);
|
|
4565
|
+
}
|
|
4566
|
+
}
|
|
4567
|
+
const handleResult = (q, result, isSubQuery) => {
|
|
4568
|
+
return parseResult(
|
|
4569
|
+
q,
|
|
4570
|
+
q.query.parsers,
|
|
4571
|
+
q.query.returnType || "all",
|
|
4572
|
+
result,
|
|
4573
|
+
isSubQuery
|
|
4574
|
+
);
|
|
4575
|
+
};
|
|
4576
|
+
function maybeWrappedThen(resolve, reject) {
|
|
4577
|
+
const adapter = this.internal.transactionStorage.getStore();
|
|
4578
|
+
if (this.query.wrapInTransaction && !adapter) {
|
|
4579
|
+
return this.transaction(
|
|
4580
|
+
() => new Promise((resolve2, reject2) => {
|
|
4581
|
+
const adapter2 = this.internal.transactionStorage.getStore();
|
|
4582
|
+
return then(this, adapter2, resolve2, reject2);
|
|
4583
|
+
})
|
|
4584
|
+
).then(resolve, reject);
|
|
4585
|
+
} else {
|
|
4586
|
+
return then(this, adapter || this.query.adapter, resolve, reject);
|
|
4587
|
+
}
|
|
4588
|
+
}
|
|
4589
|
+
const queriesNames = {};
|
|
4590
|
+
let nameI = 0;
|
|
4591
|
+
const then = async (q, adapter, resolve, reject) => {
|
|
4592
|
+
let sql;
|
|
4593
|
+
let logData;
|
|
4594
|
+
const localError = queryError;
|
|
4595
|
+
try {
|
|
4596
|
+
let beforeCallbacks;
|
|
4597
|
+
let afterCallbacks;
|
|
4598
|
+
if (q.query.type === "insert") {
|
|
4599
|
+
beforeCallbacks = q.query.beforeCreate;
|
|
4600
|
+
afterCallbacks = q.query.afterCreate;
|
|
4601
|
+
} else if (q.query.type === "update") {
|
|
4602
|
+
beforeCallbacks = q.query.beforeUpdate;
|
|
4603
|
+
afterCallbacks = q.query.afterUpdate;
|
|
4604
|
+
} else if (q.query.type === "delete") {
|
|
4605
|
+
beforeCallbacks = q.query.beforeDelete;
|
|
4606
|
+
afterCallbacks = q.query.afterDelete;
|
|
4607
|
+
}
|
|
4608
|
+
if (beforeCallbacks || q.query.beforeQuery) {
|
|
4609
|
+
await Promise.all(
|
|
4610
|
+
getCallbacks(beforeCallbacks, q.query.beforeQuery).map((cb) => cb(q))
|
|
4611
|
+
);
|
|
4612
|
+
}
|
|
4613
|
+
sql = q.toSql();
|
|
4614
|
+
if (q.query.autoPreparedStatements) {
|
|
4615
|
+
sql.name = queriesNames[sql.text] || (queriesNames[sql.text] = (nameI++).toString(36));
|
|
4616
|
+
}
|
|
4617
|
+
if (q.query.log) {
|
|
4618
|
+
logData = q.query.log.beforeQuery(sql);
|
|
4619
|
+
}
|
|
4620
|
+
const queryResult = await adapter[queryMethodByReturnType[q.query.returnType || "all"]](sql);
|
|
4621
|
+
if (q.query.patchResult) {
|
|
4622
|
+
await q.query.patchResult(queryResult);
|
|
4623
|
+
}
|
|
4624
|
+
if (q.query.log) {
|
|
4625
|
+
q.query.log.afterQuery(sql, logData);
|
|
4626
|
+
sql = void 0;
|
|
4627
|
+
}
|
|
4628
|
+
const result = q.query.handleResult(q, queryResult);
|
|
4629
|
+
if (afterCallbacks || q.query.afterQuery) {
|
|
4630
|
+
await Promise.all(
|
|
4631
|
+
getCallbacks(q.query.afterQuery, afterCallbacks).map(
|
|
4632
|
+
(query) => query(q, result)
|
|
4633
|
+
)
|
|
4634
|
+
);
|
|
4635
|
+
}
|
|
4636
|
+
resolve == null ? void 0 : resolve(result);
|
|
4637
|
+
} catch (err) {
|
|
4638
|
+
let error;
|
|
4639
|
+
if (err instanceof pg.DatabaseError) {
|
|
4640
|
+
error = new q.error();
|
|
4641
|
+
assignError(error, err);
|
|
4642
|
+
error.cause = localError;
|
|
4643
|
+
} else {
|
|
4644
|
+
error = err;
|
|
4645
|
+
if (error instanceof Error) {
|
|
4646
|
+
error.cause = localError;
|
|
4647
|
+
}
|
|
4648
|
+
}
|
|
4649
|
+
if (q.query.log && sql && logData) {
|
|
4650
|
+
q.query.log.onError(error, sql, logData);
|
|
4651
|
+
}
|
|
4652
|
+
reject == null ? void 0 : reject(error);
|
|
4653
|
+
}
|
|
4654
|
+
};
|
|
4655
|
+
const assignError = (to, from) => {
|
|
4656
|
+
to.message = from.message;
|
|
4657
|
+
to.length = from.length;
|
|
4658
|
+
to.name = from.name;
|
|
4659
|
+
to.severity = from.severity;
|
|
4660
|
+
to.code = from.code;
|
|
4661
|
+
to.detail = from.detail;
|
|
4662
|
+
to.hint = from.hint;
|
|
4663
|
+
to.position = from.position;
|
|
4664
|
+
to.internalPosition = from.internalPosition;
|
|
4665
|
+
to.internalQuery = from.internalQuery;
|
|
4666
|
+
to.where = from.where;
|
|
4667
|
+
to.schema = from.schema;
|
|
4668
|
+
to.table = from.table;
|
|
4669
|
+
to.column = from.column;
|
|
4670
|
+
to.dataType = from.dataType;
|
|
4671
|
+
to.constraint = from.constraint;
|
|
4672
|
+
to.file = from.file;
|
|
4673
|
+
to.line = from.line;
|
|
4674
|
+
to.routine = from.routine;
|
|
4675
|
+
return to;
|
|
4676
|
+
};
|
|
4677
|
+
const parseResult = (q, parsers, returnType = "all", result, isSubQuery) => {
|
|
4678
|
+
var _a, _b;
|
|
4679
|
+
switch (returnType) {
|
|
4680
|
+
case "all": {
|
|
4681
|
+
if (q.query.throwOnNotFound && result.rows.length === 0)
|
|
4682
|
+
throw new NotFoundError(q);
|
|
4683
|
+
return parsers ? result.rows.map((row) => parseRecord(parsers, row)) : result.rows;
|
|
4684
|
+
}
|
|
4685
|
+
case "one": {
|
|
4686
|
+
const row = result.rows[0];
|
|
4687
|
+
if (!row)
|
|
4688
|
+
return;
|
|
4689
|
+
return parsers ? parseRecord(parsers, row) : row;
|
|
4690
|
+
}
|
|
4691
|
+
case "oneOrThrow": {
|
|
4692
|
+
const row = result.rows[0];
|
|
4693
|
+
if (!row)
|
|
4694
|
+
throw new NotFoundError(q);
|
|
4695
|
+
return parsers ? parseRecord(parsers, row) : row;
|
|
4696
|
+
}
|
|
4697
|
+
case "rows": {
|
|
4698
|
+
return parsers ? parseRows(
|
|
4699
|
+
parsers,
|
|
4700
|
+
result.fields,
|
|
4701
|
+
result.rows
|
|
4702
|
+
) : result.rows;
|
|
4703
|
+
}
|
|
4704
|
+
case "pluck": {
|
|
4705
|
+
if (parsers == null ? void 0 : parsers.pluck) {
|
|
4706
|
+
if (isSubQuery) {
|
|
4707
|
+
return result.rows.map((row) => parsers.pluck(row));
|
|
4708
|
+
}
|
|
4709
|
+
return result.rows.map((row) => parsers.pluck(row[0]));
|
|
4710
|
+
} else if (isSubQuery) {
|
|
4711
|
+
return result.rows;
|
|
4712
|
+
}
|
|
4713
|
+
return result.rows.map((row) => row[0]);
|
|
4714
|
+
}
|
|
4715
|
+
case "value": {
|
|
4716
|
+
const value = (_a = result.rows[0]) == null ? void 0 : _a[0];
|
|
4717
|
+
return value !== void 0 ? parseValue(value, parsers) : q.query.notFoundDefault;
|
|
4718
|
+
}
|
|
4719
|
+
case "valueOrThrow": {
|
|
4720
|
+
const value = (_b = result.rows[0]) == null ? void 0 : _b[0];
|
|
4721
|
+
if (value === void 0)
|
|
4722
|
+
throw new NotFoundError(q);
|
|
4723
|
+
return parseValue(value, parsers);
|
|
4724
|
+
}
|
|
4725
|
+
case "rowCount": {
|
|
4726
|
+
if (q.query.throwOnNotFound && result.rowCount === 0) {
|
|
4727
|
+
throw new NotFoundError(q);
|
|
4728
|
+
}
|
|
4729
|
+
return result.rowCount;
|
|
4730
|
+
}
|
|
4731
|
+
case "void": {
|
|
4732
|
+
return;
|
|
4733
|
+
}
|
|
4734
|
+
}
|
|
4735
|
+
};
|
|
4736
|
+
const parseRecord = (parsers, row) => {
|
|
4737
|
+
for (const key in parsers) {
|
|
4738
|
+
if (row[key] !== null && row[key] !== void 0) {
|
|
4739
|
+
row[key] = parsers[key](row[key]);
|
|
4740
|
+
}
|
|
4741
|
+
}
|
|
4742
|
+
return row;
|
|
4743
|
+
};
|
|
4744
|
+
const parseRows = (parsers, fields, rows) => {
|
|
4745
|
+
fields.forEach((field, i) => {
|
|
4746
|
+
const parser = parsers[field.name];
|
|
4747
|
+
if (parser) {
|
|
4748
|
+
rows.forEach((row) => {
|
|
4749
|
+
row[i] = parser(row[i]);
|
|
4750
|
+
});
|
|
4751
|
+
}
|
|
4752
|
+
});
|
|
4753
|
+
return rows;
|
|
4754
|
+
};
|
|
4755
|
+
const parseValue = (value, parsers) => {
|
|
4756
|
+
if (value !== null) {
|
|
4757
|
+
const parser = parsers == null ? void 0 : parsers[getValueKey];
|
|
4758
|
+
if (parser) {
|
|
4759
|
+
return parser(value);
|
|
4760
|
+
}
|
|
4761
|
+
}
|
|
4762
|
+
return value;
|
|
4763
|
+
};
|
|
4764
|
+
const getCallbacks = (first, second) => {
|
|
4765
|
+
return first && second ? [...first, ...second] : first ? first : second;
|
|
4766
|
+
};
|
|
4767
|
+
|
|
4761
4768
|
var __defProp$5 = Object.defineProperty;
|
|
4762
4769
|
var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
|
|
4763
4770
|
var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
|
|
@@ -4867,11 +4874,11 @@ const insert = (self, {
|
|
|
4867
4874
|
}
|
|
4868
4875
|
if (returnType === "oneOrThrow" || ((_a = values.from) == null ? void 0 : _a.query.returnType) === "oneOrThrow") {
|
|
4869
4876
|
const { handleResult } = q.query;
|
|
4870
|
-
q.query.handleResult =
|
|
4877
|
+
q.query.handleResult = (q2, r, s) => {
|
|
4871
4878
|
if (r.rowCount === 0) {
|
|
4872
4879
|
throw new NotFoundError(q2);
|
|
4873
4880
|
}
|
|
4874
|
-
return
|
|
4881
|
+
return handleResult(q2, r, s);
|
|
4875
4882
|
};
|
|
4876
4883
|
}
|
|
4877
4884
|
const requiredColumns = Object.keys(ctx.requiredReturning);
|
|
@@ -4887,14 +4894,15 @@ const insert = (self, {
|
|
|
4887
4894
|
if (ctx.returnTypeAll) {
|
|
4888
4895
|
q.query.returnType = "all";
|
|
4889
4896
|
const { handleResult } = q.query;
|
|
4890
|
-
q.query.handleResult =
|
|
4891
|
-
ctx.resultAll =
|
|
4897
|
+
q.query.handleResult = (q2, queryResult, s) => {
|
|
4898
|
+
ctx.resultAll = handleResult(q2, queryResult, s);
|
|
4892
4899
|
if (queryMethodByReturnType[returnType] === "arrays") {
|
|
4893
4900
|
queryResult.rows.forEach(
|
|
4894
|
-
(row,
|
|
4901
|
+
(row, i) => queryResult.rows[i] = Object.values(row)
|
|
4895
4902
|
);
|
|
4896
4903
|
}
|
|
4897
|
-
|
|
4904
|
+
q2.query.returnType = returnType;
|
|
4905
|
+
return handleResult(q2, queryResult, s);
|
|
4898
4906
|
};
|
|
4899
4907
|
} else {
|
|
4900
4908
|
q.query.returnType = returnType;
|
|
@@ -5791,39 +5799,44 @@ class Update {
|
|
|
5791
5799
|
if (!ctx.willSetKeys && checkIfUpdateIsEmpty(query)) {
|
|
5792
5800
|
delete query.type;
|
|
5793
5801
|
}
|
|
5794
|
-
|
|
5795
|
-
|
|
5796
|
-
|
|
5797
|
-
|
|
5798
|
-
|
|
5799
|
-
|
|
5802
|
+
const { queries } = ctx;
|
|
5803
|
+
if (queries || ctx.returnTypeAll) {
|
|
5804
|
+
query.returnType = "all";
|
|
5805
|
+
if (queries) {
|
|
5806
|
+
if (!((_a = query.select) == null ? void 0 : _a.includes("*"))) {
|
|
5807
|
+
this.primaryKeys.forEach((key) => {
|
|
5808
|
+
var _a2;
|
|
5809
|
+
if (!((_a2 = query.select) == null ? void 0 : _a2.includes(key))) {
|
|
5810
|
+
this._select(key);
|
|
5811
|
+
}
|
|
5812
|
+
});
|
|
5813
|
+
}
|
|
5814
|
+
query.patchResult = async (queryResult) => {
|
|
5815
|
+
await Promise.all(queries.map((fn) => fn(queryResult)));
|
|
5816
|
+
if (ctx.updateData) {
|
|
5817
|
+
const t = this.baseQuery.clone();
|
|
5818
|
+
const keys = this.primaryKeys;
|
|
5819
|
+
t._whereIn(
|
|
5820
|
+
keys,
|
|
5821
|
+
queryResult.rows.map((item) => keys.map((key) => item[key]))
|
|
5822
|
+
);
|
|
5823
|
+
await t._update(ctx.updateData);
|
|
5824
|
+
for (const row of queryResult.rows) {
|
|
5825
|
+
Object.assign(row, ctx.updateData);
|
|
5826
|
+
}
|
|
5800
5827
|
}
|
|
5801
|
-
}
|
|
5828
|
+
};
|
|
5802
5829
|
}
|
|
5803
|
-
}
|
|
5804
|
-
if (ctx.updateLater || ctx.returnTypeAll) {
|
|
5805
|
-
query.returnType = "all";
|
|
5806
5830
|
const { handleResult } = query;
|
|
5807
|
-
query.handleResult =
|
|
5808
|
-
ctx.resultAll =
|
|
5809
|
-
if (ctx.updateLater) {
|
|
5810
|
-
await Promise.all(ctx.updateLaterPromises);
|
|
5811
|
-
const t = this.baseQuery.clone();
|
|
5812
|
-
const keys = this.primaryKeys;
|
|
5813
|
-
t._whereIn(
|
|
5814
|
-
keys,
|
|
5815
|
-
ctx.resultAll.map((item) => keys.map((key) => item[key]))
|
|
5816
|
-
);
|
|
5817
|
-
await t._update(ctx.updateLater);
|
|
5818
|
-
ctx.resultAll.forEach((item) => Object.assign(item, ctx.updateLater));
|
|
5819
|
-
}
|
|
5831
|
+
query.handleResult = (q, queryResult, s) => {
|
|
5832
|
+
ctx.resultAll = handleResult(q, queryResult);
|
|
5820
5833
|
if (queryMethodByReturnType[originalReturnType] === "arrays") {
|
|
5821
5834
|
queryResult.rows.forEach(
|
|
5822
|
-
(row,
|
|
5835
|
+
(row, i) => queryResult.rows[i] = Object.values(row)
|
|
5823
5836
|
);
|
|
5824
5837
|
}
|
|
5825
5838
|
q.query.returnType = originalReturnType;
|
|
5826
|
-
return
|
|
5839
|
+
return handleResult(q, queryResult, s);
|
|
5827
5840
|
};
|
|
5828
5841
|
}
|
|
5829
5842
|
return update(this);
|
|
@@ -5969,16 +5982,29 @@ class QueryUpsertOrCreate {
|
|
|
5969
5982
|
this.query.returnType = "one";
|
|
5970
5983
|
this.query.wrapInTransaction = true;
|
|
5971
5984
|
const { handleResult } = this.query;
|
|
5972
|
-
|
|
5985
|
+
let result;
|
|
5986
|
+
let created = false;
|
|
5987
|
+
this.query.handleResult = (q, r, s) => {
|
|
5988
|
+
return created ? result : handleResult(q, r, s);
|
|
5989
|
+
};
|
|
5990
|
+
this.query.patchResult = async (queryResult) => {
|
|
5973
5991
|
if (queryResult.rowCount === 0) {
|
|
5974
|
-
|
|
5992
|
+
const inner = this.create(data);
|
|
5993
|
+
const { handleResult: handleResult2 } = inner.query;
|
|
5994
|
+
inner.query.handleResult = (q, r, s) => {
|
|
5995
|
+
queryResult = r;
|
|
5996
|
+
const res = handleResult2(q, r, s);
|
|
5997
|
+
result = res;
|
|
5998
|
+
return res;
|
|
5999
|
+
};
|
|
6000
|
+
await inner;
|
|
6001
|
+
created = true;
|
|
5975
6002
|
} else if (queryResult.rowCount > 1) {
|
|
5976
6003
|
throw new MoreThanOneRowError(
|
|
5977
|
-
|
|
6004
|
+
this,
|
|
5978
6005
|
`Only one row was expected to find, found ${queryResult.rowCount} rows.`
|
|
5979
6006
|
);
|
|
5980
6007
|
}
|
|
5981
|
-
return handleResult(q, queryResult, i);
|
|
5982
6008
|
};
|
|
5983
6009
|
return this;
|
|
5984
6010
|
}
|