pqb 0.56.9 → 0.56.12
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 +113 -42
- package/dist/index.js +422 -226
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +423 -228
- package/dist/index.mjs.map +1 -1
- package/dist/postgres-js.js +3 -0
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs +3 -0
- package/dist/postgres-js.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExpressionTypeMethod, Expression, RawSQLBase, emptyObject, isTemplateLiteralArgs, ColumnTypeBase, setColumnData, pushColumnData, templateLiteralSQLToCode, quoteObjectKey, toArray, emptyArray, singleQuote, addCode, singleQuoteArray, objectHasValues, toSnakeCase, columnDefaultArgumentToCode, columnErrorMessagesToCode, setObjectValueImmutable, getValueKey, addValue, isIterable, isExpression, dateDataToCode, joinTruthy, arrayDataToCode, numberDataToCode, noop, stringDataToCode, getDefaultLanguage, setDefaultNowFn, setDefaultLanguage, setCurrentColumnName, timestampHelpers, _getQueryAliasOrName, _getQueryOuterAliases, returnArg, pushQueryValueImmutable, NotFoundError, OrchidOrmInternalError, _setSubQueryAliases, _applyRelationAliases, isRelationQuery, getFreeAlias, _checkIfAliased, logColors, OrchidOrmError,
|
|
1
|
+
import { ExpressionTypeMethod, Expression, RawSQLBase, emptyObject, isTemplateLiteralArgs, ColumnTypeBase, setColumnData, pushColumnData, templateLiteralSQLToCode, quoteObjectKey, toArray, emptyArray, singleQuote, addCode, singleQuoteArray, objectHasValues, toSnakeCase, columnDefaultArgumentToCode, columnErrorMessagesToCode, setObjectValueImmutable, getValueKey, addValue, isIterable, isExpression, dateDataToCode, joinTruthy, arrayDataToCode, numberDataToCode, noop, stringDataToCode, getDefaultLanguage, setDefaultNowFn, setDefaultLanguage, setCurrentColumnName, timestampHelpers, _getQueryAliasOrName, _getQueryOuterAliases, returnArg, pushQueryValueImmutable, NotFoundError, applyTransforms, OrchidOrmInternalError, _setSubQueryAliases, _applyRelationAliases, isRelationQuery, getFreeAlias, _checkIfAliased, getQueryParsers, logColors, OrchidOrmError, callWithThis, requirePrimaryKeys, _setQueryAs, _copyQueryAliasToQuery, setParserToQuery, _addToHookSelectWithTable, spreadObjectValues, _addToHookSelect, newDelayedRelationSelect, pushOrNewArray, getPrimaryKeys, setDelayedRelation, UnhandledTypeError, isRawSQL, pushOrNewArrayToObjectImmutable, _getQueryFreeAlias, _setQueryAlias, QueryHookUtils, MoreThanOneRowError, isObjectEmpty, ValExpression, applyMixins, _getQueryAs, QueryError, snakeCaseKey } from 'orchid-core';
|
|
2
2
|
import { inspect } from 'node:util';
|
|
3
3
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
4
|
import { templateLiteralToSQL as templateLiteralToSQL$1 } from 'pqb';
|
|
@@ -1078,6 +1078,9 @@ const quoteValue = (arg, ctx, quotedAs, IN) => {
|
|
|
1078
1078
|
if ("toSQL" in arg) {
|
|
1079
1079
|
return `(${getSqlText(arg.toSQL(ctx))})`;
|
|
1080
1080
|
}
|
|
1081
|
+
if (!(arg instanceof Date) && !Array.isArray(arg)) {
|
|
1082
|
+
arg = JSON.stringify(arg);
|
|
1083
|
+
}
|
|
1081
1084
|
}
|
|
1082
1085
|
return addValue(ctx.values, arg);
|
|
1083
1086
|
};
|
|
@@ -1764,6 +1767,9 @@ const defaultSchemaConfig = {
|
|
|
1764
1767
|
narrowType() {
|
|
1765
1768
|
return this;
|
|
1766
1769
|
},
|
|
1770
|
+
narrowAllTypes() {
|
|
1771
|
+
return this;
|
|
1772
|
+
},
|
|
1767
1773
|
dateAsNumber() {
|
|
1768
1774
|
return this.parse(Date.parse);
|
|
1769
1775
|
},
|
|
@@ -2589,7 +2595,7 @@ const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
|
|
|
2589
2595
|
);
|
|
2590
2596
|
}
|
|
2591
2597
|
if (!select && data.joinedShapes?.[column]) {
|
|
2592
|
-
return `"${column}".
|
|
2598
|
+
return `"${column}"."${column}"`;
|
|
2593
2599
|
}
|
|
2594
2600
|
return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
|
|
2595
2601
|
};
|
|
@@ -2599,7 +2605,7 @@ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
|
|
|
2599
2605
|
return columnWithDotToSql(ctx, data, data.shape, column, index, quotedAs);
|
|
2600
2606
|
} else {
|
|
2601
2607
|
if (data.joinedShapes?.[column]) {
|
|
2602
|
-
return `"${column}".
|
|
2608
|
+
return `"${column}"."${column}"`;
|
|
2603
2609
|
}
|
|
2604
2610
|
if (data.select) {
|
|
2605
2611
|
for (const s of data.select) {
|
|
@@ -2904,7 +2910,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2904
2910
|
quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
|
|
2905
2911
|
} else {
|
|
2906
2912
|
column = query.joinedShapes?.[key]?.value;
|
|
2907
|
-
quotedColumn = `"${key}".
|
|
2913
|
+
quotedColumn = `"${key}"."${key}"`;
|
|
2908
2914
|
}
|
|
2909
2915
|
if (!column || !quotedColumn) {
|
|
2910
2916
|
throw new Error(`Unknown column ${key} provided to condition`);
|
|
@@ -2969,6 +2975,7 @@ const pushIn = (ctx, query, ands, quotedAs, arg) => {
|
|
|
2969
2975
|
const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
2970
2976
|
let target;
|
|
2971
2977
|
let on;
|
|
2978
|
+
const forbidLateral = "u" in args;
|
|
2972
2979
|
if ("l" in args) {
|
|
2973
2980
|
const { aliasValue } = ctx;
|
|
2974
2981
|
ctx.aliasValue = true;
|
|
@@ -2976,7 +2983,7 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
|
2976
2983
|
query,
|
|
2977
2984
|
args.a
|
|
2978
2985
|
)}"`;
|
|
2979
|
-
on = `${args.i ? `"${args.a}".
|
|
2986
|
+
on = `${args.i ? `"${args.a}"."${args.a}" IS NOT NULL` : "true"}`;
|
|
2980
2987
|
ctx.aliasValue = aliasValue;
|
|
2981
2988
|
} else if ("j" in args) {
|
|
2982
2989
|
const { j, s, r } = args;
|
|
@@ -2989,7 +2996,7 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
|
2989
2996
|
target += ` ${joinAs}`;
|
|
2990
2997
|
}
|
|
2991
2998
|
if (r && s) {
|
|
2992
|
-
target =
|
|
2999
|
+
target = subJoinToSql(ctx, j, quotedTable, !forbidLateral, joinAs, true);
|
|
2993
3000
|
} else {
|
|
2994
3001
|
on = whereToSql(ctx, j, j.q, joinAs);
|
|
2995
3002
|
}
|
|
@@ -2999,7 +3006,7 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
|
2999
3006
|
if ("r" in args) {
|
|
3000
3007
|
const { s, r } = args;
|
|
3001
3008
|
if (s) {
|
|
3002
|
-
target =
|
|
3009
|
+
target = subJoinToSql(ctx, r, target, !forbidLateral, target);
|
|
3003
3010
|
} else {
|
|
3004
3011
|
on = whereToSql(ctx, r, r.q, target);
|
|
3005
3012
|
}
|
|
@@ -3030,14 +3037,14 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
|
3030
3037
|
let joinAs;
|
|
3031
3038
|
if ("r" in args) {
|
|
3032
3039
|
const { r } = args;
|
|
3033
|
-
const res = getArgQueryTarget(ctx, q, s, s);
|
|
3034
|
-
target =
|
|
3040
|
+
const res = getArgQueryTarget(ctx, q, s && !forbidLateral, s, s);
|
|
3041
|
+
target = res.target;
|
|
3035
3042
|
joinAs = res.joinAs;
|
|
3036
|
-
if (!s) {
|
|
3043
|
+
if (!s || forbidLateral) {
|
|
3037
3044
|
on = whereToSql(ctx, r, r.q, `"${getQueryAs(r)}"`);
|
|
3038
3045
|
}
|
|
3039
3046
|
} else {
|
|
3040
|
-
const res = getArgQueryTarget(ctx, q, s);
|
|
3047
|
+
const res = getArgQueryTarget(ctx, q, false, s);
|
|
3041
3048
|
target = res.target;
|
|
3042
3049
|
joinAs = res.joinAs;
|
|
3043
3050
|
if ("a" in args) {
|
|
@@ -3066,7 +3073,7 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
|
3066
3073
|
}
|
|
3067
3074
|
return { target, on };
|
|
3068
3075
|
};
|
|
3069
|
-
const getArgQueryTarget = (ctx, first, joinSubQuery, cloned) => {
|
|
3076
|
+
const getArgQueryTarget = (ctx, first, lateral, joinSubQuery, cloned) => {
|
|
3070
3077
|
const joinQuery = first.q;
|
|
3071
3078
|
const quotedFrom = typeof joinQuery.from === "string" ? `"${joinQuery.from}"` : void 0;
|
|
3072
3079
|
let joinAs = quotedFrom || `"${first.table}"`;
|
|
@@ -3074,7 +3081,7 @@ const getArgQueryTarget = (ctx, first, joinSubQuery, cloned) => {
|
|
|
3074
3081
|
const addAs = qAs && qAs !== joinAs;
|
|
3075
3082
|
if (joinSubQuery) {
|
|
3076
3083
|
return {
|
|
3077
|
-
target: subJoinToSql(ctx, first, joinAs, qAs, cloned),
|
|
3084
|
+
target: subJoinToSql(ctx, first, joinAs, lateral, qAs, cloned),
|
|
3078
3085
|
joinAs: addAs ? qAs : joinAs
|
|
3079
3086
|
};
|
|
3080
3087
|
} else {
|
|
@@ -3086,12 +3093,13 @@ const getArgQueryTarget = (ctx, first, joinSubQuery, cloned) => {
|
|
|
3086
3093
|
return { target, joinAs };
|
|
3087
3094
|
}
|
|
3088
3095
|
};
|
|
3089
|
-
const subJoinToSql = (ctx, jq, innerAs, outerAs, cloned) => {
|
|
3096
|
+
const subJoinToSql = (ctx, jq, innerAs, lateral, outerAs, cloned) => {
|
|
3090
3097
|
if (!jq.q.select && jq.q.selectAllColumns) {
|
|
3091
3098
|
if (!cloned) jq = jq.clone();
|
|
3092
3099
|
jq.q.select = [new RawSQL(`${innerAs}.*`)];
|
|
3093
3100
|
}
|
|
3094
|
-
|
|
3101
|
+
const sql = `(${getSqlText(jq.toSQL(ctx))}) ${outerAs || innerAs}`;
|
|
3102
|
+
return lateral ? `LATERAL ${sql}` : sql;
|
|
3095
3103
|
};
|
|
3096
3104
|
const processArgs = (args, ctx, query, joinAs, joinShape, quotedAs) => {
|
|
3097
3105
|
return args.length ? args.length === 1 ? getObjectOrRawConditions(
|
|
@@ -3175,7 +3183,26 @@ const skipQueryKeysForSubQuery = {
|
|
|
3175
3183
|
returnsOne: true,
|
|
3176
3184
|
aliases: true,
|
|
3177
3185
|
defaults: true,
|
|
3178
|
-
transform: true
|
|
3186
|
+
transform: true,
|
|
3187
|
+
throwOnNotFound: true,
|
|
3188
|
+
before: true,
|
|
3189
|
+
after: true,
|
|
3190
|
+
beforeCreate: true,
|
|
3191
|
+
afterCreate: true,
|
|
3192
|
+
afterCreateCommit: true,
|
|
3193
|
+
afterCreateSelect: true,
|
|
3194
|
+
beforeUpdate: true,
|
|
3195
|
+
afterUpdate: true,
|
|
3196
|
+
afterUpdateCommit: true,
|
|
3197
|
+
afterUpdateSelect: true,
|
|
3198
|
+
beforeDelete: true,
|
|
3199
|
+
afterDelete: true,
|
|
3200
|
+
afterDeleteCommit: true,
|
|
3201
|
+
afterDeleteSelect: true,
|
|
3202
|
+
catchAfterCommitErrors: true,
|
|
3203
|
+
log: true,
|
|
3204
|
+
logger: true,
|
|
3205
|
+
autoPreparedStatements: true
|
|
3179
3206
|
};
|
|
3180
3207
|
const getIsJoinSubQuery = (query) => {
|
|
3181
3208
|
const {
|
|
@@ -3190,14 +3217,14 @@ const getIsJoinSubQuery = (query) => {
|
|
|
3190
3217
|
return false;
|
|
3191
3218
|
};
|
|
3192
3219
|
|
|
3193
|
-
const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
|
|
3220
|
+
const processJoinArgs = (joinTo, first, args, joinSubQuery, shape, whereExists, forbidLateral) => {
|
|
3194
3221
|
if (typeof first === "string") {
|
|
3195
3222
|
if (first in joinTo.relations) {
|
|
3196
3223
|
const { query: toQuery, joinQuery } = joinTo.relations[first];
|
|
3197
3224
|
const j = joinQuery(toQuery, joinTo);
|
|
3198
3225
|
if (typeof args[0] === "function") {
|
|
3199
3226
|
const r = args[0](
|
|
3200
|
-
makeJoinQueryBuilder(j, j.q.joinedShapes, joinTo)
|
|
3227
|
+
makeJoinQueryBuilder(j, j.q.joinedShapes, joinTo, shape)
|
|
3201
3228
|
);
|
|
3202
3229
|
return {
|
|
3203
3230
|
j: j.merge(r),
|
|
@@ -3218,7 +3245,7 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
|
|
|
3218
3245
|
j.table = first;
|
|
3219
3246
|
j.q = {
|
|
3220
3247
|
shape: w.shape,
|
|
3221
|
-
|
|
3248
|
+
runtimeComputeds: w.computeds,
|
|
3222
3249
|
adapter: joinToQ.adapter,
|
|
3223
3250
|
handleResult: joinToQ.handleResult,
|
|
3224
3251
|
returnType: "all",
|
|
@@ -3236,7 +3263,8 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
|
|
|
3236
3263
|
...j.q.joinedShapes,
|
|
3237
3264
|
...joinedShapes
|
|
3238
3265
|
} : joinedShapes,
|
|
3239
|
-
joinTo
|
|
3266
|
+
joinTo,
|
|
3267
|
+
shape
|
|
3240
3268
|
)
|
|
3241
3269
|
);
|
|
3242
3270
|
return {
|
|
@@ -3248,7 +3276,7 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
|
|
|
3248
3276
|
}
|
|
3249
3277
|
const args0 = args.length ? args[0] : returnArg;
|
|
3250
3278
|
if (typeof args0 === "function") {
|
|
3251
|
-
|
|
3279
|
+
let q = first;
|
|
3252
3280
|
if (q.joinQueryAfterCallback) {
|
|
3253
3281
|
let base = q.baseQuery;
|
|
3254
3282
|
if (q.q.as) {
|
|
@@ -3258,14 +3286,17 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
|
|
|
3258
3286
|
base,
|
|
3259
3287
|
joinTo
|
|
3260
3288
|
);
|
|
3261
|
-
if (query.and) {
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3289
|
+
if (query.and || query.or || query.scopes) {
|
|
3290
|
+
q = _clone(q);
|
|
3291
|
+
if (query.and) {
|
|
3292
|
+
pushQueryArrayImmutable(q, "and", query.and);
|
|
3293
|
+
}
|
|
3294
|
+
if (query.or) {
|
|
3295
|
+
pushQueryArrayImmutable(q, "or", query.or);
|
|
3296
|
+
}
|
|
3297
|
+
if (query.scopes) {
|
|
3298
|
+
q.q.scopes = { ...q.q.scopes, ...query.scopes };
|
|
3299
|
+
}
|
|
3269
3300
|
}
|
|
3270
3301
|
}
|
|
3271
3302
|
const joinedShapes = {
|
|
@@ -3279,11 +3310,16 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
|
|
|
3279
3310
|
...q.q.joinedShapes,
|
|
3280
3311
|
...joinedShapes
|
|
3281
3312
|
} : joinedShapes,
|
|
3282
|
-
joinTo
|
|
3313
|
+
joinTo,
|
|
3314
|
+
shape
|
|
3283
3315
|
)
|
|
3284
3316
|
);
|
|
3285
3317
|
joinSubQuery || (joinSubQuery = getIsJoinSubQuery(r));
|
|
3286
|
-
return {
|
|
3318
|
+
return {
|
|
3319
|
+
q: joinSubQuery && !forbidLateral ? q.merge(r) : q,
|
|
3320
|
+
r,
|
|
3321
|
+
s: joinSubQuery
|
|
3322
|
+
};
|
|
3287
3323
|
}
|
|
3288
3324
|
return {
|
|
3289
3325
|
q: first,
|
|
@@ -3299,12 +3335,15 @@ const preprocessJoinArg = (q, arg) => {
|
|
|
3299
3335
|
arg.joinQueryAfterCallback = arg.joinQuery;
|
|
3300
3336
|
return arg;
|
|
3301
3337
|
};
|
|
3302
|
-
const makeJoinQueryBuilder = (joinedQuery, joinedShapes, joinTo) => {
|
|
3338
|
+
const makeJoinQueryBuilder = (joinedQuery, joinedShapes, joinTo, shape) => {
|
|
3303
3339
|
const q = joinedQuery.baseQuery.clone();
|
|
3304
3340
|
q.baseQuery = q;
|
|
3305
3341
|
q.q.as = joinedQuery.q.as;
|
|
3306
3342
|
q.q.joinedShapes = joinedShapes;
|
|
3307
3343
|
q.q.joinTo = joinTo;
|
|
3344
|
+
if (shape) {
|
|
3345
|
+
q.q.shape = shape;
|
|
3346
|
+
}
|
|
3308
3347
|
return q;
|
|
3309
3348
|
};
|
|
3310
3349
|
|
|
@@ -3324,14 +3363,23 @@ const noneMethods = {
|
|
|
3324
3363
|
// It is `async` so it returns a chainable Promise.
|
|
3325
3364
|
async then(resolve, reject) {
|
|
3326
3365
|
try {
|
|
3327
|
-
|
|
3328
|
-
|
|
3366
|
+
let result = noneResult(this, this.q, this.q.returnType);
|
|
3367
|
+
if (this.q.transform) {
|
|
3368
|
+
result = applyTransforms(
|
|
3369
|
+
this.q,
|
|
3370
|
+
this.q.returnType,
|
|
3371
|
+
this.q.transform,
|
|
3372
|
+
result
|
|
3373
|
+
);
|
|
3374
|
+
}
|
|
3375
|
+
return resolve?.(result);
|
|
3329
3376
|
} catch (err) {
|
|
3330
|
-
reject?.(err);
|
|
3377
|
+
return reject?.(err);
|
|
3331
3378
|
}
|
|
3332
3379
|
},
|
|
3333
|
-
|
|
3334
|
-
|
|
3380
|
+
catch(reject) {
|
|
3381
|
+
return this.then(void 0, reject);
|
|
3382
|
+
}
|
|
3335
3383
|
};
|
|
3336
3384
|
const _queryNone = (q) => {
|
|
3337
3385
|
if (isQueryNone(q)) return q;
|
|
@@ -3478,6 +3526,7 @@ const existsArgs = (self, q, args) => {
|
|
|
3478
3526
|
preprocessJoinArg(self, q),
|
|
3479
3527
|
args,
|
|
3480
3528
|
false,
|
|
3529
|
+
void 0,
|
|
3481
3530
|
true
|
|
3482
3531
|
);
|
|
3483
3532
|
return [
|
|
@@ -4318,7 +4367,7 @@ const getFullColumnTable = (q, column, index, as) => {
|
|
|
4318
4367
|
return as && table !== as && _checkIfAliased(q, table, as) ? as : table;
|
|
4319
4368
|
};
|
|
4320
4369
|
|
|
4321
|
-
const
|
|
4370
|
+
const _joinReturningArgs = (query, require, first, args, forbidLateral) => {
|
|
4322
4371
|
let joinKey;
|
|
4323
4372
|
let shape;
|
|
4324
4373
|
let parsers;
|
|
@@ -4333,7 +4382,7 @@ const _join = (query, require, type, first, args) => {
|
|
|
4333
4382
|
first = first._internalJoin;
|
|
4334
4383
|
}
|
|
4335
4384
|
if (require && isQueryNone(first)) {
|
|
4336
|
-
return
|
|
4385
|
+
return;
|
|
4337
4386
|
}
|
|
4338
4387
|
const q = first;
|
|
4339
4388
|
if (!isInternalJoin) {
|
|
@@ -4342,9 +4391,9 @@ const _join = (query, require, type, first, args) => {
|
|
|
4342
4391
|
joinKey = q.q.as || q.table;
|
|
4343
4392
|
if (joinKey) {
|
|
4344
4393
|
shape = getShapeFromSelect(q, joinSubQuery && !!q.q.select);
|
|
4345
|
-
parsers = q
|
|
4394
|
+
parsers = getQueryParsers(q);
|
|
4346
4395
|
batchParsers = q.q.batchParsers;
|
|
4347
|
-
computeds = q.q.
|
|
4396
|
+
computeds = q.q.runtimeComputeds;
|
|
4348
4397
|
if (joinSubQuery) {
|
|
4349
4398
|
first = q.clone();
|
|
4350
4399
|
first.shape = shape;
|
|
@@ -4356,9 +4405,9 @@ const _join = (query, require, type, first, args) => {
|
|
|
4356
4405
|
if (relation) {
|
|
4357
4406
|
shape = getShapeFromSelect(relation.query);
|
|
4358
4407
|
const r = relation.query;
|
|
4359
|
-
parsers = r
|
|
4408
|
+
parsers = getQueryParsers(r);
|
|
4360
4409
|
batchParsers = r.q.batchParsers;
|
|
4361
|
-
computeds = r.q.
|
|
4410
|
+
computeds = r.q.runtimeComputeds;
|
|
4362
4411
|
} else {
|
|
4363
4412
|
const w = query.q.withShapes?.[joinKey];
|
|
4364
4413
|
shape = w?.shape;
|
|
@@ -4376,18 +4425,23 @@ const _join = (query, require, type, first, args) => {
|
|
|
4376
4425
|
query,
|
|
4377
4426
|
first,
|
|
4378
4427
|
args,
|
|
4379
|
-
joinSubQuery
|
|
4428
|
+
joinSubQuery,
|
|
4429
|
+
shape,
|
|
4430
|
+
false,
|
|
4431
|
+
forbidLateral
|
|
4380
4432
|
);
|
|
4381
4433
|
if (require && "r" in joinArgs && isQueryNone(joinArgs.r)) {
|
|
4382
|
-
return
|
|
4434
|
+
return;
|
|
4383
4435
|
} else if (joinKey && "s" in joinArgs && joinArgs.s) {
|
|
4384
4436
|
const j = "j" in joinArgs ? joinArgs.r ?? joinArgs.j : "r" in joinArgs ? joinArgs.r : joinArgs.q;
|
|
4385
4437
|
const jq = j.q;
|
|
4386
4438
|
if (jq.select || !jq.selectAllColumns) {
|
|
4387
4439
|
const { q } = query;
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
|
|
4440
|
+
if ("r" in joinArgs && joinArgs.r) {
|
|
4441
|
+
joinArgs.c = shape = getShapeFromSelect(j, true);
|
|
4442
|
+
}
|
|
4443
|
+
setObjectValueImmutable(q, "joinedShapes", joinKey, shape);
|
|
4444
|
+
setObjectValueImmutable(q, "joinedParsers", joinKey, getQueryParsers(j));
|
|
4391
4445
|
if (jq.batchParsers) {
|
|
4392
4446
|
setObjectValueImmutable(
|
|
4393
4447
|
jq,
|
|
@@ -4396,7 +4450,12 @@ const _join = (query, require, type, first, args) => {
|
|
|
4396
4450
|
jq.batchParsers
|
|
4397
4451
|
);
|
|
4398
4452
|
}
|
|
4399
|
-
setObjectValueImmutable(
|
|
4453
|
+
setObjectValueImmutable(
|
|
4454
|
+
q,
|
|
4455
|
+
"joinedComputeds",
|
|
4456
|
+
joinKey,
|
|
4457
|
+
jq.runtimeComputeds
|
|
4458
|
+
);
|
|
4400
4459
|
} else {
|
|
4401
4460
|
addAllShapesAndParsers(
|
|
4402
4461
|
query,
|
|
@@ -4417,6 +4476,13 @@ const _join = (query, require, type, first, args) => {
|
|
|
4417
4476
|
computeds
|
|
4418
4477
|
);
|
|
4419
4478
|
}
|
|
4479
|
+
return joinArgs;
|
|
4480
|
+
};
|
|
4481
|
+
const _join = (query, require, type, first, args) => {
|
|
4482
|
+
const joinArgs = _joinReturningArgs(query, require, first, args);
|
|
4483
|
+
if (!joinArgs) {
|
|
4484
|
+
return _queryNone(query);
|
|
4485
|
+
}
|
|
4420
4486
|
pushQueryValueImmutable(query, "join", {
|
|
4421
4487
|
type,
|
|
4422
4488
|
args: joinArgs
|
|
@@ -4482,7 +4548,12 @@ const _joinLateral = (self, type, arg, as, innerJoinLateral) => {
|
|
|
4482
4548
|
if (joinKey) {
|
|
4483
4549
|
const shape = getShapeFromSelect(arg, true);
|
|
4484
4550
|
setObjectValueImmutable(q.q, "joinedShapes", joinKey, shape);
|
|
4485
|
-
setObjectValueImmutable(
|
|
4551
|
+
setObjectValueImmutable(
|
|
4552
|
+
q.q,
|
|
4553
|
+
"joinedParsers",
|
|
4554
|
+
joinKey,
|
|
4555
|
+
getQueryParsers(arg)
|
|
4556
|
+
);
|
|
4486
4557
|
if (arg.q.batchParsers) {
|
|
4487
4558
|
setObjectValueImmutable(
|
|
4488
4559
|
q.q,
|
|
@@ -4493,12 +4564,39 @@ const _joinLateral = (self, type, arg, as, innerJoinLateral) => {
|
|
|
4493
4564
|
}
|
|
4494
4565
|
}
|
|
4495
4566
|
as || (as = getQueryAs(arg));
|
|
4496
|
-
setObjectValueImmutable(q.q, "joinedComputeds", as, arg.q.
|
|
4567
|
+
setObjectValueImmutable(q.q, "joinedComputeds", as, arg.q.runtimeComputeds);
|
|
4568
|
+
const joinArgs = {
|
|
4569
|
+
l: arg,
|
|
4570
|
+
a: as,
|
|
4571
|
+
i: innerJoinLateral
|
|
4572
|
+
};
|
|
4573
|
+
if (arg.q.returnType === "value" || arg.q.returnType === "valueOrThrow") {
|
|
4574
|
+
const map = q.q.joinValueDedup ? new Map(q.q.joinValueDedup) : /* @__PURE__ */ new Map();
|
|
4575
|
+
q.q.joinValueDedup = map;
|
|
4576
|
+
const select = arg.q.select[0];
|
|
4577
|
+
arg.q.select = [];
|
|
4578
|
+
const dedupKey = getSqlText(arg.toSQL());
|
|
4579
|
+
const existing = map.get(dedupKey);
|
|
4580
|
+
if (existing) {
|
|
4581
|
+
existing.q.q.select = [
|
|
4582
|
+
{
|
|
4583
|
+
selectAs: {
|
|
4584
|
+
...existing.q.q.select[0].selectAs,
|
|
4585
|
+
[joinKey]: select
|
|
4586
|
+
}
|
|
4587
|
+
}
|
|
4588
|
+
];
|
|
4589
|
+
return existing.a;
|
|
4590
|
+
} else {
|
|
4591
|
+
arg.q.select = [{ selectAs: { [joinKey]: select } }];
|
|
4592
|
+
map.set(dedupKey, { q: arg, a: as });
|
|
4593
|
+
}
|
|
4594
|
+
}
|
|
4497
4595
|
pushQueryValueImmutable(q, "join", {
|
|
4498
4596
|
type: `${type} LATERAL`,
|
|
4499
|
-
args:
|
|
4597
|
+
args: joinArgs
|
|
4500
4598
|
});
|
|
4501
|
-
return
|
|
4599
|
+
return joinKey;
|
|
4502
4600
|
};
|
|
4503
4601
|
|
|
4504
4602
|
const escape = (value, migration, nested) => {
|
|
@@ -4878,6 +4976,7 @@ class ComputedColumn {
|
|
|
4878
4976
|
const computeAtRuntime = (deps, fn) => new ComputedColumn("one", deps, fn);
|
|
4879
4977
|
const computeBatchAtRuntime = (deps, fn) => new ComputedColumn("many", deps, fn);
|
|
4880
4978
|
const applyComputedColumns = (q, fn) => {
|
|
4979
|
+
var _a;
|
|
4881
4980
|
q.computeAtRuntime = computeAtRuntime;
|
|
4882
4981
|
q.computeBatchAtRuntime = computeBatchAtRuntime;
|
|
4883
4982
|
const computed = fn(q);
|
|
@@ -4885,8 +4984,8 @@ const applyComputedColumns = (q, fn) => {
|
|
|
4885
4984
|
let item = computed[key];
|
|
4886
4985
|
if (typeof item === "function") item = item.call(computed);
|
|
4887
4986
|
if (item instanceof ComputedColumn) {
|
|
4888
|
-
q.q.
|
|
4889
|
-
...q.q.
|
|
4987
|
+
q.q.runtimeComputeds = {
|
|
4988
|
+
...q.q.runtimeComputeds,
|
|
4890
4989
|
[key]: item
|
|
4891
4990
|
};
|
|
4892
4991
|
} else {
|
|
@@ -4902,11 +5001,10 @@ const applyComputedColumns = (q, fn) => {
|
|
|
4902
5001
|
data.computed = item;
|
|
4903
5002
|
data.explicitSelect = true;
|
|
4904
5003
|
data.readOnly = true;
|
|
4905
|
-
|
|
4906
|
-
|
|
4907
|
-
key
|
|
4908
|
-
|
|
4909
|
-
);
|
|
5004
|
+
const parse = item.result.value._parse;
|
|
5005
|
+
if (parse) {
|
|
5006
|
+
((_a = q.q).defaultParsers ?? (_a.defaultParsers = {}))[key] = parse;
|
|
5007
|
+
}
|
|
4910
5008
|
}
|
|
4911
5009
|
}
|
|
4912
5010
|
q.computeAtRuntime = q.computeBatchAtRuntime = void 0;
|
|
@@ -5115,7 +5213,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5115
5213
|
)
|
|
5116
5214
|
);
|
|
5117
5215
|
}
|
|
5118
|
-
sql = q.toSQL();
|
|
5216
|
+
const localSql = sql = q.toSQL();
|
|
5119
5217
|
const { hookSelect, delayedRelationSelect } = sql;
|
|
5120
5218
|
const { returnType = "all" } = query;
|
|
5121
5219
|
const tempReturnType = hookSelect || returnType === "rows" && q.q.batchParsers || delayedRelationSelect?.value ? "all" : returnType;
|
|
@@ -5140,7 +5238,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5140
5238
|
if (query.patchResult) {
|
|
5141
5239
|
await query.patchResult(q, hookSelect, queryResult);
|
|
5142
5240
|
}
|
|
5143
|
-
result = query.handleResult(q, tempReturnType, queryResult);
|
|
5241
|
+
result = query.handleResult(q, tempReturnType, queryResult, localSql);
|
|
5144
5242
|
} else {
|
|
5145
5243
|
const queryMethod = queryMethodByReturnType[tempReturnType];
|
|
5146
5244
|
const queryBatch = async (batch) => {
|
|
@@ -5178,10 +5276,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5178
5276
|
if (query.patchResult) {
|
|
5179
5277
|
await query.patchResult(q, hookSelect, queryResult);
|
|
5180
5278
|
}
|
|
5181
|
-
result = query.handleResult(q, tempReturnType, queryResult);
|
|
5182
|
-
}
|
|
5183
|
-
if (result && typeof result === "object" && typeof result.then === "function") {
|
|
5184
|
-
result = await result;
|
|
5279
|
+
result = query.handleResult(q, tempReturnType, queryResult, localSql);
|
|
5185
5280
|
}
|
|
5186
5281
|
let tempColumns;
|
|
5187
5282
|
let renames;
|
|
@@ -5205,8 +5300,8 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5205
5300
|
}
|
|
5206
5301
|
}
|
|
5207
5302
|
if (query.selectedComputeds) {
|
|
5208
|
-
const
|
|
5209
|
-
if (
|
|
5303
|
+
const promise2 = processComputedResult(query, result);
|
|
5304
|
+
if (promise2) await promise2;
|
|
5210
5305
|
}
|
|
5211
5306
|
}
|
|
5212
5307
|
const hasAfterHook = afterHooks || afterCommitHooks || query.after;
|
|
@@ -5263,19 +5358,15 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5263
5358
|
);
|
|
5264
5359
|
const selectQuery = q2.clone();
|
|
5265
5360
|
selectQuery.q.type = selectQuery.q.returnType = void 0;
|
|
5266
|
-
|
|
5267
|
-
const
|
|
5268
|
-
|
|
5269
|
-
|
|
5270
|
-
|
|
5271
|
-
for (const key in renames) {
|
|
5272
|
-
if (key in relationsSelect) {
|
|
5273
|
-
selectAs[renames[key]] = relationsSelect[key];
|
|
5274
|
-
}
|
|
5275
|
-
}
|
|
5276
|
-
} else {
|
|
5277
|
-
selectAs = { ...relationsSelect };
|
|
5361
|
+
const matchSourceTableIds = {};
|
|
5362
|
+
for (const pkey of primaryKeys) {
|
|
5363
|
+
matchSourceTableIds[pkey] = {
|
|
5364
|
+
in: result.map((row) => row[pkey])
|
|
5365
|
+
};
|
|
5278
5366
|
}
|
|
5367
|
+
((_a = selectQuery.q).and ?? (_a.and = [])).push(matchSourceTableIds);
|
|
5368
|
+
const relationsSelect = delayedRelationSelect.value;
|
|
5369
|
+
const selectAs = { ...relationsSelect };
|
|
5279
5370
|
const select = [{ selectAs }];
|
|
5280
5371
|
const relationKeyAliases = primaryKeys.map((key) => {
|
|
5281
5372
|
if (key in selectAs) {
|
|
@@ -5299,16 +5390,30 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5299
5390
|
Object.assign(row, relationRow);
|
|
5300
5391
|
}
|
|
5301
5392
|
}
|
|
5393
|
+
if (renames) {
|
|
5394
|
+
for (const key in relationsSelect) {
|
|
5395
|
+
if (key in renames) {
|
|
5396
|
+
delete renames[key];
|
|
5397
|
+
}
|
|
5398
|
+
}
|
|
5399
|
+
}
|
|
5302
5400
|
}
|
|
5401
|
+
const promise = parseBatch(q, queryResult, delayedRelationSelect);
|
|
5402
|
+
if (promise) await promise;
|
|
5303
5403
|
if (hookSelect || tempReturnType !== returnType) {
|
|
5304
5404
|
if (renames) {
|
|
5305
|
-
|
|
5306
|
-
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5405
|
+
const renamedResult = Array.from({
|
|
5406
|
+
length: result.length
|
|
5407
|
+
});
|
|
5408
|
+
for (let i = 0, len = result.length; i < len; ++i) {
|
|
5409
|
+
const record = result[i];
|
|
5410
|
+
const renamedRecord = renamedResult[i] = { ...record };
|
|
5411
|
+
for (const a in renames) {
|
|
5412
|
+
renamedRecord[a] = record[renames[a]];
|
|
5413
|
+
renamedRecord[renames[a]] = record[a];
|
|
5310
5414
|
}
|
|
5311
5415
|
}
|
|
5416
|
+
result = renamedResult;
|
|
5312
5417
|
}
|
|
5313
5418
|
result = filterResult(
|
|
5314
5419
|
q,
|
|
@@ -5362,68 +5467,46 @@ const execQuery = (adapter, method, sql) => {
|
|
|
5362
5467
|
return result;
|
|
5363
5468
|
});
|
|
5364
5469
|
};
|
|
5365
|
-
const handleResult = (q, returnType, result, isSubQuery) => {
|
|
5366
|
-
const
|
|
5470
|
+
const handleResult = (q, returnType, result, sql, isSubQuery) => {
|
|
5471
|
+
const parsers = getQueryParsers(q, sql.hookSelect);
|
|
5367
5472
|
switch (returnType) {
|
|
5368
5473
|
case "all": {
|
|
5369
5474
|
if (q.q.throwOnNotFound && result.rows.length === 0)
|
|
5370
5475
|
throw new NotFoundError(q);
|
|
5371
|
-
const promise = parseBatch(q, result);
|
|
5372
5476
|
const { rows } = result;
|
|
5373
5477
|
if (parsers) {
|
|
5374
5478
|
for (const row of rows) {
|
|
5375
5479
|
parseRecord(parsers, row);
|
|
5376
5480
|
}
|
|
5377
5481
|
}
|
|
5378
|
-
return
|
|
5482
|
+
return rows;
|
|
5379
5483
|
}
|
|
5380
5484
|
case "one": {
|
|
5381
5485
|
const { rows } = result;
|
|
5382
5486
|
if (!rows.length) return;
|
|
5383
|
-
const promise = parseBatch(q, result);
|
|
5384
5487
|
if (parsers) parseRecord(parsers, rows[0]);
|
|
5385
|
-
return
|
|
5488
|
+
return rows[0];
|
|
5386
5489
|
}
|
|
5387
5490
|
case "oneOrThrow": {
|
|
5388
5491
|
const { rows } = result;
|
|
5389
5492
|
if (!rows.length) throw new NotFoundError(q);
|
|
5390
|
-
const promise = parseBatch(q, result);
|
|
5391
5493
|
if (parsers) parseRecord(parsers, rows[0]);
|
|
5392
|
-
return
|
|
5494
|
+
return rows[0];
|
|
5393
5495
|
}
|
|
5394
5496
|
case "rows": {
|
|
5395
5497
|
const { rows } = result;
|
|
5396
|
-
|
|
5397
|
-
if (promise) {
|
|
5398
|
-
return promise.then(() => {
|
|
5399
|
-
if (parsers) parseRows(parsers, result.fields, rows);
|
|
5400
|
-
return rows;
|
|
5401
|
-
});
|
|
5402
|
-
} else if (parsers) {
|
|
5498
|
+
if (parsers) {
|
|
5403
5499
|
parseRows(parsers, result.fields, rows);
|
|
5404
5500
|
}
|
|
5405
5501
|
return rows;
|
|
5406
5502
|
}
|
|
5407
5503
|
case "pluck": {
|
|
5408
5504
|
const { rows } = result;
|
|
5409
|
-
const promise = parseBatch(q, result);
|
|
5410
|
-
if (promise) {
|
|
5411
|
-
return promise.then(() => {
|
|
5412
|
-
parsePluck(parsers, isSubQuery, rows);
|
|
5413
|
-
return rows;
|
|
5414
|
-
});
|
|
5415
|
-
}
|
|
5416
5505
|
parsePluck(parsers, isSubQuery, rows);
|
|
5417
5506
|
return rows;
|
|
5418
5507
|
}
|
|
5419
5508
|
case "value": {
|
|
5420
5509
|
const { rows } = result;
|
|
5421
|
-
const promise = parseBatch(q, result);
|
|
5422
|
-
if (promise) {
|
|
5423
|
-
return promise.then(() => {
|
|
5424
|
-
return rows[0]?.[0] !== void 0 ? parseValue(rows[0][0], parsers) : q.q.notFoundDefault;
|
|
5425
|
-
});
|
|
5426
|
-
}
|
|
5427
5510
|
return rows[0]?.[0] !== void 0 ? parseValue(rows[0][0], parsers) : q.q.notFoundDefault;
|
|
5428
5511
|
}
|
|
5429
5512
|
case "valueOrThrow": {
|
|
@@ -5434,13 +5517,6 @@ const handleResult = (q, returnType, result, isSubQuery) => {
|
|
|
5434
5517
|
return result.rowCount;
|
|
5435
5518
|
}
|
|
5436
5519
|
const { rows } = result;
|
|
5437
|
-
const promise = parseBatch(q, result);
|
|
5438
|
-
if (promise) {
|
|
5439
|
-
return promise.then(() => {
|
|
5440
|
-
if (rows[0]?.[0] === void 0) throw new NotFoundError(q);
|
|
5441
|
-
return parseValue(rows[0][0], parsers);
|
|
5442
|
-
});
|
|
5443
|
-
}
|
|
5444
5520
|
if (rows[0]?.[0] === void 0) throw new NotFoundError(q);
|
|
5445
5521
|
return parseValue(rows[0][0], parsers);
|
|
5446
5522
|
}
|
|
@@ -5449,9 +5525,9 @@ const handleResult = (q, returnType, result, isSubQuery) => {
|
|
|
5449
5525
|
}
|
|
5450
5526
|
}
|
|
5451
5527
|
};
|
|
5452
|
-
const parseBatch = (q, queryResult) => {
|
|
5528
|
+
const parseBatch = (q, queryResult, delayedRelationSelect) => {
|
|
5453
5529
|
let promises;
|
|
5454
|
-
if (q.q.batchParsers) {
|
|
5530
|
+
if (q.q.batchParsers && !delayedRelationSelect?.value) {
|
|
5455
5531
|
for (const parser of q.q.batchParsers) {
|
|
5456
5532
|
const res = parser.fn(parser.path, queryResult);
|
|
5457
5533
|
if (res) (promises ?? (promises = [])).push(res);
|
|
@@ -5564,44 +5640,46 @@ const filterAllResult = (result, tempColumns, hasAfterHook) => {
|
|
|
5564
5640
|
return result;
|
|
5565
5641
|
};
|
|
5566
5642
|
|
|
5643
|
+
const addWithParsers = (w, parsers) => {
|
|
5644
|
+
for (const key in w.shape) {
|
|
5645
|
+
const { _parse } = w.shape[key];
|
|
5646
|
+
if (_parse) parsers[key] = _parse;
|
|
5647
|
+
}
|
|
5648
|
+
};
|
|
5567
5649
|
function queryFrom(self, arg) {
|
|
5568
5650
|
const data = self.q;
|
|
5569
5651
|
if (typeof arg === "string") {
|
|
5570
5652
|
data.as || (data.as = arg);
|
|
5571
5653
|
const w = data.withShapes?.[arg];
|
|
5572
5654
|
data.shape = w?.shape ?? anyShape;
|
|
5573
|
-
data.
|
|
5574
|
-
|
|
5575
|
-
data.
|
|
5655
|
+
data.runtimeComputeds = w?.computeds;
|
|
5656
|
+
const parsers = {};
|
|
5657
|
+
data.defaultParsers = parsers;
|
|
5658
|
+
if (w) addWithParsers(w, parsers);
|
|
5576
5659
|
} else if (Array.isArray(arg)) {
|
|
5577
5660
|
const { shape } = data;
|
|
5578
|
-
|
|
5661
|
+
const joinedParsers = {};
|
|
5579
5662
|
for (const item of arg) {
|
|
5580
5663
|
if (typeof item === "string") {
|
|
5581
5664
|
const w = data.withShapes[item];
|
|
5582
5665
|
Object.assign(shape, w.shape);
|
|
5583
|
-
if (w.computeds)
|
|
5584
|
-
|
|
5585
|
-
|
|
5586
|
-
|
|
5587
|
-
|
|
5588
|
-
w.shape[key]
|
|
5589
|
-
);
|
|
5590
|
-
}
|
|
5666
|
+
if (w.computeds)
|
|
5667
|
+
data.runtimeComputeds = { ...data.runtimeComputeds, ...w.computeds };
|
|
5668
|
+
const parsers = {};
|
|
5669
|
+
joinedParsers[item] = parsers;
|
|
5670
|
+
addWithParsers(w, parsers);
|
|
5591
5671
|
} else if (!isExpression(item)) {
|
|
5592
5672
|
Object.assign(shape, getShapeFromSelect(item, true));
|
|
5593
|
-
|
|
5594
|
-
|
|
5595
|
-
clonedParsers = true;
|
|
5596
|
-
}
|
|
5597
|
-
Object.assign(data.parsers, item.q.parsers);
|
|
5673
|
+
const key = getQueryAs(item);
|
|
5674
|
+
joinedParsers[key] = getQueryParsers(item);
|
|
5598
5675
|
}
|
|
5599
5676
|
}
|
|
5677
|
+
data.joinedParsers = joinedParsers;
|
|
5600
5678
|
} else {
|
|
5601
5679
|
const q = arg;
|
|
5602
5680
|
data.as || (data.as = q.q.as || q.table || "t");
|
|
5603
5681
|
data.shape = getShapeFromSelect(q, true);
|
|
5604
|
-
data.
|
|
5682
|
+
data.defaultParsers = getQueryParsers(q);
|
|
5605
5683
|
data.batchParsers = q.q.batchParsers;
|
|
5606
5684
|
}
|
|
5607
5685
|
data.from = arg;
|
|
@@ -5714,7 +5792,7 @@ const addParsersForSelectJoined = (q, arg, as = arg) => {
|
|
|
5714
5792
|
}
|
|
5715
5793
|
};
|
|
5716
5794
|
const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
5717
|
-
if (typeof arg === "object"
|
|
5795
|
+
if (typeof arg === "object") {
|
|
5718
5796
|
const { q: query } = arg;
|
|
5719
5797
|
if (query.batchParsers) {
|
|
5720
5798
|
pushQueryArrayImmutable(
|
|
@@ -5726,7 +5804,8 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5726
5804
|
}))
|
|
5727
5805
|
);
|
|
5728
5806
|
}
|
|
5729
|
-
|
|
5807
|
+
const parsers = isExpression(arg) ? void 0 : getQueryParsers(arg);
|
|
5808
|
+
if (parsers || query.hookSelect || query.transform || query.returnType === "oneOrThrow" || query.returnType === "valueOrThrow" || query.returnType === "one" || query.returnType === "value") {
|
|
5730
5809
|
pushQueryValueImmutable(q, "batchParsers", {
|
|
5731
5810
|
path: [key],
|
|
5732
5811
|
fn: (path, queryResult) => {
|
|
@@ -5750,7 +5829,6 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5750
5829
|
collectNestedSelectBatches(batches, rows, path, last);
|
|
5751
5830
|
switch (returnType) {
|
|
5752
5831
|
case "all": {
|
|
5753
|
-
const { parsers } = query;
|
|
5754
5832
|
if (parsers) {
|
|
5755
5833
|
for (const { data } of batches) {
|
|
5756
5834
|
for (const one of data) {
|
|
@@ -5762,7 +5840,6 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5762
5840
|
}
|
|
5763
5841
|
case "one":
|
|
5764
5842
|
case "oneOrThrow": {
|
|
5765
|
-
const { parsers } = query;
|
|
5766
5843
|
if (parsers) {
|
|
5767
5844
|
if (returnType === "one") {
|
|
5768
5845
|
for (const batch of batches) {
|
|
@@ -5793,7 +5870,7 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5793
5870
|
break;
|
|
5794
5871
|
}
|
|
5795
5872
|
case "pluck": {
|
|
5796
|
-
const parse =
|
|
5873
|
+
const parse = parsers?.pluck;
|
|
5797
5874
|
if (parse) {
|
|
5798
5875
|
for (const { data } of batches) {
|
|
5799
5876
|
for (let i = 0; i < data.length; i++) {
|
|
@@ -5806,7 +5883,7 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5806
5883
|
case "value":
|
|
5807
5884
|
case "valueOrThrow": {
|
|
5808
5885
|
const notNullable = !query.getColumn?.data.isNullable;
|
|
5809
|
-
const parse =
|
|
5886
|
+
const parse = parsers?.[getValueKey];
|
|
5810
5887
|
if (parse) {
|
|
5811
5888
|
if (returnType === "value") {
|
|
5812
5889
|
for (const item of batches) {
|
|
@@ -5977,12 +6054,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
|
|
|
5977
6054
|
subQuery = value;
|
|
5978
6055
|
}
|
|
5979
6056
|
}
|
|
5980
|
-
|
|
5981
|
-
value,
|
|
5982
|
-
q,
|
|
5983
|
-
key
|
|
5984
|
-
);
|
|
5985
|
-
_joinLateral(
|
|
6057
|
+
const as2 = _joinLateral(
|
|
5986
6058
|
q,
|
|
5987
6059
|
innerJoinLateral ? "JOIN" : "LEFT JOIN",
|
|
5988
6060
|
subQuery,
|
|
@@ -5991,6 +6063,13 @@ const processSelectArg = (q, as, arg, columnAs) => {
|
|
|
5991
6063
|
// `JOIN` will handle it on itself.
|
|
5992
6064
|
innerJoinLateral && returnType !== "one" && returnType !== "oneOrThrow"
|
|
5993
6065
|
);
|
|
6066
|
+
if (as2) {
|
|
6067
|
+
value.q.joinedForSelect = _copyQueryAliasToQuery(
|
|
6068
|
+
value,
|
|
6069
|
+
q,
|
|
6070
|
+
as2
|
|
6071
|
+
);
|
|
6072
|
+
}
|
|
5994
6073
|
}
|
|
5995
6074
|
}
|
|
5996
6075
|
selectAs[key] = addParserForSelectItem(
|
|
@@ -6037,31 +6116,30 @@ const setParserForSelectedString = (query, arg, as, columnAs, columnAlias) => {
|
|
|
6037
6116
|
const computeds = q.joinedComputeds?.[table];
|
|
6038
6117
|
if (computeds?.[column]) {
|
|
6039
6118
|
const computed = computeds[column];
|
|
6040
|
-
|
|
6041
|
-
for (const column2 of computed.deps) {
|
|
6042
|
-
map.set(column2, { select: `${table}.${column2}` });
|
|
6043
|
-
}
|
|
6119
|
+
_addToHookSelectWithTable(query, computed.deps, table);
|
|
6044
6120
|
setObjectValueImmutable(q, "selectedComputeds", column, computed);
|
|
6045
6121
|
return;
|
|
6046
6122
|
}
|
|
6047
6123
|
return arg;
|
|
6048
6124
|
};
|
|
6049
6125
|
const selectColumn = (query, q, key, columnAs, columnAlias) => {
|
|
6050
|
-
if (
|
|
6051
|
-
const
|
|
6052
|
-
if (
|
|
6053
|
-
|
|
6054
|
-
|
|
6055
|
-
|
|
6056
|
-
const
|
|
6057
|
-
|
|
6058
|
-
|
|
6059
|
-
|
|
6060
|
-
|
|
6061
|
-
|
|
6062
|
-
|
|
6063
|
-
|
|
6064
|
-
|
|
6126
|
+
if (key === "*") {
|
|
6127
|
+
const { defaultParsers } = query.q;
|
|
6128
|
+
if (defaultParsers) {
|
|
6129
|
+
spreadObjectValues(query.q, "parsers", defaultParsers);
|
|
6130
|
+
}
|
|
6131
|
+
} else {
|
|
6132
|
+
const parser = query.q.defaultParsers?.[key];
|
|
6133
|
+
if (parser) setObjectValueImmutable(q, "parsers", columnAs || key, parser);
|
|
6134
|
+
if (q.runtimeComputeds?.[key]) {
|
|
6135
|
+
const computed = q.runtimeComputeds[key];
|
|
6136
|
+
_addToHookSelect(query, computed.deps);
|
|
6137
|
+
query.q.selectedComputeds = {
|
|
6138
|
+
...query.q.selectedComputeds,
|
|
6139
|
+
[columnAlias || key]: computed
|
|
6140
|
+
};
|
|
6141
|
+
return;
|
|
6142
|
+
}
|
|
6065
6143
|
}
|
|
6066
6144
|
return key;
|
|
6067
6145
|
};
|
|
@@ -6116,7 +6194,7 @@ const getShapeFromSelect = (q, isSubQuery) => {
|
|
|
6116
6194
|
const { returnType } = it.q;
|
|
6117
6195
|
if (returnType === "value" || returnType === "valueOrThrow") {
|
|
6118
6196
|
const type = it.q.getColumn;
|
|
6119
|
-
result[key] = type
|
|
6197
|
+
result[key] = type ? mapSubSelectColumn(type, isSubQuery) : UnknownColumn.instance;
|
|
6120
6198
|
} else {
|
|
6121
6199
|
result[key] = new JSONTextColumn(defaultSchemaConfig);
|
|
6122
6200
|
}
|
|
@@ -6194,6 +6272,11 @@ function _querySelect(q, args) {
|
|
|
6194
6272
|
}
|
|
6195
6273
|
return pushQueryArrayImmutable(q, "select", selectArgs);
|
|
6196
6274
|
}
|
|
6275
|
+
const _querySelectAll = (query) => {
|
|
6276
|
+
const q = query;
|
|
6277
|
+
q.q.select = ["*"];
|
|
6278
|
+
q.q.parsers = q.q.defaultParsers;
|
|
6279
|
+
};
|
|
6197
6280
|
class Select {
|
|
6198
6281
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6199
6282
|
select(...args) {
|
|
@@ -6217,7 +6300,7 @@ class Select {
|
|
|
6217
6300
|
*/
|
|
6218
6301
|
selectAll() {
|
|
6219
6302
|
const q = _clone(this);
|
|
6220
|
-
q
|
|
6303
|
+
_querySelectAll(q);
|
|
6221
6304
|
if (q.q.returning) {
|
|
6222
6305
|
q.q.returnType = q.q.returningMany ? "all" : "oneOrThrow";
|
|
6223
6306
|
q.q.returning = void 0;
|
|
@@ -7068,13 +7151,13 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
|
|
|
7068
7151
|
};
|
|
7069
7152
|
function selectedObjectToSQL(ctx, quotedAs, item) {
|
|
7070
7153
|
const sql = item.toSQL(ctx, quotedAs);
|
|
7071
|
-
return ctx.aliasValue ? `${sql}
|
|
7154
|
+
return ctx.aliasValue ? `${sql} "${quotedAs}"` : sql;
|
|
7072
7155
|
}
|
|
7073
7156
|
const selectAllSql = (query, quotedAs, jsonList) => {
|
|
7074
7157
|
if (jsonList) {
|
|
7075
7158
|
Object.assign(jsonList, query.selectAllShape);
|
|
7076
7159
|
}
|
|
7077
|
-
return query.join?.length ? query.selectAllColumns?.map((item) => `${quotedAs}.${item}`).join(", ") || `${quotedAs}.*` : query.selectAllColumns?.join(", ") || "*";
|
|
7160
|
+
return query.join?.length || query.updateFrom ? query.selectAllColumns?.map((item) => `${quotedAs}.${item}`).join(", ") || `${quotedAs}.*` : query.selectAllColumns?.join(", ") || "*";
|
|
7078
7161
|
};
|
|
7079
7162
|
const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) => {
|
|
7080
7163
|
const { returnType = "all" } = query.q;
|
|
@@ -7116,14 +7199,14 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7116
7199
|
break;
|
|
7117
7200
|
}
|
|
7118
7201
|
case "all":
|
|
7119
|
-
case "pluck":
|
|
7120
7202
|
case "value":
|
|
7203
|
+
case "pluck":
|
|
7121
7204
|
case "rows":
|
|
7122
|
-
sql = `"${query.q.joinedForSelect}".
|
|
7205
|
+
sql = `"${query.q.joinedForSelect}"."${as}"`;
|
|
7123
7206
|
break;
|
|
7124
7207
|
case "valueOrThrow":
|
|
7125
7208
|
if (query.q.returning) return;
|
|
7126
|
-
sql = `"${query.q.joinedForSelect}".
|
|
7209
|
+
sql = `"${query.q.joinedForSelect}"."${as}"`;
|
|
7127
7210
|
break;
|
|
7128
7211
|
case "void":
|
|
7129
7212
|
return;
|
|
@@ -7145,7 +7228,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7145
7228
|
case "pluck": {
|
|
7146
7229
|
const { select } = query.q;
|
|
7147
7230
|
const first = select?.[0];
|
|
7148
|
-
if (!first && query.q.
|
|
7231
|
+
if (!first && query.q.runtimeComputeds?.[as]) {
|
|
7149
7232
|
query = queryJson(query);
|
|
7150
7233
|
} else if (!first) {
|
|
7151
7234
|
throw new OrchidOrmInternalError(
|
|
@@ -7162,7 +7245,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7162
7245
|
}
|
|
7163
7246
|
case "value":
|
|
7164
7247
|
case "valueOrThrow":
|
|
7165
|
-
if (!query.q.returning && query.q.
|
|
7248
|
+
if (!query.q.returning && query.q.runtimeComputeds?.[as]) {
|
|
7166
7249
|
query = queryJson(query);
|
|
7167
7250
|
}
|
|
7168
7251
|
break;
|
|
@@ -7274,9 +7357,6 @@ const pushFromAndAs = (ctx, table, data, quotedAs) => {
|
|
|
7274
7357
|
let sql = "FROM ";
|
|
7275
7358
|
const from = getFrom(ctx, table, data, quotedAs);
|
|
7276
7359
|
sql += from;
|
|
7277
|
-
if (data.as && quotedAs && quotedAs !== from) {
|
|
7278
|
-
sql += ` ${quotedAs}`;
|
|
7279
|
-
}
|
|
7280
7360
|
for (const as in data.sources) {
|
|
7281
7361
|
const source = data.sources[as];
|
|
7282
7362
|
const lang = getSearchLang(ctx, data, source, quotedAs);
|
|
@@ -7323,6 +7403,9 @@ const getFrom = (ctx, table, data, quotedAs) => {
|
|
|
7323
7403
|
return fromToSql(ctx, data, from, quotedAs);
|
|
7324
7404
|
}
|
|
7325
7405
|
let sql = quoteSchemaAndTable(data.schema, table.table);
|
|
7406
|
+
if (data.as && quotedAs && quotedAs !== sql) {
|
|
7407
|
+
sql += ` ${quotedAs}`;
|
|
7408
|
+
}
|
|
7326
7409
|
if (data.only) sql = `ONLY ${sql}`;
|
|
7327
7410
|
return sql;
|
|
7328
7411
|
};
|
|
@@ -7331,13 +7414,13 @@ const fromToSql = (ctx, data, from, quotedAs) => {
|
|
|
7331
7414
|
let sql;
|
|
7332
7415
|
if (typeof from === "object") {
|
|
7333
7416
|
if (isExpression(from)) {
|
|
7334
|
-
sql = from.toSQL(ctx, quotedAs);
|
|
7417
|
+
sql = from.toSQL(ctx, quotedAs) + " " + quotedAs;
|
|
7335
7418
|
} else {
|
|
7336
7419
|
only = from.q.only;
|
|
7337
7420
|
if (!from.table) {
|
|
7338
7421
|
sql = `(${getSqlText(toSQL(from, ctx))})`;
|
|
7339
7422
|
} else if (!checkIfASimpleQuery(from)) {
|
|
7340
|
-
sql = `(${getSqlText(toSQL(from, ctx))})`;
|
|
7423
|
+
sql = `(${getSqlText(toSQL(from, ctx))}) ${quotedAs || `"${getQueryAs(from)}"`}`;
|
|
7341
7424
|
} else {
|
|
7342
7425
|
sql = quoteSchemaAndTable(from.q.schema, from.table);
|
|
7343
7426
|
}
|
|
@@ -7455,7 +7538,45 @@ const pushUpdateSql = (ctx, table, query, quotedAs) => {
|
|
|
7455
7538
|
}
|
|
7456
7539
|
ctx.sql.push("SET");
|
|
7457
7540
|
ctx.sql.push(set.join(", "));
|
|
7458
|
-
|
|
7541
|
+
const { updateFrom } = query;
|
|
7542
|
+
let fromWhereSql;
|
|
7543
|
+
if (updateFrom) {
|
|
7544
|
+
const { target, on } = processJoinItem(
|
|
7545
|
+
ctx,
|
|
7546
|
+
table,
|
|
7547
|
+
query,
|
|
7548
|
+
updateFrom,
|
|
7549
|
+
quotedAs
|
|
7550
|
+
);
|
|
7551
|
+
ctx.sql.push(`FROM ${target}`);
|
|
7552
|
+
fromWhereSql = on;
|
|
7553
|
+
if (query.join) {
|
|
7554
|
+
const joinSet = query.join.length > 1 ? /* @__PURE__ */ new Set() : null;
|
|
7555
|
+
for (const item of query.join) {
|
|
7556
|
+
const { target: target2, on: on2 } = processJoinItem(
|
|
7557
|
+
ctx,
|
|
7558
|
+
table,
|
|
7559
|
+
query,
|
|
7560
|
+
item.args,
|
|
7561
|
+
quotedAs
|
|
7562
|
+
);
|
|
7563
|
+
if (joinSet) {
|
|
7564
|
+
const key = `${item.type}${target2}${on2}`;
|
|
7565
|
+
if (joinSet.has(key)) continue;
|
|
7566
|
+
joinSet.add(key);
|
|
7567
|
+
}
|
|
7568
|
+
ctx.sql.push(`${item.type} ${target2} ON true`);
|
|
7569
|
+
if (on2) {
|
|
7570
|
+
fromWhereSql = fromWhereSql ? fromWhereSql + " AND " + on2 : on2;
|
|
7571
|
+
}
|
|
7572
|
+
}
|
|
7573
|
+
}
|
|
7574
|
+
}
|
|
7575
|
+
const mainWhereSql = whereToSql(ctx, table, query, quotedAs);
|
|
7576
|
+
const whereSql = mainWhereSql ? fromWhereSql ? mainWhereSql + " AND " + fromWhereSql : mainWhereSql : fromWhereSql;
|
|
7577
|
+
if (whereSql) {
|
|
7578
|
+
ctx.sql.push("WHERE", whereSql);
|
|
7579
|
+
}
|
|
7459
7580
|
hookSelect = pushUpdateReturning(
|
|
7460
7581
|
ctx,
|
|
7461
7582
|
table,
|
|
@@ -8747,7 +8868,7 @@ class WithMethods {
|
|
|
8747
8868
|
const shape = getShapeFromSelect(query, true);
|
|
8748
8869
|
return setQueryObjectValueImmutable(q, "withShapes", name, {
|
|
8749
8870
|
shape,
|
|
8750
|
-
computeds: query.q.
|
|
8871
|
+
computeds: query.q.runtimeComputeds
|
|
8751
8872
|
});
|
|
8752
8873
|
}
|
|
8753
8874
|
withRecursive(name, ...args) {
|
|
@@ -8758,7 +8879,7 @@ class WithMethods {
|
|
|
8758
8879
|
arg.q.withShapes = q.q.withShapes;
|
|
8759
8880
|
let query = typeof baseFn === "function" ? baseFn(arg) : baseFn;
|
|
8760
8881
|
const shape = getShapeFromSelect(query, true);
|
|
8761
|
-
const withConfig = { shape, computeds: query.q.
|
|
8882
|
+
const withConfig = { shape, computeds: query.q.runtimeComputeds };
|
|
8762
8883
|
((_a = arg.q).withShapes ?? (_a.withShapes = {}))[name] = withConfig;
|
|
8763
8884
|
const recursive = recursiveFn(arg);
|
|
8764
8885
|
query = _queryUnion(query, [recursive], options.union ?? "UNION ALL");
|
|
@@ -8775,14 +8896,15 @@ class WithMethods {
|
|
|
8775
8896
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8776
8897
|
withSql(name, ...args) {
|
|
8777
8898
|
const q = _clone(this);
|
|
8778
|
-
const [options,
|
|
8899
|
+
const [options, shapeFn, sql] = args.length === 2 ? [void 0, args[0], args[1]] : args;
|
|
8900
|
+
const shape = shapeFn(this.columnTypes);
|
|
8779
8901
|
pushQueryValueImmutable(q, "with", {
|
|
8780
8902
|
n: name,
|
|
8781
|
-
o: options,
|
|
8903
|
+
o: { ...options, columns: Object.keys(shape) },
|
|
8782
8904
|
s: sql(q)
|
|
8783
8905
|
});
|
|
8784
8906
|
return setQueryObjectValueImmutable(q, "withShapes", name, {
|
|
8785
|
-
shape
|
|
8907
|
+
shape
|
|
8786
8908
|
});
|
|
8787
8909
|
}
|
|
8788
8910
|
}
|
|
@@ -9033,7 +9155,7 @@ const createSelect = (q) => {
|
|
|
9033
9155
|
if (q.q.returnType === "void" || isSelectingCount(q)) {
|
|
9034
9156
|
q.q.select = void 0;
|
|
9035
9157
|
} else if (!q.q.select) {
|
|
9036
|
-
q
|
|
9158
|
+
_querySelectAll(q);
|
|
9037
9159
|
q.q.returning = true;
|
|
9038
9160
|
}
|
|
9039
9161
|
};
|
|
@@ -10670,11 +10792,12 @@ class Join {
|
|
|
10670
10792
|
*/
|
|
10671
10793
|
joinLateral(arg, cb) {
|
|
10672
10794
|
const q = _clone(this);
|
|
10673
|
-
|
|
10795
|
+
_joinLateral(
|
|
10674
10796
|
q,
|
|
10675
10797
|
"JOIN",
|
|
10676
10798
|
_joinLateralProcessArg(q, arg, cb)
|
|
10677
10799
|
);
|
|
10800
|
+
return q;
|
|
10678
10801
|
}
|
|
10679
10802
|
/**
|
|
10680
10803
|
* The same as {@link joinLateral}, but when no records found for the join it will result in `null`:
|
|
@@ -10693,11 +10816,12 @@ class Join {
|
|
|
10693
10816
|
*/
|
|
10694
10817
|
leftJoinLateral(arg, cb) {
|
|
10695
10818
|
const q = _clone(this);
|
|
10696
|
-
|
|
10697
|
-
|
|
10819
|
+
_joinLateral(
|
|
10820
|
+
q,
|
|
10698
10821
|
"LEFT JOIN",
|
|
10699
10822
|
_joinLateralProcessArg(q, arg, cb)
|
|
10700
10823
|
);
|
|
10824
|
+
return q;
|
|
10701
10825
|
}
|
|
10702
10826
|
/**
|
|
10703
10827
|
* This method may be useful
|
|
@@ -10879,6 +11003,7 @@ class JsonMethods {
|
|
|
10879
11003
|
const mergableObjects = /* @__PURE__ */ new Set([
|
|
10880
11004
|
"shape",
|
|
10881
11005
|
"withShapes",
|
|
11006
|
+
"defaultParsers",
|
|
10882
11007
|
"parsers",
|
|
10883
11008
|
"defaults",
|
|
10884
11009
|
"joinedShapes",
|
|
@@ -10965,7 +11090,8 @@ const _queryChangeCounter = (self, op, data) => {
|
|
|
10965
11090
|
pushQueryValueImmutable(self, "updateData", map);
|
|
10966
11091
|
return self;
|
|
10967
11092
|
};
|
|
10968
|
-
const _queryUpdate = (
|
|
11093
|
+
const _queryUpdate = (updateSelf, arg) => {
|
|
11094
|
+
const query = updateSelf;
|
|
10969
11095
|
const { q } = query;
|
|
10970
11096
|
q.type = "update";
|
|
10971
11097
|
const returnCount = !q.select;
|
|
@@ -10973,6 +11099,7 @@ const _queryUpdate = (query, arg) => {
|
|
|
10973
11099
|
pushQueryValueImmutable(query, "updateData", set);
|
|
10974
11100
|
const { shape } = q;
|
|
10975
11101
|
const ctx = {};
|
|
11102
|
+
let selectQuery;
|
|
10976
11103
|
for (const key in arg) {
|
|
10977
11104
|
const item = shape[key];
|
|
10978
11105
|
if (!item && shape !== anyShape) {
|
|
@@ -10984,8 +11111,12 @@ const _queryUpdate = (query, arg) => {
|
|
|
10984
11111
|
if (item) throwOnReadOnly(query, item, key);
|
|
10985
11112
|
let value = set[key];
|
|
10986
11113
|
if (typeof value === "function") {
|
|
11114
|
+
if (!selectQuery) {
|
|
11115
|
+
selectQuery = query.clone();
|
|
11116
|
+
selectQuery.q.type = void 0;
|
|
11117
|
+
}
|
|
10987
11118
|
value = resolveSubQueryCallbackV2(
|
|
10988
|
-
|
|
11119
|
+
selectQuery,
|
|
10989
11120
|
value
|
|
10990
11121
|
);
|
|
10991
11122
|
if (value instanceof Db && value.q.type && value.q.subQuery) {
|
|
@@ -10998,14 +11129,7 @@ const _queryUpdate = (query, arg) => {
|
|
|
10998
11129
|
}
|
|
10999
11130
|
if (value !== null && value !== void 0 && !isExpression(value)) {
|
|
11000
11131
|
if (value instanceof Db) {
|
|
11001
|
-
moveQueryValueToWith(
|
|
11002
|
-
query,
|
|
11003
|
-
q,
|
|
11004
|
-
value,
|
|
11005
|
-
"with",
|
|
11006
|
-
set,
|
|
11007
|
-
key
|
|
11008
|
-
);
|
|
11132
|
+
moveQueryValueToWith(query, q, value, "with", set, key);
|
|
11009
11133
|
} else {
|
|
11010
11134
|
const encode = item?.data.encode;
|
|
11011
11135
|
if (encode) set[key] = encode(value);
|
|
@@ -11033,10 +11157,7 @@ const _queryUpdate = (query, arg) => {
|
|
|
11033
11157
|
primaryKeys,
|
|
11034
11158
|
queryResult.rows.map((item) => primaryKeys.map((key) => item[key]))
|
|
11035
11159
|
);
|
|
11036
|
-
await _queryUpdate(
|
|
11037
|
-
t,
|
|
11038
|
-
ctx.collect.data
|
|
11039
|
-
);
|
|
11160
|
+
await _queryUpdate(t, ctx.collect.data);
|
|
11040
11161
|
for (const row of queryResult.rows) {
|
|
11041
11162
|
Object.assign(row, ctx.collect.data);
|
|
11042
11163
|
}
|
|
@@ -11048,7 +11169,9 @@ const _queryUpdate = (query, arg) => {
|
|
|
11048
11169
|
q.returnType = "valueOrThrow";
|
|
11049
11170
|
q.returning = true;
|
|
11050
11171
|
}
|
|
11051
|
-
|
|
11172
|
+
if (!q.updateFrom) {
|
|
11173
|
+
throwIfNoWhere(query, "update");
|
|
11174
|
+
}
|
|
11052
11175
|
return query;
|
|
11053
11176
|
};
|
|
11054
11177
|
const _queryUpdateOrThrow = (q, arg) => {
|
|
@@ -11286,6 +11409,77 @@ class Update {
|
|
|
11286
11409
|
updateOrThrow(arg) {
|
|
11287
11410
|
return _queryUpdateOrThrow(_clone(this), arg);
|
|
11288
11411
|
}
|
|
11412
|
+
/**
|
|
11413
|
+
* Use `updateFrom` to update records in one table based on a query result from another table or CTE.
|
|
11414
|
+
*
|
|
11415
|
+
* `updateFrom` accepts the same arguments as {@link Query.join}.
|
|
11416
|
+
*
|
|
11417
|
+
* ```ts
|
|
11418
|
+
* // save all author names to their books by using a relation name:
|
|
11419
|
+
* db.books.updateFrom('author').set({ authorName: (q) => q.ref('author.name') });
|
|
11420
|
+
*
|
|
11421
|
+
* // update from authors that match the condition:
|
|
11422
|
+
* db.books
|
|
11423
|
+
* .updateFrom((q) => q.author.where({ writingSkills: 'good' }))
|
|
11424
|
+
* .set({ authorName: (q) => q.ref('author.name') });
|
|
11425
|
+
*
|
|
11426
|
+
* // update from any table using custom `on` conditions:
|
|
11427
|
+
* db.books
|
|
11428
|
+
* .updateFrom(
|
|
11429
|
+
* () => db.authors,
|
|
11430
|
+
* (q) => q.on('authors.id', 'books.authorId'),
|
|
11431
|
+
* )
|
|
11432
|
+
* .set({ authorName: (q) => q.ref('author.name') });
|
|
11433
|
+
*
|
|
11434
|
+
* // conditions after `updateFrom` can reference both tables:
|
|
11435
|
+
* db.books
|
|
11436
|
+
* .updateFrom(() => db.authors)
|
|
11437
|
+
* .where({
|
|
11438
|
+
* 'authors.id': (q) => q.ref('books.authorId'),
|
|
11439
|
+
* })
|
|
11440
|
+
* .set({ authorName: (q) => q.ref('author.name') });
|
|
11441
|
+
*
|
|
11442
|
+
* // can join and use another table in between `updateFrom` and `set`:
|
|
11443
|
+
* db.books
|
|
11444
|
+
* .updateFrom('author')
|
|
11445
|
+
* .join('publisher')
|
|
11446
|
+
* .set({
|
|
11447
|
+
* authorName: (q) => q.ref('author.name'),
|
|
11448
|
+
* publisherName: (q) => q.ref('publisher.name'),
|
|
11449
|
+
* });
|
|
11450
|
+
*
|
|
11451
|
+
* // updating from a CTE
|
|
11452
|
+
* db.books
|
|
11453
|
+
* .with('a', () =>
|
|
11454
|
+
* db.authors.where({ writingSkills: 'good' }).select('id', 'name').limit(10),
|
|
11455
|
+
* )
|
|
11456
|
+
* .updateFrom('a', (q) => q.on('a.id', 'books.authorId'))
|
|
11457
|
+
* .set({ authorName: (q) => q.ref('author.name') });
|
|
11458
|
+
* ```
|
|
11459
|
+
*/
|
|
11460
|
+
updateFrom(arg, ...args) {
|
|
11461
|
+
const q = _clone(this);
|
|
11462
|
+
const joinArgs = _joinReturningArgs(
|
|
11463
|
+
q,
|
|
11464
|
+
true,
|
|
11465
|
+
arg,
|
|
11466
|
+
args,
|
|
11467
|
+
// eslint-disable-line @typescript-eslint/no-explicit-any
|
|
11468
|
+
true
|
|
11469
|
+
);
|
|
11470
|
+
if (!joinArgs) {
|
|
11471
|
+
return _queryNone(q);
|
|
11472
|
+
}
|
|
11473
|
+
joinArgs.u = true;
|
|
11474
|
+
q.q.updateFrom = joinArgs;
|
|
11475
|
+
return q;
|
|
11476
|
+
}
|
|
11477
|
+
/**
|
|
11478
|
+
* Use after {@link updateFrom}
|
|
11479
|
+
*/
|
|
11480
|
+
set(arg) {
|
|
11481
|
+
return _queryUpdate(_clone(this), arg);
|
|
11482
|
+
}
|
|
11289
11483
|
/**
|
|
11290
11484
|
* Increments a column by `1`, returns a count of updated records by default.
|
|
11291
11485
|
*
|
|
@@ -11614,8 +11808,8 @@ function orCreate(query, data, updateData, mergeData) {
|
|
|
11614
11808
|
const { handleResult } = q;
|
|
11615
11809
|
let result;
|
|
11616
11810
|
let created = false;
|
|
11617
|
-
q.handleResult = (q2, t, r, s) => {
|
|
11618
|
-
return created ? result : handleResult(q2, t, r, s);
|
|
11811
|
+
q.handleResult = (q2, t, r, s, i) => {
|
|
11812
|
+
return created ? result : handleResult(q2, t, r, s, i);
|
|
11619
11813
|
};
|
|
11620
11814
|
q.hookSelect = new Map(q.hookSelect);
|
|
11621
11815
|
q.patchResult = async (q2, hookSelect, queryResult) => {
|
|
@@ -11654,7 +11848,7 @@ function orCreate(query, data, updateData, mergeData) {
|
|
|
11654
11848
|
);
|
|
11655
11849
|
let afterHooks;
|
|
11656
11850
|
let afterCommitHooks;
|
|
11657
|
-
q22.q.handleResult = (a, t, r, s) => {
|
|
11851
|
+
q22.q.handleResult = (a, t, r, s, i) => {
|
|
11658
11852
|
if (hasAfterCallback || hasAfterCommitCallback) {
|
|
11659
11853
|
const fieldName = r.fields[0].name;
|
|
11660
11854
|
if (r.rows[0][fieldName]) {
|
|
@@ -11666,7 +11860,7 @@ function orCreate(query, data, updateData, mergeData) {
|
|
|
11666
11860
|
}
|
|
11667
11861
|
delete r.rows[0][fieldName];
|
|
11668
11862
|
}
|
|
11669
|
-
result = handleResult(a, t, r, s);
|
|
11863
|
+
result = handleResult(a, t, r, s, i);
|
|
11670
11864
|
return a.q.hookSelect ? result.map((row) => ({ ...row })) : result;
|
|
11671
11865
|
};
|
|
11672
11866
|
q22.q.log = q2.q.log;
|
|
@@ -12990,15 +13184,6 @@ class Db extends QueryMethods {
|
|
|
12990
13184
|
const self = this;
|
|
12991
13185
|
const { softDelete } = options;
|
|
12992
13186
|
const scopes = options.scopes || softDelete ? {} : emptyObject;
|
|
12993
|
-
this.internal = {
|
|
12994
|
-
transactionStorage,
|
|
12995
|
-
scopes,
|
|
12996
|
-
snakeCase: options.snakeCase,
|
|
12997
|
-
noPrimaryKey: options.noPrimaryKey === "ignore",
|
|
12998
|
-
comment: options.comment,
|
|
12999
|
-
nowSQL: options.nowSQL,
|
|
13000
|
-
tableData
|
|
13001
|
-
};
|
|
13002
13187
|
this.baseQuery = this;
|
|
13003
13188
|
this.relations = {};
|
|
13004
13189
|
this.relationQueries = {};
|
|
@@ -13008,6 +13193,7 @@ class Db extends QueryMethods {
|
|
|
13008
13193
|
let modifyQuery = void 0;
|
|
13009
13194
|
let prepareSelectAll = false;
|
|
13010
13195
|
let hasHookSetters;
|
|
13196
|
+
let runtimeDefaultColumns;
|
|
13011
13197
|
const { snakeCase } = options;
|
|
13012
13198
|
for (const key in shape) {
|
|
13013
13199
|
const column = shape[key];
|
|
@@ -13033,9 +13219,8 @@ class Db extends QueryMethods {
|
|
|
13033
13219
|
modifyQuery = pushOrNewArray(modifyQuery, (q) => mq(q, column));
|
|
13034
13220
|
}
|
|
13035
13221
|
if (typeof column.data.default === "function") {
|
|
13036
|
-
|
|
13037
|
-
|
|
13038
|
-
else arr.push(key);
|
|
13222
|
+
if (!runtimeDefaultColumns) runtimeDefaultColumns = [key];
|
|
13223
|
+
else runtimeDefaultColumns.push(key);
|
|
13039
13224
|
if (!column.data.runtimeDefault) {
|
|
13040
13225
|
const {
|
|
13041
13226
|
data: { default: def, encode }
|
|
@@ -13047,6 +13232,16 @@ class Db extends QueryMethods {
|
|
|
13047
13232
|
hasHookSetters = true;
|
|
13048
13233
|
}
|
|
13049
13234
|
}
|
|
13235
|
+
this.internal = {
|
|
13236
|
+
runtimeDefaultColumns,
|
|
13237
|
+
transactionStorage,
|
|
13238
|
+
scopes,
|
|
13239
|
+
snakeCase: options.snakeCase,
|
|
13240
|
+
noPrimaryKey: options.noPrimaryKey === "ignore",
|
|
13241
|
+
comment: options.comment,
|
|
13242
|
+
nowSQL: options.nowSQL,
|
|
13243
|
+
tableData
|
|
13244
|
+
};
|
|
13050
13245
|
this.q = {
|
|
13051
13246
|
adapter,
|
|
13052
13247
|
shape,
|
|
@@ -13054,7 +13249,7 @@ class Db extends QueryMethods {
|
|
|
13054
13249
|
logger,
|
|
13055
13250
|
log: logParamToLogObject(logger, options.log),
|
|
13056
13251
|
autoPreparedStatements: options.autoPreparedStatements ?? false,
|
|
13057
|
-
|
|
13252
|
+
defaultParsers: hasParsers ? parsers : void 0,
|
|
13058
13253
|
language: options.language,
|
|
13059
13254
|
schema: options?.schema
|
|
13060
13255
|
};
|
|
@@ -13458,5 +13653,5 @@ function copyTableData(query, arg) {
|
|
|
13458
13653
|
return q;
|
|
13459
13654
|
}
|
|
13460
13655
|
|
|
13461
|
-
export { AfterCommitError, AggregateMethods, ArrayColumn, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ColumnRefExpression, ColumnType, ComputedColumn, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, Delete, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, ExpressionMethods, FnExpression, For, FromMethods, Having, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, Join, JsonMethods, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQueryMethods, MoneyColumn, NumberAsStringBaseColumn, NumberBaseColumn, OnMethods, Operators, OrExpression, PathColumn, PointColumn, PolygonColumn, PostgisGeographyPointColumn, QueryAsMethods, QueryGet, QueryHooks, QueryLog, QueryMethods, QueryUpsert, RawSQL, RealColumn, RefExpression, SearchMethods, Select, SerialColumn, SmallIntColumn, SmallSerialColumn, SqlMethod, StringColumn, TextBaseColumn, TextColumn, Then, TimeColumn, TimestampColumn, TimestampTZColumn, Transaction, TransformMethods, TsQueryColumn, TsVectorColumn, UUIDColumn, Union, UnknownColumn, Update, VarCharColumn, VirtualColumn, Where, WithMethods, XMLColumn, _addWith, _clone, _getSelectableColumn, _initQueryBuilder, _queryAfterSaveCommit, _queryAll, _queryChangeCounter, _queryCreate, _queryCreateForEachFrom, _queryCreateMany, _queryCreateManyFrom, _queryCreateOneFrom, _queryDefaults, _queryDelete, _queryExec, _queryFindBy, _queryFindByOptional, _queryGet, _queryGetOptional, _queryHookAfterCreate, _queryHookAfterCreateCommit, _queryHookAfterDelete, _queryHookAfterDeleteCommit, _queryHookAfterQuery, _queryHookAfterSave, _queryHookAfterUpdate, _queryHookAfterUpdateCommit, _queryHookBeforeCreate, _queryHookBeforeDelete, _queryHookBeforeQuery, _queryHookBeforeSave, _queryHookBeforeUpdate, _queryInsert, _queryInsertForEachFrom, _queryInsertMany, _queryInsertManyFrom, _queryInsertOneFrom, _queryJoinOn, _queryJoinOnJsonPathEquals, _queryJoinOrOn, _queryOr, _queryOrNot, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUnion, _queryUpdate, _queryUpdateOrThrow, _queryWhere, _queryWhereExists, _queryWhereIn, _queryWhereNot, _queryWhereNotOneOf, _queryWhereNotSql, _queryWhereOneOf, _queryWhereSql, _runAfterCommitHooks, addColumnParserToQuery, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, applyComputedColumns, assignDbDataToColumn, checkIfASimpleQuery, cloneQueryBaseUnscoped, columnCheckToCode, columnCode, columnExcludesToCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, commitSql, constraintInnerToCode, constraintToCode, copyTableData, countSelect, createDbWithAdapter, defaultSchemaConfig, escapeForLog, escapeForMigration, escapeString, excludeInnerToCode, excludeToCode, extendQuery, filterResult, foreignKeyArgumentToCode, getClonedQueryData, getColumnBaseType, getColumnInfo, getColumnTypes, getFullColumnTable, getQueryAs, getShapeFromSelect, getSqlText, handleResult, identityToCode, indexInnerToCode, indexToCode, isDefaultTimeStamp, isInUserTransaction, isQueryReturnsAll, isSelectingCount, joinSubQuery, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeFnExpression, moveQueryValueToWith, parseRecord, parseTableData, parseTableDataInput, postgisTypmodToSql, primaryKeyInnerToCode, processComputedBatches, processComputedResult, processSelectArg, pushLimitSQL, pushQueryArrayImmutable, pushQueryOn, pushQueryOnForOuter, pushQueryOrOn, pushTableDataCode, queryFrom, queryFromSql, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, raw, referencesArgsToCode, resolveSubQueryCallbackV2, rollbackSql, saveAliasedShape, setColumnDefaultParse, setColumnEncode, setColumnParse, setColumnParseNull, setParserForSelectedString, setQueryObjectValueImmutable, setQueryOperators, simplifyColumnDefault, sqlFn, sqlQueryArgsToExpression, tableDataMethods, templateLiteralToSQL, testTransaction, throwIfJoinLateral, throwIfNoWhere, toSQL };
|
|
13656
|
+
export { AfterCommitError, AggregateMethods, ArrayColumn, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ColumnRefExpression, ColumnType, ComputedColumn, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, Delete, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, ExpressionMethods, FnExpression, For, FromMethods, Having, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, Join, JsonMethods, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQueryMethods, MoneyColumn, NumberAsStringBaseColumn, NumberBaseColumn, OnMethods, Operators, OrExpression, PathColumn, PointColumn, PolygonColumn, PostgisGeographyPointColumn, QueryAsMethods, QueryGet, QueryHooks, QueryLog, QueryMethods, QueryUpsert, RawSQL, RealColumn, RefExpression, SearchMethods, Select, SerialColumn, SmallIntColumn, SmallSerialColumn, SqlMethod, StringColumn, TextBaseColumn, TextColumn, Then, TimeColumn, TimestampColumn, TimestampTZColumn, Transaction, TransformMethods, TsQueryColumn, TsVectorColumn, UUIDColumn, Union, UnknownColumn, Update, VarCharColumn, VirtualColumn, Where, WithMethods, XMLColumn, _addWith, _clone, _getSelectableColumn, _initQueryBuilder, _queryAfterSaveCommit, _queryAll, _queryChangeCounter, _queryCreate, _queryCreateForEachFrom, _queryCreateMany, _queryCreateManyFrom, _queryCreateOneFrom, _queryDefaults, _queryDelete, _queryExec, _queryFindBy, _queryFindByOptional, _queryGet, _queryGetOptional, _queryHookAfterCreate, _queryHookAfterCreateCommit, _queryHookAfterDelete, _queryHookAfterDeleteCommit, _queryHookAfterQuery, _queryHookAfterSave, _queryHookAfterUpdate, _queryHookAfterUpdateCommit, _queryHookBeforeCreate, _queryHookBeforeDelete, _queryHookBeforeQuery, _queryHookBeforeSave, _queryHookBeforeUpdate, _queryInsert, _queryInsertForEachFrom, _queryInsertMany, _queryInsertManyFrom, _queryInsertOneFrom, _queryJoinOn, _queryJoinOnJsonPathEquals, _queryJoinOrOn, _queryOr, _queryOrNot, _queryRows, _querySelect, _querySelectAll, _queryTake, _queryTakeOptional, _queryUnion, _queryUpdate, _queryUpdateOrThrow, _queryWhere, _queryWhereExists, _queryWhereIn, _queryWhereNot, _queryWhereNotOneOf, _queryWhereNotSql, _queryWhereOneOf, _queryWhereSql, _runAfterCommitHooks, addColumnParserToQuery, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, applyComputedColumns, assignDbDataToColumn, checkIfASimpleQuery, cloneQueryBaseUnscoped, columnCheckToCode, columnCode, columnExcludesToCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, commitSql, constraintInnerToCode, constraintToCode, copyTableData, countSelect, createDbWithAdapter, defaultSchemaConfig, escapeForLog, escapeForMigration, escapeString, excludeInnerToCode, excludeToCode, extendQuery, filterResult, foreignKeyArgumentToCode, getClonedQueryData, getColumnBaseType, getColumnInfo, getColumnTypes, getFullColumnTable, getQueryAs, getShapeFromSelect, getSqlText, handleResult, identityToCode, indexInnerToCode, indexToCode, isDefaultTimeStamp, isInUserTransaction, isQueryReturnsAll, isSelectingCount, joinSubQuery, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeFnExpression, moveQueryValueToWith, parseRecord, parseTableData, parseTableDataInput, postgisTypmodToSql, primaryKeyInnerToCode, processComputedBatches, processComputedResult, processSelectArg, pushLimitSQL, pushQueryArrayImmutable, pushQueryOn, pushQueryOnForOuter, pushQueryOrOn, pushTableDataCode, queryFrom, queryFromSql, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, raw, referencesArgsToCode, resolveSubQueryCallbackV2, rollbackSql, saveAliasedShape, setColumnDefaultParse, setColumnEncode, setColumnParse, setColumnParseNull, setParserForSelectedString, setQueryObjectValueImmutable, setQueryOperators, simplifyColumnDefault, sqlFn, sqlQueryArgsToExpression, tableDataMethods, templateLiteralToSQL, testTransaction, throwIfJoinLateral, throwIfNoWhere, toSQL };
|
|
13462
13657
|
//# sourceMappingURL=index.mjs.map
|