@zenstackhq/orm 3.9.0-beta.1 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +165 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +70 -15
- package/dist/index.d.mts +70 -15
- package/dist/index.mjs +165 -76
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/dist/index.mjs
CHANGED
|
@@ -24,26 +24,6 @@ function formatError(error) {
|
|
|
24
24
|
return fromError(error).toString();
|
|
25
25
|
}
|
|
26
26
|
//#endregion
|
|
27
|
-
//#region src/utils/object-utils.ts
|
|
28
|
-
/**
|
|
29
|
-
* Extract fields from an object.
|
|
30
|
-
*/
|
|
31
|
-
function extractFields(obj, fields) {
|
|
32
|
-
return Object.fromEntries(Object.entries(obj).filter(([key]) => fields.includes(key)));
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Create an object with fields as keys and true values.
|
|
36
|
-
*/
|
|
37
|
-
function fieldsToSelectObject(fields) {
|
|
38
|
-
return Object.fromEntries(fields.map((f) => [f, true]));
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Checks if the given value is an empty plain object.
|
|
42
|
-
*/
|
|
43
|
-
function isEmptyObject(x) {
|
|
44
|
-
return isPlainObject(x) && Object.keys(x).length === 0;
|
|
45
|
-
}
|
|
46
|
-
//#endregion
|
|
47
27
|
//#region src/client/executor/error-processor.ts
|
|
48
28
|
/**
|
|
49
29
|
* Extracts database error code from an error thrown by the database driver.
|
|
@@ -177,6 +157,26 @@ function createInternalError(message, model, options) {
|
|
|
177
157
|
return error;
|
|
178
158
|
}
|
|
179
159
|
//#endregion
|
|
160
|
+
//#region src/utils/object-utils.ts
|
|
161
|
+
/**
|
|
162
|
+
* Extract fields from an object.
|
|
163
|
+
*/
|
|
164
|
+
function extractFields(obj, fields) {
|
|
165
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => fields.includes(key)));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Create an object with fields as keys and true values.
|
|
169
|
+
*/
|
|
170
|
+
function fieldsToSelectObject(fields) {
|
|
171
|
+
return Object.fromEntries(fields.map((f) => [f, true]));
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Checks if the given value is an empty plain object.
|
|
175
|
+
*/
|
|
176
|
+
function isEmptyObject(x) {
|
|
177
|
+
return isPlainObject(x) && Object.keys(x).length === 0;
|
|
178
|
+
}
|
|
179
|
+
//#endregion
|
|
180
180
|
//#region src/client/query-utils.ts
|
|
181
181
|
var query_utils_exports = /* @__PURE__ */ __exportAll({
|
|
182
182
|
TEMP_ALIAS_PREFIX: () => "$$_",
|
|
@@ -696,6 +696,8 @@ var BaseCrudDialect = class {
|
|
|
696
696
|
if (typeof ob !== "object" || ob === null) return void 0;
|
|
697
697
|
if ("_fuzzyRelevance" in ob) return "_fuzzyRelevance";
|
|
698
698
|
if ("_ftsRelevance" in ob) return "_ftsRelevance";
|
|
699
|
+
const argsEntry = Object.entries(ob).find(([, v]) => v !== null && typeof v === "object" && "args" in v);
|
|
700
|
+
if (argsEntry) return argsEntry[0];
|
|
699
701
|
}).find((k) => k !== void 0);
|
|
700
702
|
if (offendingKey) throw createNotSupportedError(`cursor pagination cannot be combined with "${offendingKey}" ordering`);
|
|
701
703
|
}
|
|
@@ -718,9 +720,16 @@ var BaseCrudDialect = class {
|
|
|
718
720
|
const fieldDef = requireField(this.schema, model, key);
|
|
719
721
|
if (fieldDef.relation) result = this.and(result, this.buildRelationFilter(model, modelAlias, key, fieldDef, payload));
|
|
720
722
|
else {
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
723
|
+
let computedArgs;
|
|
724
|
+
let filterPayload = payload;
|
|
725
|
+
if (fieldDef.computed && fieldDef.params && payload && typeof payload === "object" && "args" in payload) {
|
|
726
|
+
computedArgs = payload.args;
|
|
727
|
+
const { args: _args, ...rest } = payload;
|
|
728
|
+
filterPayload = rest;
|
|
729
|
+
}
|
|
730
|
+
const fieldRef = this.fieldRef(fieldDef.originModel ?? model, key, fieldDef.originModel ?? modelAlias, true, computedArgs);
|
|
731
|
+
if (fieldDef.array) result = this.and(result, this.buildArrayFilter(fieldRef, fieldDef, filterPayload));
|
|
732
|
+
else result = this.and(result, this.buildPrimitiveFilter(fieldRef, fieldDef, filterPayload));
|
|
724
733
|
}
|
|
725
734
|
}
|
|
726
735
|
if ("$expr" in _where && typeof _where["$expr"] === "function") result = this.and(result, _where["$expr"](this.eb));
|
|
@@ -778,7 +787,7 @@ var BaseCrudDialect = class {
|
|
|
778
787
|
}
|
|
779
788
|
const joinAlias = tmpAlias(`${modelAlias}$${field}`);
|
|
780
789
|
const joinPairs = buildJoinPairs(this.schema, model, fieldDef.originModel ?? modelAlias, field, joinAlias);
|
|
781
|
-
const baseJoin = this.
|
|
790
|
+
const baseJoin = this.buildSelectModel(fieldDef.type, joinAlias).select(this.eb.lit(1).as("_")).where(() => this.and(...joinPairs.map(([left, right]) => this.eb(this.eb.ref(left), "=", this.eb.ref(right)))));
|
|
782
791
|
const existsSelect = (extraFilter) => {
|
|
783
792
|
const q = extraFilter ? baseJoin.where(extraFilter) : baseJoin;
|
|
784
793
|
return this.buildExistsExpression(q);
|
|
@@ -979,7 +988,7 @@ var BaseCrudDialect = class {
|
|
|
979
988
|
else if (fieldDef.array) clauses.push(this.buildArrayFilter(fieldReceiver, fieldDef, value));
|
|
980
989
|
else {
|
|
981
990
|
let _receiver = fieldReceiver;
|
|
982
|
-
if (fieldDef.type === "String") _receiver = this.trimTextQuotes(this.castText(fieldReceiver));
|
|
991
|
+
if (fieldDef.type === "String" || isEnum(this.schema, fieldDef.type)) _receiver = this.trimTextQuotes(this.castText(fieldReceiver));
|
|
983
992
|
clauses.push(this.buildPrimitiveFilter(_receiver, fieldDef, value));
|
|
984
993
|
}
|
|
985
994
|
}
|
|
@@ -1121,9 +1130,9 @@ var BaseCrudDialect = class {
|
|
|
1121
1130
|
buildOrderBy(query, model, modelAlias, orderBy, negated, take) {
|
|
1122
1131
|
if (!orderBy) return query;
|
|
1123
1132
|
let result = query;
|
|
1124
|
-
const buildFieldRef = (model, field, modelAlias) => {
|
|
1133
|
+
const buildFieldRef = (model, field, modelAlias, computedArgs) => {
|
|
1125
1134
|
const fieldDef = requireField(this.schema, model, field);
|
|
1126
|
-
return fieldDef.originModel ? this.fieldRef(fieldDef.originModel, field, fieldDef.originModel) : this.fieldRef(model, field, modelAlias);
|
|
1135
|
+
return fieldDef.originModel ? this.fieldRef(fieldDef.originModel, field, fieldDef.originModel, true, computedArgs) : this.fieldRef(model, field, modelAlias, true, computedArgs);
|
|
1127
1136
|
};
|
|
1128
1137
|
enumerate(orderBy).forEach((orderBy, index) => {
|
|
1129
1138
|
for (const [field, value] of Object.entries(orderBy)) {
|
|
@@ -1179,7 +1188,7 @@ var BaseCrudDialect = class {
|
|
|
1179
1188
|
return this.buildOrderBy(joined, relationModel, joinAlias, value, negated, take);
|
|
1180
1189
|
}
|
|
1181
1190
|
applyScalarOrderBy(query, model, modelAlias, field, value, negated, buildFieldRef) {
|
|
1182
|
-
const fieldRef = buildFieldRef(model, field, modelAlias);
|
|
1191
|
+
const fieldRef = buildFieldRef(model, field, modelAlias, value && typeof value === "object" && "args" in value ? value.args : void 0);
|
|
1183
1192
|
if (value === "asc" || value === "desc") return query.orderBy(fieldRef, this.negateSort(value, negated));
|
|
1184
1193
|
if (typeof value === "object" && "sort" in value && (value.sort === "asc" || value.sort === "desc")) {
|
|
1185
1194
|
const sort = this.negateSort(value.sort, negated);
|
|
@@ -1189,11 +1198,15 @@ var BaseCrudDialect = class {
|
|
|
1189
1198
|
return query;
|
|
1190
1199
|
}
|
|
1191
1200
|
applyAggregationOrderBy(query, model, modelAlias, field, value, negated, buildFieldRef) {
|
|
1192
|
-
|
|
1201
|
+
if (!isPlainObject(value)) throw createInvalidInputError(`invalid orderBy value for field "${field}"`);
|
|
1193
1202
|
let result = query;
|
|
1194
1203
|
for (const [k, v] of Object.entries(value)) {
|
|
1195
|
-
|
|
1196
|
-
|
|
1204
|
+
const sort = isPlainObject(v) ? v["sort"] : v;
|
|
1205
|
+
if (sort !== "asc" && sort !== "desc") throw createInvalidInputError(`invalid orderBy value for field "${field}"`);
|
|
1206
|
+
const computedArgs = isPlainObject(v) && "args" in v ? v["args"] : void 0;
|
|
1207
|
+
const aggExpr = aggregate(this.eb, buildFieldRef(model, k, modelAlias, computedArgs), field);
|
|
1208
|
+
const nulls = isPlainObject(v) ? v["nulls"] : void 0;
|
|
1209
|
+
result = nulls === "first" || nulls === "last" ? this.buildOrderByField(result, aggExpr, this.negateSort(sort, negated), nulls) : result.orderBy(aggExpr, this.negateSort(sort, negated));
|
|
1197
1210
|
}
|
|
1198
1211
|
return result;
|
|
1199
1212
|
}
|
|
@@ -1228,6 +1241,7 @@ var BaseCrudDialect = class {
|
|
|
1228
1241
|
computed: true
|
|
1229
1242
|
})) {
|
|
1230
1243
|
if (this.shouldOmitField(omit, model, fieldDef.name)) continue;
|
|
1244
|
+
if (fieldDef.computed && fieldDef.params) continue;
|
|
1231
1245
|
result = this.buildSelectField(result, model, modelAlias, fieldDef.name);
|
|
1232
1246
|
}
|
|
1233
1247
|
const descendants = getDelegateDescendantModels(this.schema, model);
|
|
@@ -1237,6 +1251,7 @@ var BaseCrudDialect = class {
|
|
|
1237
1251
|
const jsonObject = {};
|
|
1238
1252
|
for (const fieldDef of getModelFields(this.schema, subModel.name, { computed: true })) {
|
|
1239
1253
|
if (this.shouldOmitField(omit, subModel.name, fieldDef.name)) continue;
|
|
1254
|
+
if (fieldDef.computed && fieldDef.params) continue;
|
|
1240
1255
|
jsonObject[fieldDef.name] = this.fieldRef(subModel.name, fieldDef.name, subModel.name);
|
|
1241
1256
|
}
|
|
1242
1257
|
return this.buildJsonObject(jsonObject).as(`${DELEGATE_JOINED_FIELD_PREFIX}${subModel.name}`);
|
|
@@ -1257,11 +1272,11 @@ var BaseCrudDialect = class {
|
|
|
1257
1272
|
if (payload && typeof payload === "object") subQuery = this.buildFilterSortTake(model, payload, subQuery, subQueryAlias);
|
|
1258
1273
|
return subQuery;
|
|
1259
1274
|
}
|
|
1260
|
-
buildSelectField(query, model, modelAlias, field) {
|
|
1275
|
+
buildSelectField(query, model, modelAlias, field, computedArgs) {
|
|
1261
1276
|
const fieldDef = requireField(this.schema, model, field);
|
|
1262
1277
|
const fieldModel = fieldDef.originModel ?? model;
|
|
1263
1278
|
const alias = fieldDef.originModel ?? modelAlias;
|
|
1264
|
-
return query.select(() => this.fieldRef(fieldModel, field, alias).as(field));
|
|
1279
|
+
return query.select(() => this.fieldRef(fieldModel, field, alias, true, computedArgs).as(field));
|
|
1265
1280
|
}
|
|
1266
1281
|
buildDelegateJoin(thisModel, thisModelAlias, otherModelAlias, query) {
|
|
1267
1282
|
const idFields = requireIdFields(this.schema, thisModel);
|
|
@@ -1328,7 +1343,7 @@ var BaseCrudDialect = class {
|
|
|
1328
1343
|
not(...args) {
|
|
1329
1344
|
return this.eb.not(this.and(...args));
|
|
1330
1345
|
}
|
|
1331
|
-
fieldRef(model, field, modelAlias, inlineComputedField = true) {
|
|
1346
|
+
fieldRef(model, field, modelAlias, inlineComputedField = true, computedArgs) {
|
|
1332
1347
|
const fieldDef = requireField(this.schema, model, field);
|
|
1333
1348
|
if (!fieldDef.computed) return this.eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
1334
1349
|
else {
|
|
@@ -1340,7 +1355,7 @@ var BaseCrudDialect = class {
|
|
|
1340
1355
|
computer = computedFields?.[lowerCaseFirst(computedModel)]?.[field] ?? computedFields?.[computedModel]?.[field];
|
|
1341
1356
|
}
|
|
1342
1357
|
if (!computer) throw createConfigError(`Computed field "${field}" implementation not provided for model "${model}"`);
|
|
1343
|
-
return computer(this.eb, { modelAlias });
|
|
1358
|
+
return computer(this.eb, { modelAlias }, computedArgs);
|
|
1344
1359
|
}
|
|
1345
1360
|
}
|
|
1346
1361
|
canJoinWithoutNestedSelect(modelDef, payload) {
|
|
@@ -1476,18 +1491,21 @@ var LateralJoinDialectBase = class extends BaseCrudDialect {
|
|
|
1476
1491
|
if (descendantModels.length > 0) Object.assign(objArgs, ...descendantModels.map((subModel) => ({ [`${DELEGATE_JOINED_FIELD_PREFIX}${subModel.name}`]: eb.ref(`${DELEGATE_JOINED_FIELD_PREFIX}${subModel.name}`) })));
|
|
1477
1492
|
if (payload === true || !payload.select) {
|
|
1478
1493
|
const omit = typeof payload === "object" ? payload.omit : void 0;
|
|
1479
|
-
Object.assign(objArgs, ...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([name]) => !this.shouldOmitField(omit, relationModel, name)).map(([field]) => ({ [field]: this.fieldRef(relationModel, field, relationModelAlias, false) })));
|
|
1494
|
+
Object.assign(objArgs, ...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([, value]) => !(value.computed && value.params)).filter(([name]) => !this.shouldOmitField(omit, relationModel, name)).map(([field]) => ({ [field]: this.fieldRef(relationModel, field, relationModelAlias, false) })));
|
|
1480
1495
|
} else if (payload.select) Object.assign(objArgs, ...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
|
|
1481
1496
|
if (field === "_count") {
|
|
1482
1497
|
const subJson = this.buildCountJson(relationModel, eb, relationModelAlias, value);
|
|
1483
1498
|
return { [field]: subJson };
|
|
1484
1499
|
} else {
|
|
1485
|
-
const
|
|
1500
|
+
const fieldDef = requireField(this.schema, relationModel, field);
|
|
1501
|
+
const fieldValue = fieldDef.relation ? eb.ref(`${parentResultName}$${field}.$data`) : fieldDef.computed && fieldDef.params ? this.fieldRef(relationModel, field, relationModelAlias, true, value.args) : this.fieldRef(relationModel, field, relationModelAlias, false);
|
|
1486
1502
|
return { [field]: fieldValue };
|
|
1487
1503
|
}
|
|
1488
1504
|
}));
|
|
1489
1505
|
if (typeof payload === "object" && payload.include && typeof payload.include === "object") Object.assign(objArgs, ...Object.entries(payload.include).filter(([, value]) => value).map(([field, value]) => {
|
|
1490
1506
|
if (field === "_count") return { [field]: this.buildCountJson(relationModel, eb, relationModelAlias, value) };
|
|
1507
|
+
const fieldDef = requireField(this.schema, relationModel, field);
|
|
1508
|
+
if (fieldDef.computed && fieldDef.params) return { [field]: this.fieldRef(relationModel, field, relationModelAlias, true, value.args) };
|
|
1491
1509
|
return { [field]: eb.ref(`${parentResultName}$${field}.$data`) };
|
|
1492
1510
|
}));
|
|
1493
1511
|
return objArgs;
|
|
@@ -2153,21 +2171,27 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
2153
2171
|
if (descendantModels.length > 0) objArgs.push(...descendantModels.map((subModel) => [sql.lit(`${DELEGATE_JOINED_FIELD_PREFIX}${subModel.name}`), eb.ref(`${DELEGATE_JOINED_FIELD_PREFIX}${subModel.name}`)]).flatMap((v) => v));
|
|
2154
2172
|
if (payload === true || !payload.select) {
|
|
2155
2173
|
const omit = typeof payload === "object" ? payload.omit : void 0;
|
|
2156
|
-
objArgs.push(...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([name]) => !this.shouldOmitField(omit, relationModel, name)).map(([field]) => [sql.lit(field), this.fieldRef(relationModel, field, subQueryName, false)]).flatMap((v) => v));
|
|
2174
|
+
objArgs.push(...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([, value]) => !(value.computed && value.params)).filter(([name]) => !this.shouldOmitField(omit, relationModel, name)).map(([field]) => [sql.lit(field), this.fieldRef(relationModel, field, subQueryName, false)]).flatMap((v) => v));
|
|
2157
2175
|
} else if (payload.select) objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
|
|
2158
2176
|
if (field === "_count") {
|
|
2159
2177
|
const subJson = this.buildCountJson(relationModel, eb, tmpAlias(`${parentAlias}$${relationField}`), value);
|
|
2160
2178
|
return [sql.lit(field), subJson];
|
|
2161
|
-
} else
|
|
2162
|
-
const
|
|
2163
|
-
|
|
2164
|
-
|
|
2179
|
+
} else {
|
|
2180
|
+
const fieldDef = requireField(this.schema, relationModel, field);
|
|
2181
|
+
if (fieldDef.relation) {
|
|
2182
|
+
const subJson = this.buildRelationJSON(relationModel, eb, field, tmpAlias(`${parentAlias}$${relationField}`), value);
|
|
2183
|
+
return [sql.lit(field), subJson];
|
|
2184
|
+
} else if (fieldDef.computed && fieldDef.params) return [sql.lit(field), this.fieldRef(relationModel, field, subQueryName, true, value.args)];
|
|
2185
|
+
else return [sql.lit(field), this.fieldRef(relationModel, field, subQueryName, false)];
|
|
2186
|
+
}
|
|
2165
2187
|
}).flatMap((v) => v));
|
|
2166
2188
|
if (typeof payload === "object" && payload.include && typeof payload.include === "object") objArgs.push(...Object.entries(payload.include).filter(([, value]) => value).map(([field, value]) => {
|
|
2167
2189
|
if (field === "_count") {
|
|
2168
2190
|
const subJson = this.buildCountJson(relationModel, eb, tmpAlias(`${parentAlias}$${relationField}`), value);
|
|
2169
2191
|
return [sql.lit(field), subJson];
|
|
2170
2192
|
}
|
|
2193
|
+
const fieldDef = requireField(this.schema, relationModel, field);
|
|
2194
|
+
if (fieldDef.computed && fieldDef.params) return [sql.lit(field), this.fieldRef(relationModel, field, subQueryName, true, value.args)];
|
|
2171
2195
|
const subJson = this.buildRelationJSON(relationModel, eb, field, tmpAlias(`${parentAlias}$${relationField}`), value);
|
|
2172
2196
|
return [sql.lit(field), subJson];
|
|
2173
2197
|
}).flatMap((v) => v));
|
|
@@ -2516,6 +2540,25 @@ var ZodSchemaFactory = class {
|
|
|
2516
2540
|
]);
|
|
2517
2541
|
}).with("DateTime", () => this.makeDateTimeValueSchema()).with("Bytes", () => z$1.instanceof(Uint8Array)).with("Json", () => this.makeJsonValueSchema()).otherwise(() => z$1.unknown());
|
|
2518
2542
|
}
|
|
2543
|
+
isParameterizedComputedField(fieldDef) {
|
|
2544
|
+
return !!(fieldDef.computed && fieldDef.params);
|
|
2545
|
+
}
|
|
2546
|
+
makeFieldArgsSchema(params) {
|
|
2547
|
+
return z$1.strictObject(Object.fromEntries(Object.entries(params).map(([name, param]) => {
|
|
2548
|
+
let paramSchema = this.makeScalarSchema(param.type);
|
|
2549
|
+
if (param.array) paramSchema = paramSchema.array();
|
|
2550
|
+
if (param.optional) paramSchema = paramSchema.optional();
|
|
2551
|
+
return [name, paramSchema];
|
|
2552
|
+
})));
|
|
2553
|
+
}
|
|
2554
|
+
addComputedArgsToFilter(filterSchema, params) {
|
|
2555
|
+
const objectMembers = (filterSchema instanceof z$1.ZodUnion ? filterSchema.options : [filterSchema]).filter((m) => m instanceof z$1.ZodObject);
|
|
2556
|
+
const components = Object.assign({}, ...objectMembers.map((o) => o.shape));
|
|
2557
|
+
return z$1.strictObject({
|
|
2558
|
+
args: this.makeFieldArgsSchema(params),
|
|
2559
|
+
...components
|
|
2560
|
+
});
|
|
2561
|
+
}
|
|
2519
2562
|
makeEnumSchema(_enum) {
|
|
2520
2563
|
const enumDef = getEnum(this.schema, _enum);
|
|
2521
2564
|
invariant(enumDef, `Enum "${_enum}" not found in schema`);
|
|
@@ -2573,6 +2616,7 @@ var ZodSchemaFactory = class {
|
|
|
2573
2616
|
else if (this.isTypeDefType(fieldDef.type)) fieldSchema = this.makeTypedJsonFilterSchema(fieldDef.type, !!fieldDef.optional, !!fieldDef.array, allowedFilterKinds);
|
|
2574
2617
|
else fieldSchema = this.makePrimitiveFilterSchema(fieldDef.type, !!fieldDef.optional, withAggregations, allowedFilterKinds, !!fieldDef.fuzzy, !!fieldDef.fullText);
|
|
2575
2618
|
}
|
|
2619
|
+
if (fieldSchema && this.isParameterizedComputedField(fieldDef)) fieldSchema = this.addComputedArgsToFilter(fieldSchema, fieldDef.params);
|
|
2576
2620
|
if (fieldSchema) fields[field] = fieldSchema.optional();
|
|
2577
2621
|
}
|
|
2578
2622
|
if (unique) {
|
|
@@ -2890,11 +2934,17 @@ var ZodSchemaFactory = class {
|
|
|
2890
2934
|
}
|
|
2891
2935
|
makeSelectSchema(model, options) {
|
|
2892
2936
|
const fields = {};
|
|
2893
|
-
for (const [field, fieldDef] of this.getModelFields(model))
|
|
2894
|
-
if (
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2937
|
+
for (const [field, fieldDef] of this.getModelFields(model)) {
|
|
2938
|
+
if (this.isParameterizedComputedField(fieldDef)) {
|
|
2939
|
+
fields[field] = z$1.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional();
|
|
2940
|
+
continue;
|
|
2941
|
+
}
|
|
2942
|
+
if (fieldDef.relation) {
|
|
2943
|
+
if (!this.shouldIncludeRelations(options)) continue;
|
|
2944
|
+
if (this.isModelAllowed(fieldDef.type)) fields[field] = this.makeRelationSelectIncludeSchema(model, field, options).optional();
|
|
2945
|
+
} else if (this.options.allowQueryTimeOmitOverride === false && this.isFieldOmittedByConfig(model, field)) fields[field] = z$1.literal(false).optional();
|
|
2946
|
+
else fields[field] = z$1.boolean().optional();
|
|
2947
|
+
}
|
|
2898
2948
|
if (this.shouldIncludeRelations(options)) {
|
|
2899
2949
|
const _countSchema = this.makeCountSelectionSchema(model, options);
|
|
2900
2950
|
if (!(_countSchema instanceof z$1.ZodNever)) fields["_count"] = _countSchema;
|
|
@@ -2951,8 +3001,11 @@ var ZodSchemaFactory = class {
|
|
|
2951
3001
|
}
|
|
2952
3002
|
makeOmitSchema(model) {
|
|
2953
3003
|
const fields = {};
|
|
2954
|
-
for (const [field, fieldDef] of this.getModelFields(model))
|
|
2955
|
-
|
|
3004
|
+
for (const [field, fieldDef] of this.getModelFields(model)) {
|
|
3005
|
+
if (this.isParameterizedComputedField(fieldDef)) continue;
|
|
3006
|
+
if (!fieldDef.relation) if (this.options.allowQueryTimeOmitOverride !== false) fields[field] = z$1.boolean().optional();
|
|
3007
|
+
else fields[field] = z$1.literal(true).optional();
|
|
3008
|
+
}
|
|
2956
3009
|
this.addExtResultFields(model, fields);
|
|
2957
3010
|
const result = z$1.strictObject(fields);
|
|
2958
3011
|
this.registerSchema(`${model}OmitInput`, result);
|
|
@@ -2975,7 +3028,7 @@ var ZodSchemaFactory = class {
|
|
|
2975
3028
|
if (fieldDef.relation) {
|
|
2976
3029
|
if (!this.shouldIncludeRelations(options)) continue;
|
|
2977
3030
|
if (this.isModelAllowed(fieldDef.type)) fields[field] = this.makeRelationSelectIncludeSchema(model, field, options).optional();
|
|
2978
|
-
}
|
|
3031
|
+
} else if (this.isParameterizedComputedField(fieldDef)) fields[field] = z$1.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional();
|
|
2979
3032
|
}
|
|
2980
3033
|
if (this.shouldIncludeRelations(options)) {
|
|
2981
3034
|
const _countSchema = this.makeCountSelectionSchema(model, options);
|
|
@@ -2996,7 +3049,12 @@ var ZodSchemaFactory = class {
|
|
|
2996
3049
|
if (fieldDef.array) relationOrderBy = refineAtMostOneKey(relationOrderBy.safeExtend({ _count: sort }));
|
|
2997
3050
|
return relationOrderBy;
|
|
2998
3051
|
}).optional();
|
|
2999
|
-
} else if (fieldDef.
|
|
3052
|
+
} else if (fieldDef.computed && fieldDef.params) fields[field] = z$1.strictObject({
|
|
3053
|
+
args: this.makeFieldArgsSchema(fieldDef.params),
|
|
3054
|
+
sort,
|
|
3055
|
+
nulls: z$1.union([z$1.literal("first"), z$1.literal("last")]).optional()
|
|
3056
|
+
}).optional();
|
|
3057
|
+
else if (fieldDef.optional) fields[field] = z$1.union([sort, z$1.strictObject({
|
|
3000
3058
|
sort,
|
|
3001
3059
|
nulls: z$1.union([z$1.literal("first"), z$1.literal("last")]).optional()
|
|
3002
3060
|
})]).optional();
|
|
@@ -3040,7 +3098,7 @@ var ZodSchemaFactory = class {
|
|
|
3040
3098
|
return schema;
|
|
3041
3099
|
}
|
|
3042
3100
|
makeDistinctSchema(model) {
|
|
3043
|
-
const nonRelationFields = this.getModelFields(model).filter(([, def]) => !def.relation).map(([name]) => name);
|
|
3101
|
+
const nonRelationFields = this.getModelFields(model).filter(([, def]) => !def.relation && !this.isParameterizedComputedField(def)).map(([name]) => name);
|
|
3044
3102
|
const schema = nonRelationFields.length > 0 ? this.orArray(z$1.enum(nonRelationFields), true) : z$1.never();
|
|
3045
3103
|
this.registerSchema(`${model}DistinctInput`, schema);
|
|
3046
3104
|
return schema;
|
|
@@ -3361,8 +3419,8 @@ var ZodSchemaFactory = class {
|
|
|
3361
3419
|
makeCountAggregateInputSchema(model) {
|
|
3362
3420
|
const schema = z$1.union([z$1.literal(true), z$1.strictObject({
|
|
3363
3421
|
_all: z$1.literal(true).optional(),
|
|
3364
|
-
...this.getModelFields(model).reduce((acc, [field]) => {
|
|
3365
|
-
acc[field] = z$1.literal(true).optional();
|
|
3422
|
+
...this.getModelFields(model).reduce((acc, [field, fieldDef]) => {
|
|
3423
|
+
acc[field] = this.isParameterizedComputedField(fieldDef) ? z$1.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional() : z$1.literal(true).optional();
|
|
3366
3424
|
return acc;
|
|
3367
3425
|
}, {})
|
|
3368
3426
|
})]);
|
|
@@ -3386,7 +3444,7 @@ var ZodSchemaFactory = class {
|
|
|
3386
3444
|
}
|
|
3387
3445
|
makeSumAvgInputSchema(model) {
|
|
3388
3446
|
const schema = z$1.strictObject(this.getModelFields(model).reduce((acc, [field, fieldDef]) => {
|
|
3389
|
-
if (this.isNumericField(fieldDef)) acc[field] = z$1.literal(true).optional();
|
|
3447
|
+
if (this.isNumericField(fieldDef)) acc[field] = this.isParameterizedComputedField(fieldDef) ? z$1.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional() : z$1.literal(true).optional();
|
|
3390
3448
|
return acc;
|
|
3391
3449
|
}, {}));
|
|
3392
3450
|
this.registerSchema(`${model}SumAvgAggregateInput`, schema);
|
|
@@ -3394,15 +3452,22 @@ var ZodSchemaFactory = class {
|
|
|
3394
3452
|
}
|
|
3395
3453
|
makeMinMaxInputSchema(model) {
|
|
3396
3454
|
const schema = z$1.strictObject(this.getModelFields(model).reduce((acc, [field, fieldDef]) => {
|
|
3397
|
-
if (!fieldDef.relation && !fieldDef.array) acc[field] = z$1.literal(true).optional();
|
|
3455
|
+
if (!fieldDef.relation && !fieldDef.array) acc[field] = this.isParameterizedComputedField(fieldDef) ? z$1.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional() : z$1.literal(true).optional();
|
|
3398
3456
|
return acc;
|
|
3399
3457
|
}, {}));
|
|
3400
3458
|
this.registerSchema(`${model}MinMaxAggregateInput`, schema);
|
|
3401
3459
|
return schema;
|
|
3402
3460
|
}
|
|
3403
3461
|
makeGroupBySchema(model, options) {
|
|
3404
|
-
const nonRelationFields = this.getModelFields(model).filter(([, def]) => !def.relation).map(([name]) => name);
|
|
3405
|
-
const
|
|
3462
|
+
const nonRelationFields = this.getModelFields(model).filter(([, def]) => !def.relation && !this.isParameterizedComputedField(def)).map(([name]) => name);
|
|
3463
|
+
const byMembers = [];
|
|
3464
|
+
if (nonRelationFields.length > 0) byMembers.push(z$1.enum(nonRelationFields));
|
|
3465
|
+
for (const [field, fieldDef] of this.getModelFields(model)) if (this.isParameterizedComputedField(fieldDef)) byMembers.push(z$1.strictObject({
|
|
3466
|
+
field: z$1.literal(field),
|
|
3467
|
+
args: this.makeFieldArgsSchema(fieldDef.params)
|
|
3468
|
+
}));
|
|
3469
|
+
const byElement = byMembers.length === 0 ? void 0 : byMembers.length === 1 ? byMembers[0] : z$1.union(byMembers);
|
|
3470
|
+
const bySchema = byElement ? this.orArray(byElement, true) : z$1.never();
|
|
3406
3471
|
const baseSchema = z$1.strictObject({
|
|
3407
3472
|
where: this.makeWhereSchema(model, false, false, false, options).optional(),
|
|
3408
3473
|
orderBy: this.orArray(this.makeOrderBySchema(model, false, true, options), true).optional(),
|
|
@@ -3418,7 +3483,7 @@ var ZodSchemaFactory = class {
|
|
|
3418
3483
|
});
|
|
3419
3484
|
let schema = this.mergePluginArgsSchema(baseSchema, "groupBy");
|
|
3420
3485
|
schema = schema.refine((value) => {
|
|
3421
|
-
const bys = enumerate(value.by);
|
|
3486
|
+
const bys = enumerate(value.by).map((b) => typeof b === "string" ? b : b?.field);
|
|
3422
3487
|
if (value.having && typeof value.having === "object") for (const [key, val] of Object.entries(value.having)) {
|
|
3423
3488
|
if (AggregateOperators.includes(key)) continue;
|
|
3424
3489
|
if (bys.includes(key)) continue;
|
|
@@ -3428,7 +3493,7 @@ var ZodSchemaFactory = class {
|
|
|
3428
3493
|
return true;
|
|
3429
3494
|
}, "fields in \"having\" must be in \"by\"");
|
|
3430
3495
|
schema = schema.refine((value) => {
|
|
3431
|
-
const bys = enumerate(value.by);
|
|
3496
|
+
const bys = enumerate(value.by).map((b) => typeof b === "string" ? b : b?.field);
|
|
3432
3497
|
for (const orderBy of enumerate(value.orderBy)) if (orderBy && Object.keys(orderBy).filter((f) => !AggregateOperators.includes(f)).some((key) => !bys.includes(key))) return false;
|
|
3433
3498
|
return true;
|
|
3434
3499
|
}, "fields in \"orderBy\" must be in \"by\"");
|
|
@@ -4329,8 +4394,10 @@ var BaseOperationHandler = class {
|
|
|
4329
4394
|
continue;
|
|
4330
4395
|
}
|
|
4331
4396
|
const fieldDef = this.requireField(model, field);
|
|
4332
|
-
if (!fieldDef.relation)
|
|
4333
|
-
|
|
4397
|
+
if (!fieldDef.relation) {
|
|
4398
|
+
const computedArgs = fieldDef.computed && fieldDef.params && typeof payload === "object" && "args" in payload ? payload.args : void 0;
|
|
4399
|
+
result = this.dialect.buildSelectField(result, model, parentAlias, field, computedArgs);
|
|
4400
|
+
} else {
|
|
4334
4401
|
if (!fieldDef.array && !fieldDef.optional && payload.where) throw createInternalError(`Field "${field}" does not support filtering`, model);
|
|
4335
4402
|
if (fieldDef.originModel) result = this.dialect.buildRelationSelection(result, fieldDef.originModel, field, fieldDef.originModel, payload);
|
|
4336
4403
|
else result = this.dialect.buildRelationSelection(result, model, field, parentAlias, payload);
|
|
@@ -5312,6 +5379,7 @@ var BaseOperationHandler = class {
|
|
|
5312
5379
|
};
|
|
5313
5380
|
//#endregion
|
|
5314
5381
|
//#region src/client/crud/operations/aggregate.ts
|
|
5382
|
+
const argsKey = (v) => JSON.stringify(v, (_k, val) => typeof val === "bigint" ? `${val}n` : val);
|
|
5315
5383
|
var AggregateOperationHandler = class extends BaseOperationHandler {
|
|
5316
5384
|
async handle(_operation, args) {
|
|
5317
5385
|
const normalizedArgs = this.normalizeArgs(args);
|
|
@@ -5319,10 +5387,18 @@ var AggregateOperationHandler = class extends BaseOperationHandler {
|
|
|
5319
5387
|
let query = this.kysely.selectFrom((eb) => {
|
|
5320
5388
|
let subQuery = this.dialect.buildSelectModel(this.model, this.model).where(() => this.dialect.buildFilter(this.model, this.model, parsedArgs?.where));
|
|
5321
5389
|
const selectedFields = [];
|
|
5322
|
-
|
|
5390
|
+
const computedArgsByField = {};
|
|
5391
|
+
for (const [key, value] of Object.entries(parsedArgs)) if (key.startsWith("_") && value && typeof value === "object") Object.entries(value).filter(([field]) => field !== "_all").forEach(([field, val]) => {
|
|
5392
|
+
const args = val && typeof val === "object" && "args" in val ? val.args : void 0;
|
|
5393
|
+
if (val !== true && args === void 0) return;
|
|
5323
5394
|
if (!selectedFields.includes(field)) selectedFields.push(field);
|
|
5395
|
+
if (args !== void 0) {
|
|
5396
|
+
const prev = computedArgsByField[field];
|
|
5397
|
+
if (prev !== void 0 && argsKey(prev) !== argsKey(args)) throw createInvalidInputError(`computed field "${field}" is aggregated with conflicting "args"; the same field can only be aggregated with one set of arguments per query`);
|
|
5398
|
+
computedArgsByField[field] = args;
|
|
5399
|
+
}
|
|
5324
5400
|
});
|
|
5325
|
-
if (selectedFields.length > 0) for (const field of selectedFields) subQuery = this.dialect.buildSelectField(subQuery, this.model, this.model, field);
|
|
5401
|
+
if (selectedFields.length > 0) for (const field of selectedFields) subQuery = this.dialect.buildSelectField(subQuery, this.model, this.model, field, computedArgsByField[field]);
|
|
5326
5402
|
else subQuery = subQuery.select(() => eb.lit(1).as("_all"));
|
|
5327
5403
|
const skip = parsedArgs?.skip;
|
|
5328
5404
|
let take = parsedArgs?.take;
|
|
@@ -5339,7 +5415,7 @@ var AggregateOperationHandler = class extends BaseOperationHandler {
|
|
|
5339
5415
|
case "_count":
|
|
5340
5416
|
if (value === true) query = query.select((eb) => this.dialect.castInt(eb.fn.countAll()).as("_count"));
|
|
5341
5417
|
else Object.entries(value).forEach(([field, val]) => {
|
|
5342
|
-
if (val === true) if (field === "_all") query = query.select((eb) => this.dialect.castInt(eb.fn.countAll()).as(`_count._all`));
|
|
5418
|
+
if (val === true || val && typeof val === "object" && "args" in val) if (field === "_all") query = query.select((eb) => this.dialect.castInt(eb.fn.countAll()).as(`_count._all`));
|
|
5343
5419
|
else query = query.select((eb) => this.dialect.castInt(eb.fn.count(eb.ref(`$sub.${field}`))).as(`${key}.${field}`));
|
|
5344
5420
|
});
|
|
5345
5421
|
break;
|
|
@@ -5348,7 +5424,7 @@ var AggregateOperationHandler = class extends BaseOperationHandler {
|
|
|
5348
5424
|
case "_max":
|
|
5349
5425
|
case "_min":
|
|
5350
5426
|
Object.entries(value).forEach(([field, val]) => {
|
|
5351
|
-
if (val === true) query = query.select((eb) => {
|
|
5427
|
+
if (val === true || val && typeof val === "object" && "args" in val) query = query.select((eb) => {
|
|
5352
5428
|
return match(key).with("_sum", () => eb.fn.sum).with("_avg", () => eb.fn.avg).with("_max", () => eb.fn.max).with("_min", () => eb.fn.min).exhaustive()(eb.ref(`$sub.${field}`)).as(`${key}.${field}`);
|
|
5353
5429
|
});
|
|
5354
5430
|
});
|
|
@@ -5391,9 +5467,12 @@ var CountOperationHandler = class extends BaseOperationHandler {
|
|
|
5391
5467
|
const subQueryName = "$sub";
|
|
5392
5468
|
let query = this.kysely.selectFrom((eb) => {
|
|
5393
5469
|
let subQuery = this.dialect.buildSelectModel(this.model, this.model).where(() => this.dialect.buildFilter(this.model, this.model, parsedArgs?.where));
|
|
5394
|
-
if (parsedArgs?.select && typeof parsedArgs.select === "object") {
|
|
5395
|
-
|
|
5396
|
-
|
|
5470
|
+
if (parsedArgs?.select && typeof parsedArgs.select === "object") for (const [key, value] of Object.entries(parsedArgs.select)) {
|
|
5471
|
+
if (key === "_all") continue;
|
|
5472
|
+
if (value === true) subQuery = this.dialect.buildSelectField(subQuery, this.model, this.model, key);
|
|
5473
|
+
else if (value && typeof value === "object" && "args" in value) subQuery = this.dialect.buildSelectField(subQuery, this.model, this.model, key, value.args);
|
|
5474
|
+
}
|
|
5475
|
+
else subQuery = subQuery.select(() => eb.lit(1).as("_all"));
|
|
5397
5476
|
subQuery = this.dialect.buildSkipTake(subQuery, parsedArgs?.skip, parsedArgs?.take);
|
|
5398
5477
|
return subQuery.as(subQueryName);
|
|
5399
5478
|
});
|
|
@@ -5534,9 +5613,17 @@ var GroupByOperationHandler = class extends BaseOperationHandler {
|
|
|
5534
5613
|
const normalizedArgs = this.normalizeArgs(args);
|
|
5535
5614
|
const parsedArgs = this.inputValidator.validateGroupByArgs(this.model, normalizedArgs);
|
|
5536
5615
|
let query = this.kysely.selectFrom(this.model).where(() => this.dialect.buildFilter(this.model, this.model, parsedArgs?.where));
|
|
5537
|
-
const fieldRef = (field) => this.dialect.fieldRef(this.model, field);
|
|
5538
|
-
const
|
|
5539
|
-
|
|
5616
|
+
const fieldRef = (field, computedArgs) => this.dialect.fieldRef(this.model, field, this.model, true, computedArgs);
|
|
5617
|
+
const byEntries = (Array.isArray(parsedArgs.by) ? parsedArgs.by : [parsedArgs.by]).map((by) => typeof by === "string" ? {
|
|
5618
|
+
field: by,
|
|
5619
|
+
args: void 0
|
|
5620
|
+
} : {
|
|
5621
|
+
field: by.field,
|
|
5622
|
+
args: by.args
|
|
5623
|
+
});
|
|
5624
|
+
query = query.groupBy(byEntries.map((e) => {
|
|
5625
|
+
return getField(this.schema, this.model, e.field)?.computed ? sql.ref(e.field) : fieldRef(e.field);
|
|
5626
|
+
}));
|
|
5540
5627
|
const skip = parsedArgs?.skip;
|
|
5541
5628
|
let take = parsedArgs?.take;
|
|
5542
5629
|
let negateOrderBy = false;
|
|
@@ -5547,13 +5634,14 @@ var GroupByOperationHandler = class extends BaseOperationHandler {
|
|
|
5547
5634
|
query = this.dialect.buildSkipTake(query, skip, take);
|
|
5548
5635
|
query = this.dialect.buildOrderBy(query, this.model, this.model, parsedArgs.orderBy, negateOrderBy, take);
|
|
5549
5636
|
if (parsedArgs.having) query = query.having(() => this.dialect.buildFilter(this.model, this.model, parsedArgs.having));
|
|
5550
|
-
for (const
|
|
5637
|
+
for (const e of byEntries) query = query.select(() => fieldRef(e.field, e.args).as(e.field));
|
|
5551
5638
|
for (const [key, value] of Object.entries(parsedArgs)) switch (key) {
|
|
5552
5639
|
case "_count":
|
|
5553
5640
|
if (value === true) query = query.select((eb) => this.dialect.castInt(eb.fn.countAll()).as("_count"));
|
|
5554
5641
|
else Object.entries(value).forEach(([field, val]) => {
|
|
5555
|
-
|
|
5556
|
-
|
|
5642
|
+
const args = val && typeof val === "object" && "args" in val ? val.args : void 0;
|
|
5643
|
+
if (val === true || args !== void 0) if (field === "_all") query = query.select((eb) => this.dialect.castInt(eb.fn.countAll()).as(`_count._all`));
|
|
5644
|
+
else query = query.select((eb) => this.dialect.castInt(eb.fn.count(fieldRef(field, args))).as(`${key}.${field}`));
|
|
5557
5645
|
});
|
|
5558
5646
|
break;
|
|
5559
5647
|
case "_sum":
|
|
@@ -5561,7 +5649,8 @@ var GroupByOperationHandler = class extends BaseOperationHandler {
|
|
|
5561
5649
|
case "_max":
|
|
5562
5650
|
case "_min":
|
|
5563
5651
|
Object.entries(value).forEach(([field, val]) => {
|
|
5564
|
-
|
|
5652
|
+
const args = val && typeof val === "object" && "args" in val ? val.args : void 0;
|
|
5653
|
+
if (val === true || args !== void 0) query = query.select((eb) => aggregate(eb, fieldRef(field, args), key).as(`${key}.${field}`));
|
|
5565
5654
|
});
|
|
5566
5655
|
break;
|
|
5567
5656
|
}
|