@zenstackhq/runtime 3.0.0-beta.10 → 3.0.0-beta.11
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 +93 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -7
- package/dist/index.d.ts +9 -7
- package/dist/index.js +93 -71
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -62,7 +62,6 @@ var import_ts_pattern7 = require("ts-pattern");
|
|
|
62
62
|
var query_utils_exports = {};
|
|
63
63
|
__export(query_utils_exports, {
|
|
64
64
|
aggregate: () => aggregate,
|
|
65
|
-
buildFieldRef: () => buildFieldRef,
|
|
66
65
|
buildJoinPairs: () => buildJoinPairs,
|
|
67
66
|
ensureArray: () => ensureArray,
|
|
68
67
|
extractFieldName: () => extractFieldName,
|
|
@@ -408,28 +407,6 @@ function getIdValues(schema, model, data) {
|
|
|
408
407
|
}), {});
|
|
409
408
|
}
|
|
410
409
|
__name(getIdValues, "getIdValues");
|
|
411
|
-
function buildFieldRef(schema, model, field, options, eb, modelAlias, inlineComputedField = true) {
|
|
412
|
-
const fieldDef = requireField(schema, model, field);
|
|
413
|
-
if (!fieldDef.computed) {
|
|
414
|
-
return eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
415
|
-
} else {
|
|
416
|
-
if (!inlineComputedField) {
|
|
417
|
-
return eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
418
|
-
}
|
|
419
|
-
let computer;
|
|
420
|
-
if ("computedFields" in options) {
|
|
421
|
-
const computedFields = options.computedFields;
|
|
422
|
-
computer = computedFields?.[model]?.[field];
|
|
423
|
-
}
|
|
424
|
-
if (!computer) {
|
|
425
|
-
throw new QueryError(`Computed field "${field}" implementation not provided for model "${model}"`);
|
|
426
|
-
}
|
|
427
|
-
return computer(eb, {
|
|
428
|
-
modelAlias
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
__name(buildFieldRef, "buildFieldRef");
|
|
433
410
|
function fieldHasDefaultValue(fieldDef) {
|
|
434
411
|
return fieldDef.default !== void 0 || fieldDef.updatedAt;
|
|
435
412
|
}
|
|
@@ -781,7 +758,7 @@ var BaseCrudDialect = class {
|
|
|
781
758
|
take = -take;
|
|
782
759
|
}
|
|
783
760
|
result = this.buildSkipTake(result, skip, take);
|
|
784
|
-
result = this.buildOrderBy(result, model, modelAlias, args.orderBy,
|
|
761
|
+
result = this.buildOrderBy(result, model, modelAlias, args.orderBy, negateOrderBy);
|
|
785
762
|
if ("distinct" in args && args.distinct) {
|
|
786
763
|
const distinct = ensureArray(args.distinct);
|
|
787
764
|
if (this.supportsDistinctOn) {
|
|
@@ -1140,16 +1117,12 @@ var BaseCrudDialect = class {
|
|
|
1140
1117
|
]);
|
|
1141
1118
|
return this.and(...conditions.conditions);
|
|
1142
1119
|
}
|
|
1143
|
-
buildOrderBy(query, model, modelAlias, orderBy,
|
|
1120
|
+
buildOrderBy(query, model, modelAlias, orderBy, negated) {
|
|
1144
1121
|
if (!orderBy) {
|
|
1145
|
-
|
|
1146
|
-
orderBy = makeDefaultOrderBy(this.schema, model);
|
|
1147
|
-
} else {
|
|
1148
|
-
return query;
|
|
1149
|
-
}
|
|
1122
|
+
return query;
|
|
1150
1123
|
}
|
|
1151
1124
|
let result = query;
|
|
1152
|
-
const
|
|
1125
|
+
const buildFieldRef = /* @__PURE__ */ __name((model2, field, modelAlias2) => {
|
|
1153
1126
|
const fieldDef = requireField(this.schema, model2, field);
|
|
1154
1127
|
return fieldDef.originModel ? this.fieldRef(fieldDef.originModel, field, fieldDef.originModel) : this.fieldRef(model2, field, modelAlias2);
|
|
1155
1128
|
}, "buildFieldRef");
|
|
@@ -1168,7 +1141,7 @@ var BaseCrudDialect = class {
|
|
|
1168
1141
|
(0, import_common_helpers3.invariant)(value && typeof value === "object", `invalid orderBy value for field "${field}"`);
|
|
1169
1142
|
for (const [k, v] of Object.entries(value)) {
|
|
1170
1143
|
(0, import_common_helpers3.invariant)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
|
|
1171
|
-
result = result.orderBy((eb) => aggregate(eb,
|
|
1144
|
+
result = result.orderBy((eb) => aggregate(eb, buildFieldRef(model, k, modelAlias), field), import_kysely2.sql.raw(this.negateSort(v, negated)));
|
|
1172
1145
|
}
|
|
1173
1146
|
continue;
|
|
1174
1147
|
}
|
|
@@ -1177,7 +1150,7 @@ var BaseCrudDialect = class {
|
|
|
1177
1150
|
(0, import_common_helpers3.invariant)(value && typeof value === "object", 'invalid orderBy value for field "_count"');
|
|
1178
1151
|
for (const [k, v] of Object.entries(value)) {
|
|
1179
1152
|
(0, import_common_helpers3.invariant)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
|
|
1180
|
-
result = result.orderBy((eb) => eb.fn.count(
|
|
1153
|
+
result = result.orderBy((eb) => eb.fn.count(buildFieldRef(model, k, modelAlias)), import_kysely2.sql.raw(this.negateSort(v, negated)));
|
|
1181
1154
|
}
|
|
1182
1155
|
continue;
|
|
1183
1156
|
}
|
|
@@ -1186,7 +1159,7 @@ var BaseCrudDialect = class {
|
|
|
1186
1159
|
}
|
|
1187
1160
|
const fieldDef = requireField(this.schema, model, field);
|
|
1188
1161
|
if (!fieldDef.relation) {
|
|
1189
|
-
const fieldRef =
|
|
1162
|
+
const fieldRef = buildFieldRef(model, field, modelAlias);
|
|
1190
1163
|
if (value === "asc" || value === "desc") {
|
|
1191
1164
|
result = result.orderBy(fieldRef, this.negateSort(value, negated));
|
|
1192
1165
|
} else if (value && typeof value === "object" && "nulls" in value && "sort" in value && (value.sort === "asc" || value.sort === "desc") && (value.nulls === "first" || value.nulls === "last")) {
|
|
@@ -1215,7 +1188,7 @@ var BaseCrudDialect = class {
|
|
|
1215
1188
|
const joinPairs = buildJoinPairs(this.schema, model, modelAlias, field, relationModel);
|
|
1216
1189
|
return join.on((eb) => this.and(...joinPairs.map(([left, right]) => eb(this.eb.ref(left), "=", this.eb.ref(right)))));
|
|
1217
1190
|
});
|
|
1218
|
-
result = this.buildOrderBy(result, fieldDef.type, relationModel, value,
|
|
1191
|
+
result = this.buildOrderBy(result, fieldDef.type, relationModel, value, negated);
|
|
1219
1192
|
}
|
|
1220
1193
|
}
|
|
1221
1194
|
}
|
|
@@ -1262,13 +1235,9 @@ var BaseCrudDialect = class {
|
|
|
1262
1235
|
}
|
|
1263
1236
|
buildSelectField(query, model, modelAlias, field) {
|
|
1264
1237
|
const fieldDef = requireField(this.schema, model, field);
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
return query.select(this.eb.ref(`${modelAlias}.${field}`).as(field));
|
|
1269
|
-
} else {
|
|
1270
|
-
return this.buildSelectField(query, fieldDef.originModel, fieldDef.originModel, field);
|
|
1271
|
-
}
|
|
1238
|
+
const fieldModel = fieldDef.originModel ?? model;
|
|
1239
|
+
const alias = fieldDef.originModel ?? modelAlias;
|
|
1240
|
+
return query.select(() => this.fieldRef(fieldModel, field, alias).as(field));
|
|
1272
1241
|
}
|
|
1273
1242
|
buildDelegateJoin(thisModel, thisModelAlias, otherModelAlias, query) {
|
|
1274
1243
|
const idFields = requireIdFields(this.schema, thisModel);
|
|
@@ -1357,7 +1326,25 @@ var BaseCrudDialect = class {
|
|
|
1357
1326
|
return this.eb.not(this.and(...args));
|
|
1358
1327
|
}
|
|
1359
1328
|
fieldRef(model, field, modelAlias, inlineComputedField = true) {
|
|
1360
|
-
|
|
1329
|
+
const fieldDef = requireField(this.schema, model, field);
|
|
1330
|
+
if (!fieldDef.computed) {
|
|
1331
|
+
return this.eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
1332
|
+
} else {
|
|
1333
|
+
if (!inlineComputedField) {
|
|
1334
|
+
return this.eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
1335
|
+
}
|
|
1336
|
+
let computer;
|
|
1337
|
+
if ("computedFields" in this.options) {
|
|
1338
|
+
const computedFields = this.options.computedFields;
|
|
1339
|
+
computer = computedFields?.[fieldDef.originModel ?? model]?.[field];
|
|
1340
|
+
}
|
|
1341
|
+
if (!computer) {
|
|
1342
|
+
throw new QueryError(`Computed field "${field}" implementation not provided for model "${model}"`);
|
|
1343
|
+
}
|
|
1344
|
+
return computer(this.eb, {
|
|
1345
|
+
modelAlias
|
|
1346
|
+
});
|
|
1347
|
+
}
|
|
1361
1348
|
}
|
|
1362
1349
|
canJoinWithoutNestedSelect(modelDef, payload) {
|
|
1363
1350
|
if (modelDef.computedFields) {
|
|
@@ -3201,7 +3188,7 @@ var AggregateOperationHandler = class extends BaseOperationHandler {
|
|
|
3201
3188
|
take = -take;
|
|
3202
3189
|
}
|
|
3203
3190
|
subQuery = this.dialect.buildSkipTake(subQuery, skip, take);
|
|
3204
|
-
subQuery = this.dialect.buildOrderBy(subQuery, this.model, this.model, parsedArgs.orderBy,
|
|
3191
|
+
subQuery = this.dialect.buildOrderBy(subQuery, this.model, this.model, parsedArgs.orderBy, negateOrderBy);
|
|
3205
3192
|
return subQuery.as("$sub");
|
|
3206
3193
|
});
|
|
3207
3194
|
for (const [key, value] of Object.entries(parsedArgs)) {
|
|
@@ -3432,29 +3419,23 @@ var GroupByOperationHandler = class extends BaseOperationHandler {
|
|
|
3432
3419
|
async handle(_operation, args) {
|
|
3433
3420
|
const normalizedArgs = this.normalizeArgs(args);
|
|
3434
3421
|
const parsedArgs = this.inputValidator.validateGroupByArgs(this.model, normalizedArgs);
|
|
3435
|
-
let query = this.kysely.selectFrom((
|
|
3436
|
-
|
|
3437
|
-
const skip = parsedArgs?.skip;
|
|
3438
|
-
let take = parsedArgs?.take;
|
|
3439
|
-
let negateOrderBy = false;
|
|
3440
|
-
if (take !== void 0 && take < 0) {
|
|
3441
|
-
negateOrderBy = true;
|
|
3442
|
-
take = -take;
|
|
3443
|
-
}
|
|
3444
|
-
subQuery = this.dialect.buildSkipTake(subQuery, skip, take);
|
|
3445
|
-
subQuery = this.dialect.buildOrderBy(subQuery, this.model, this.model, void 0, skip !== void 0 || take !== void 0, negateOrderBy);
|
|
3446
|
-
return subQuery.as("$sub");
|
|
3447
|
-
});
|
|
3448
|
-
const fieldRef = /* @__PURE__ */ __name((field) => this.dialect.fieldRef(this.model, field, "$sub"), "fieldRef");
|
|
3422
|
+
let query = this.kysely.selectFrom(this.model).where(() => this.dialect.buildFilter(this.model, this.model, parsedArgs?.where));
|
|
3423
|
+
const fieldRef = /* @__PURE__ */ __name((field) => this.dialect.fieldRef(this.model, field), "fieldRef");
|
|
3449
3424
|
const bys = typeof parsedArgs.by === "string" ? [
|
|
3450
3425
|
parsedArgs.by
|
|
3451
3426
|
] : parsedArgs.by;
|
|
3452
3427
|
query = query.groupBy(bys.map((by) => fieldRef(by)));
|
|
3453
|
-
|
|
3454
|
-
|
|
3428
|
+
const skip = parsedArgs?.skip;
|
|
3429
|
+
let take = parsedArgs?.take;
|
|
3430
|
+
let negateOrderBy = false;
|
|
3431
|
+
if (take !== void 0 && take < 0) {
|
|
3432
|
+
negateOrderBy = true;
|
|
3433
|
+
take = -take;
|
|
3455
3434
|
}
|
|
3435
|
+
query = this.dialect.buildSkipTake(query, skip, take);
|
|
3436
|
+
query = this.dialect.buildOrderBy(query, this.model, this.model, parsedArgs.orderBy, negateOrderBy);
|
|
3456
3437
|
if (parsedArgs.having) {
|
|
3457
|
-
query = query.having(() => this.dialect.buildFilter(this.model,
|
|
3438
|
+
query = query.having(() => this.dialect.buildFilter(this.model, this.model, parsedArgs.having));
|
|
3458
3439
|
}
|
|
3459
3440
|
for (const by of bys) {
|
|
3460
3441
|
query = query.select(() => fieldRef(by).as(by));
|
|
@@ -3978,12 +3959,12 @@ function evalCall(data, expr) {
|
|
|
3978
3959
|
__name(evalCall, "evalCall");
|
|
3979
3960
|
|
|
3980
3961
|
// src/client/crud/validator/index.ts
|
|
3962
|
+
var schemaCache = /* @__PURE__ */ new WeakMap();
|
|
3981
3963
|
var InputValidator = class {
|
|
3982
3964
|
static {
|
|
3983
3965
|
__name(this, "InputValidator");
|
|
3984
3966
|
}
|
|
3985
3967
|
client;
|
|
3986
|
-
schemaCache = /* @__PURE__ */ new Map();
|
|
3987
3968
|
constructor(client) {
|
|
3988
3969
|
this.client = client;
|
|
3989
3970
|
}
|
|
@@ -4032,16 +4013,34 @@ var InputValidator = class {
|
|
|
4032
4013
|
validateGroupByArgs(model, args) {
|
|
4033
4014
|
return this.validate(model, "groupBy", void 0, (model2) => this.makeGroupBySchema(model2), args);
|
|
4034
4015
|
}
|
|
4016
|
+
getSchemaCache(cacheKey) {
|
|
4017
|
+
let thisCache = schemaCache.get(this.schema);
|
|
4018
|
+
if (!thisCache) {
|
|
4019
|
+
thisCache = /* @__PURE__ */ new Map();
|
|
4020
|
+
schemaCache.set(this.schema, thisCache);
|
|
4021
|
+
}
|
|
4022
|
+
return thisCache.get(cacheKey);
|
|
4023
|
+
}
|
|
4024
|
+
setSchemaCache(cacheKey, schema) {
|
|
4025
|
+
let thisCache = schemaCache.get(this.schema);
|
|
4026
|
+
if (!thisCache) {
|
|
4027
|
+
thisCache = /* @__PURE__ */ new Map();
|
|
4028
|
+
schemaCache.set(this.schema, thisCache);
|
|
4029
|
+
}
|
|
4030
|
+
return thisCache.set(cacheKey, schema);
|
|
4031
|
+
}
|
|
4035
4032
|
validate(model, operation, options, getSchema, args) {
|
|
4036
4033
|
const cacheKey = (0, import_json_stable_stringify.default)({
|
|
4034
|
+
type: "model",
|
|
4037
4035
|
model,
|
|
4038
4036
|
operation,
|
|
4039
|
-
options
|
|
4037
|
+
options,
|
|
4038
|
+
extraValidationsEnabled: this.extraValidationsEnabled
|
|
4040
4039
|
});
|
|
4041
|
-
let schema = this.
|
|
4040
|
+
let schema = this.getSchemaCache(cacheKey);
|
|
4042
4041
|
if (!schema) {
|
|
4043
4042
|
schema = getSchema(model, options);
|
|
4044
|
-
this.
|
|
4043
|
+
this.setSchemaCache(cacheKey, schema);
|
|
4045
4044
|
}
|
|
4046
4045
|
const { error, data } = schema.safeParse(args);
|
|
4047
4046
|
if (error) {
|
|
@@ -4100,8 +4099,12 @@ var InputValidator = class {
|
|
|
4100
4099
|
}
|
|
4101
4100
|
}
|
|
4102
4101
|
makeTypeDefSchema(type) {
|
|
4103
|
-
const key =
|
|
4104
|
-
|
|
4102
|
+
const key = (0, import_json_stable_stringify.default)({
|
|
4103
|
+
type: "typedef",
|
|
4104
|
+
name: type,
|
|
4105
|
+
extraValidationsEnabled: this.extraValidationsEnabled
|
|
4106
|
+
});
|
|
4107
|
+
let schema = this.getSchemaCache(key);
|
|
4105
4108
|
if (schema) {
|
|
4106
4109
|
return schema;
|
|
4107
4110
|
}
|
|
@@ -4120,7 +4123,7 @@ var InputValidator = class {
|
|
|
4120
4123
|
fieldSchema
|
|
4121
4124
|
];
|
|
4122
4125
|
}))).passthrough();
|
|
4123
|
-
this.
|
|
4126
|
+
this.setSchemaCache(key, schema);
|
|
4124
4127
|
return schema;
|
|
4125
4128
|
}
|
|
4126
4129
|
makeWhereSchema(model, unique, withoutRelationFields = false, withAggregations = false) {
|
|
@@ -5569,7 +5572,18 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5569
5572
|
nameMapper;
|
|
5570
5573
|
constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false) {
|
|
5571
5574
|
super(compiler, adapter, connectionProvider, plugins), this.client = client, this.driver = driver, this.compiler = compiler, this.connectionProvider = connectionProvider, this.suppressMutationHooks = suppressMutationHooks;
|
|
5572
|
-
this.
|
|
5575
|
+
if (this.schemaHasMappedNames(client.$schema)) {
|
|
5576
|
+
this.nameMapper = new QueryNameMapper(client.$schema);
|
|
5577
|
+
}
|
|
5578
|
+
}
|
|
5579
|
+
schemaHasMappedNames(schema) {
|
|
5580
|
+
const hasMapAttr = /* @__PURE__ */ __name((decl) => {
|
|
5581
|
+
if (decl.attributes?.some((attr) => attr.name === "@@map")) {
|
|
5582
|
+
return true;
|
|
5583
|
+
}
|
|
5584
|
+
return Object.values(decl.fields).some((field) => field.attributes?.some((attr) => attr.name === "@map"));
|
|
5585
|
+
}, "hasMapAttr");
|
|
5586
|
+
return Object.values(schema.models).some(hasMapAttr) || Object.values(schema.typeDefs ?? []).some(hasMapAttr);
|
|
5573
5587
|
}
|
|
5574
5588
|
get kysely() {
|
|
5575
5589
|
return this.client.$qb;
|
|
@@ -5651,7 +5665,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5651
5665
|
async proceedQuery(connection, query, parameters, queryId) {
|
|
5652
5666
|
let compiled;
|
|
5653
5667
|
if (this.suppressMutationHooks || !this.isMutationNode(query) || !this.hasEntityMutationPlugins) {
|
|
5654
|
-
const finalQuery2 = this.
|
|
5668
|
+
const finalQuery2 = this.processNameMapping(query);
|
|
5655
5669
|
compiled = this.compileQuery(finalQuery2);
|
|
5656
5670
|
if (parameters) {
|
|
5657
5671
|
compiled = {
|
|
@@ -5669,7 +5683,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5669
5683
|
])
|
|
5670
5684
|
};
|
|
5671
5685
|
}
|
|
5672
|
-
const finalQuery = this.
|
|
5686
|
+
const finalQuery = this.processNameMapping(query);
|
|
5673
5687
|
compiled = this.compileQuery(finalQuery);
|
|
5674
5688
|
if (parameters) {
|
|
5675
5689
|
compiled = {
|
|
@@ -5697,6 +5711,9 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5697
5711
|
}
|
|
5698
5712
|
return result;
|
|
5699
5713
|
}
|
|
5714
|
+
processNameMapping(query) {
|
|
5715
|
+
return this.nameMapper?.transformNode(query) ?? query;
|
|
5716
|
+
}
|
|
5700
5717
|
createClientForConnection(connection, inTx) {
|
|
5701
5718
|
const innerExecutor = this.withConnectionProvider(new import_kysely7.SingleConnectionProvider(connection));
|
|
5702
5719
|
innerExecutor.suppressMutationHooks = true;
|
|
@@ -6481,6 +6498,11 @@ var ClientImpl = class _ClientImpl {
|
|
|
6481
6498
|
...args
|
|
6482
6499
|
]);
|
|
6483
6500
|
}
|
|
6501
|
+
async $connect() {
|
|
6502
|
+
await this.kysely.connection().execute(async (conn) => {
|
|
6503
|
+
await conn.executeQuery(import_kysely10.sql`select 1`.compile(this.kysely));
|
|
6504
|
+
});
|
|
6505
|
+
}
|
|
6484
6506
|
async $disconnect() {
|
|
6485
6507
|
await this.kysely.destroy();
|
|
6486
6508
|
}
|