@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.d.cts
CHANGED
|
@@ -9,7 +9,7 @@ import { ExpressionBuilder, SelectQueryBuilder, Expression, SqlBool, ExpressionW
|
|
|
9
9
|
/**
|
|
10
10
|
* Client API methods that are not supported in transactions.
|
|
11
11
|
*/
|
|
12
|
-
declare const TRANSACTION_UNSUPPORTED_METHODS: readonly ["$transaction", "$disconnect", "$use"];
|
|
12
|
+
declare const TRANSACTION_UNSUPPORTED_METHODS: readonly ["$transaction", "$connect", "$disconnect", "$use"];
|
|
13
13
|
/**
|
|
14
14
|
* Logical combinators used in filters.
|
|
15
15
|
*/
|
|
@@ -47,7 +47,7 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
|
47
47
|
private buildDateTimeFilter;
|
|
48
48
|
private buildBytesFilter;
|
|
49
49
|
private buildEnumFilter;
|
|
50
|
-
buildOrderBy(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, orderBy: OrArray<OrderBy<Schema, GetModels<Schema>, boolean, boolean>> | undefined,
|
|
50
|
+
buildOrderBy(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, orderBy: OrArray<OrderBy<Schema, GetModels<Schema>, boolean, boolean>> | undefined, negated: boolean): SelectQueryBuilder<any, any, any>;
|
|
51
51
|
buildSelectAllFields(model: string, query: SelectQueryBuilder<any, any, any>, omit: Record<string, boolean | undefined> | undefined, modelAlias: string): SelectQueryBuilder<any, any, any>;
|
|
52
52
|
protected buildModelSelect(model: GetModels<Schema>, subQueryAlias: string, payload: true | FindArgs<Schema, GetModels<Schema>, true>, selectAllFields: boolean): SelectQueryBuilder<any, any, {}>;
|
|
53
53
|
buildSelectField(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, field: string): SelectQueryBuilder<any, any, any>;
|
|
@@ -61,7 +61,7 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
|
61
61
|
and(...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
62
62
|
or(...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
63
63
|
not(...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
|
|
64
|
-
fieldRef(model: string, field: string, modelAlias?: string, inlineComputedField?: boolean):
|
|
64
|
+
fieldRef(model: string, field: string, modelAlias?: string, inlineComputedField?: boolean): any;
|
|
65
65
|
protected canJoinWithoutNestedSelect(modelDef: ModelDef, payload: boolean | FindArgs<Schema, GetModels<Schema>, true>): boolean;
|
|
66
66
|
abstract get provider(): DataSourceProviderType;
|
|
67
67
|
/**
|
|
@@ -452,7 +452,11 @@ type ClientContract<Schema extends SchemaDef> = {
|
|
|
452
452
|
*/
|
|
453
453
|
$unuseAll(): ClientContract<Schema>;
|
|
454
454
|
/**
|
|
455
|
-
*
|
|
455
|
+
* Eagerly connects to the database.
|
|
456
|
+
*/
|
|
457
|
+
$connect(): Promise<void>;
|
|
458
|
+
/**
|
|
459
|
+
* Explicitly disconnects from the database.
|
|
456
460
|
*/
|
|
457
461
|
$disconnect(): Promise<void>;
|
|
458
462
|
/**
|
|
@@ -1106,7 +1110,6 @@ declare function getUniqueFields(schema: SchemaDef, model: string): ({
|
|
|
1106
1110
|
defs: Record<string, FieldDef>;
|
|
1107
1111
|
})[];
|
|
1108
1112
|
declare function getIdValues(schema: SchemaDef, model: string, data: any): Record<string, any>;
|
|
1109
|
-
declare function buildFieldRef<Schema extends SchemaDef>(schema: Schema, model: string, field: string, options: ClientOptions<Schema>, eb: ExpressionBuilder<any, any>, modelAlias?: string, inlineComputedField?: boolean): ExpressionWrapper<any, any, unknown>;
|
|
1110
1113
|
declare function fieldHasDefaultValue(fieldDef: FieldDef): boolean | undefined;
|
|
1111
1114
|
declare function isEnum(schema: SchemaDef, type: string): boolean;
|
|
1112
1115
|
declare function getEnum(schema: SchemaDef, type: string): _zenstackhq_sdk_schema.EnumDef | undefined;
|
|
@@ -1153,7 +1156,6 @@ declare function extractModelName(node: OperationNode): string | undefined;
|
|
|
1153
1156
|
declare function extractFieldName(node: OperationNode): string | undefined;
|
|
1154
1157
|
|
|
1155
1158
|
declare const queryUtils_aggregate: typeof aggregate;
|
|
1156
|
-
declare const queryUtils_buildFieldRef: typeof buildFieldRef;
|
|
1157
1159
|
declare const queryUtils_buildJoinPairs: typeof buildJoinPairs;
|
|
1158
1160
|
declare const queryUtils_ensureArray: typeof ensureArray;
|
|
1159
1161
|
declare const queryUtils_extractFieldName: typeof extractFieldName;
|
|
@@ -1185,7 +1187,7 @@ declare const queryUtils_requireModel: typeof requireModel;
|
|
|
1185
1187
|
declare const queryUtils_safeJSONStringify: typeof safeJSONStringify;
|
|
1186
1188
|
declare const queryUtils_stripAlias: typeof stripAlias;
|
|
1187
1189
|
declare namespace queryUtils {
|
|
1188
|
-
export { queryUtils_aggregate as aggregate,
|
|
1190
|
+
export { queryUtils_aggregate as aggregate, queryUtils_buildJoinPairs as buildJoinPairs, queryUtils_ensureArray as ensureArray, queryUtils_extractFieldName as extractFieldName, queryUtils_extractIdFields as extractIdFields, queryUtils_extractModelName as extractModelName, queryUtils_fieldHasDefaultValue as fieldHasDefaultValue, queryUtils_flattenCompoundUniqueFilters as flattenCompoundUniqueFilters, queryUtils_getDelegateDescendantModels as getDelegateDescendantModels, queryUtils_getDiscriminatorField as getDiscriminatorField, queryUtils_getEnum as getEnum, queryUtils_getField as getField, queryUtils_getIdFields as getIdFields, queryUtils_getIdValues as getIdValues, queryUtils_getManyToManyRelation as getManyToManyRelation, queryUtils_getModel as getModel, queryUtils_getRelationForeignKeyFieldPairs as getRelationForeignKeyFieldPairs, queryUtils_getTypeDef as getTypeDef, queryUtils_getUniqueFields as getUniqueFields, queryUtils_hasModel as hasModel, queryUtils_isEnum as isEnum, queryUtils_isForeignKeyField as isForeignKeyField, queryUtils_isInheritedField as isInheritedField, queryUtils_isRelationField as isRelationField, queryUtils_isScalarField as isScalarField, queryUtils_makeDefaultOrderBy as makeDefaultOrderBy, queryUtils_requireField as requireField, queryUtils_requireIdFields as requireIdFields, queryUtils_requireModel as requireModel, queryUtils_safeJSONStringify as safeJSONStringify, queryUtils_stripAlias as stripAlias };
|
|
1189
1191
|
}
|
|
1190
1192
|
|
|
1191
1193
|
declare class DefaultOperationNodeVisitor extends OperationNodeVisitor {
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { ExpressionBuilder, SelectQueryBuilder, Expression, SqlBool, ExpressionW
|
|
|
9
9
|
/**
|
|
10
10
|
* Client API methods that are not supported in transactions.
|
|
11
11
|
*/
|
|
12
|
-
declare const TRANSACTION_UNSUPPORTED_METHODS: readonly ["$transaction", "$disconnect", "$use"];
|
|
12
|
+
declare const TRANSACTION_UNSUPPORTED_METHODS: readonly ["$transaction", "$connect", "$disconnect", "$use"];
|
|
13
13
|
/**
|
|
14
14
|
* Logical combinators used in filters.
|
|
15
15
|
*/
|
|
@@ -47,7 +47,7 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
|
47
47
|
private buildDateTimeFilter;
|
|
48
48
|
private buildBytesFilter;
|
|
49
49
|
private buildEnumFilter;
|
|
50
|
-
buildOrderBy(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, orderBy: OrArray<OrderBy<Schema, GetModels<Schema>, boolean, boolean>> | undefined,
|
|
50
|
+
buildOrderBy(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, orderBy: OrArray<OrderBy<Schema, GetModels<Schema>, boolean, boolean>> | undefined, negated: boolean): SelectQueryBuilder<any, any, any>;
|
|
51
51
|
buildSelectAllFields(model: string, query: SelectQueryBuilder<any, any, any>, omit: Record<string, boolean | undefined> | undefined, modelAlias: string): SelectQueryBuilder<any, any, any>;
|
|
52
52
|
protected buildModelSelect(model: GetModels<Schema>, subQueryAlias: string, payload: true | FindArgs<Schema, GetModels<Schema>, true>, selectAllFields: boolean): SelectQueryBuilder<any, any, {}>;
|
|
53
53
|
buildSelectField(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, field: string): SelectQueryBuilder<any, any, any>;
|
|
@@ -61,7 +61,7 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
|
61
61
|
and(...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
62
62
|
or(...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
63
63
|
not(...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
|
|
64
|
-
fieldRef(model: string, field: string, modelAlias?: string, inlineComputedField?: boolean):
|
|
64
|
+
fieldRef(model: string, field: string, modelAlias?: string, inlineComputedField?: boolean): any;
|
|
65
65
|
protected canJoinWithoutNestedSelect(modelDef: ModelDef, payload: boolean | FindArgs<Schema, GetModels<Schema>, true>): boolean;
|
|
66
66
|
abstract get provider(): DataSourceProviderType;
|
|
67
67
|
/**
|
|
@@ -452,7 +452,11 @@ type ClientContract<Schema extends SchemaDef> = {
|
|
|
452
452
|
*/
|
|
453
453
|
$unuseAll(): ClientContract<Schema>;
|
|
454
454
|
/**
|
|
455
|
-
*
|
|
455
|
+
* Eagerly connects to the database.
|
|
456
|
+
*/
|
|
457
|
+
$connect(): Promise<void>;
|
|
458
|
+
/**
|
|
459
|
+
* Explicitly disconnects from the database.
|
|
456
460
|
*/
|
|
457
461
|
$disconnect(): Promise<void>;
|
|
458
462
|
/**
|
|
@@ -1106,7 +1110,6 @@ declare function getUniqueFields(schema: SchemaDef, model: string): ({
|
|
|
1106
1110
|
defs: Record<string, FieldDef>;
|
|
1107
1111
|
})[];
|
|
1108
1112
|
declare function getIdValues(schema: SchemaDef, model: string, data: any): Record<string, any>;
|
|
1109
|
-
declare function buildFieldRef<Schema extends SchemaDef>(schema: Schema, model: string, field: string, options: ClientOptions<Schema>, eb: ExpressionBuilder<any, any>, modelAlias?: string, inlineComputedField?: boolean): ExpressionWrapper<any, any, unknown>;
|
|
1110
1113
|
declare function fieldHasDefaultValue(fieldDef: FieldDef): boolean | undefined;
|
|
1111
1114
|
declare function isEnum(schema: SchemaDef, type: string): boolean;
|
|
1112
1115
|
declare function getEnum(schema: SchemaDef, type: string): _zenstackhq_sdk_schema.EnumDef | undefined;
|
|
@@ -1153,7 +1156,6 @@ declare function extractModelName(node: OperationNode): string | undefined;
|
|
|
1153
1156
|
declare function extractFieldName(node: OperationNode): string | undefined;
|
|
1154
1157
|
|
|
1155
1158
|
declare const queryUtils_aggregate: typeof aggregate;
|
|
1156
|
-
declare const queryUtils_buildFieldRef: typeof buildFieldRef;
|
|
1157
1159
|
declare const queryUtils_buildJoinPairs: typeof buildJoinPairs;
|
|
1158
1160
|
declare const queryUtils_ensureArray: typeof ensureArray;
|
|
1159
1161
|
declare const queryUtils_extractFieldName: typeof extractFieldName;
|
|
@@ -1185,7 +1187,7 @@ declare const queryUtils_requireModel: typeof requireModel;
|
|
|
1185
1187
|
declare const queryUtils_safeJSONStringify: typeof safeJSONStringify;
|
|
1186
1188
|
declare const queryUtils_stripAlias: typeof stripAlias;
|
|
1187
1189
|
declare namespace queryUtils {
|
|
1188
|
-
export { queryUtils_aggregate as aggregate,
|
|
1190
|
+
export { queryUtils_aggregate as aggregate, queryUtils_buildJoinPairs as buildJoinPairs, queryUtils_ensureArray as ensureArray, queryUtils_extractFieldName as extractFieldName, queryUtils_extractIdFields as extractIdFields, queryUtils_extractModelName as extractModelName, queryUtils_fieldHasDefaultValue as fieldHasDefaultValue, queryUtils_flattenCompoundUniqueFilters as flattenCompoundUniqueFilters, queryUtils_getDelegateDescendantModels as getDelegateDescendantModels, queryUtils_getDiscriminatorField as getDiscriminatorField, queryUtils_getEnum as getEnum, queryUtils_getField as getField, queryUtils_getIdFields as getIdFields, queryUtils_getIdValues as getIdValues, queryUtils_getManyToManyRelation as getManyToManyRelation, queryUtils_getModel as getModel, queryUtils_getRelationForeignKeyFieldPairs as getRelationForeignKeyFieldPairs, queryUtils_getTypeDef as getTypeDef, queryUtils_getUniqueFields as getUniqueFields, queryUtils_hasModel as hasModel, queryUtils_isEnum as isEnum, queryUtils_isForeignKeyField as isForeignKeyField, queryUtils_isInheritedField as isInheritedField, queryUtils_isRelationField as isRelationField, queryUtils_isScalarField as isScalarField, queryUtils_makeDefaultOrderBy as makeDefaultOrderBy, queryUtils_requireField as requireField, queryUtils_requireIdFields as requireIdFields, queryUtils_requireModel as requireModel, queryUtils_safeJSONStringify as safeJSONStringify, queryUtils_stripAlias as stripAlias };
|
|
1189
1191
|
}
|
|
1190
1192
|
|
|
1191
1193
|
declare class DefaultOperationNodeVisitor extends OperationNodeVisitor {
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,6 @@ import { match as match7 } from "ts-pattern";
|
|
|
16
16
|
var query_utils_exports = {};
|
|
17
17
|
__export(query_utils_exports, {
|
|
18
18
|
aggregate: () => aggregate,
|
|
19
|
-
buildFieldRef: () => buildFieldRef,
|
|
20
19
|
buildJoinPairs: () => buildJoinPairs,
|
|
21
20
|
ensureArray: () => ensureArray,
|
|
22
21
|
extractFieldName: () => extractFieldName,
|
|
@@ -362,28 +361,6 @@ function getIdValues(schema, model, data) {
|
|
|
362
361
|
}), {});
|
|
363
362
|
}
|
|
364
363
|
__name(getIdValues, "getIdValues");
|
|
365
|
-
function buildFieldRef(schema, model, field, options, eb, modelAlias, inlineComputedField = true) {
|
|
366
|
-
const fieldDef = requireField(schema, model, field);
|
|
367
|
-
if (!fieldDef.computed) {
|
|
368
|
-
return eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
369
|
-
} else {
|
|
370
|
-
if (!inlineComputedField) {
|
|
371
|
-
return eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
372
|
-
}
|
|
373
|
-
let computer;
|
|
374
|
-
if ("computedFields" in options) {
|
|
375
|
-
const computedFields = options.computedFields;
|
|
376
|
-
computer = computedFields?.[model]?.[field];
|
|
377
|
-
}
|
|
378
|
-
if (!computer) {
|
|
379
|
-
throw new QueryError(`Computed field "${field}" implementation not provided for model "${model}"`);
|
|
380
|
-
}
|
|
381
|
-
return computer(eb, {
|
|
382
|
-
modelAlias
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
__name(buildFieldRef, "buildFieldRef");
|
|
387
364
|
function fieldHasDefaultValue(fieldDef) {
|
|
388
365
|
return fieldDef.default !== void 0 || fieldDef.updatedAt;
|
|
389
366
|
}
|
|
@@ -735,7 +712,7 @@ var BaseCrudDialect = class {
|
|
|
735
712
|
take = -take;
|
|
736
713
|
}
|
|
737
714
|
result = this.buildSkipTake(result, skip, take);
|
|
738
|
-
result = this.buildOrderBy(result, model, modelAlias, args.orderBy,
|
|
715
|
+
result = this.buildOrderBy(result, model, modelAlias, args.orderBy, negateOrderBy);
|
|
739
716
|
if ("distinct" in args && args.distinct) {
|
|
740
717
|
const distinct = ensureArray(args.distinct);
|
|
741
718
|
if (this.supportsDistinctOn) {
|
|
@@ -1094,16 +1071,12 @@ var BaseCrudDialect = class {
|
|
|
1094
1071
|
]);
|
|
1095
1072
|
return this.and(...conditions.conditions);
|
|
1096
1073
|
}
|
|
1097
|
-
buildOrderBy(query, model, modelAlias, orderBy,
|
|
1074
|
+
buildOrderBy(query, model, modelAlias, orderBy, negated) {
|
|
1098
1075
|
if (!orderBy) {
|
|
1099
|
-
|
|
1100
|
-
orderBy = makeDefaultOrderBy(this.schema, model);
|
|
1101
|
-
} else {
|
|
1102
|
-
return query;
|
|
1103
|
-
}
|
|
1076
|
+
return query;
|
|
1104
1077
|
}
|
|
1105
1078
|
let result = query;
|
|
1106
|
-
const
|
|
1079
|
+
const buildFieldRef = /* @__PURE__ */ __name((model2, field, modelAlias2) => {
|
|
1107
1080
|
const fieldDef = requireField(this.schema, model2, field);
|
|
1108
1081
|
return fieldDef.originModel ? this.fieldRef(fieldDef.originModel, field, fieldDef.originModel) : this.fieldRef(model2, field, modelAlias2);
|
|
1109
1082
|
}, "buildFieldRef");
|
|
@@ -1122,7 +1095,7 @@ var BaseCrudDialect = class {
|
|
|
1122
1095
|
invariant2(value && typeof value === "object", `invalid orderBy value for field "${field}"`);
|
|
1123
1096
|
for (const [k, v] of Object.entries(value)) {
|
|
1124
1097
|
invariant2(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
|
|
1125
|
-
result = result.orderBy((eb) => aggregate(eb,
|
|
1098
|
+
result = result.orderBy((eb) => aggregate(eb, buildFieldRef(model, k, modelAlias), field), sql.raw(this.negateSort(v, negated)));
|
|
1126
1099
|
}
|
|
1127
1100
|
continue;
|
|
1128
1101
|
}
|
|
@@ -1131,7 +1104,7 @@ var BaseCrudDialect = class {
|
|
|
1131
1104
|
invariant2(value && typeof value === "object", 'invalid orderBy value for field "_count"');
|
|
1132
1105
|
for (const [k, v] of Object.entries(value)) {
|
|
1133
1106
|
invariant2(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
|
|
1134
|
-
result = result.orderBy((eb) => eb.fn.count(
|
|
1107
|
+
result = result.orderBy((eb) => eb.fn.count(buildFieldRef(model, k, modelAlias)), sql.raw(this.negateSort(v, negated)));
|
|
1135
1108
|
}
|
|
1136
1109
|
continue;
|
|
1137
1110
|
}
|
|
@@ -1140,7 +1113,7 @@ var BaseCrudDialect = class {
|
|
|
1140
1113
|
}
|
|
1141
1114
|
const fieldDef = requireField(this.schema, model, field);
|
|
1142
1115
|
if (!fieldDef.relation) {
|
|
1143
|
-
const fieldRef =
|
|
1116
|
+
const fieldRef = buildFieldRef(model, field, modelAlias);
|
|
1144
1117
|
if (value === "asc" || value === "desc") {
|
|
1145
1118
|
result = result.orderBy(fieldRef, this.negateSort(value, negated));
|
|
1146
1119
|
} else if (value && typeof value === "object" && "nulls" in value && "sort" in value && (value.sort === "asc" || value.sort === "desc") && (value.nulls === "first" || value.nulls === "last")) {
|
|
@@ -1169,7 +1142,7 @@ var BaseCrudDialect = class {
|
|
|
1169
1142
|
const joinPairs = buildJoinPairs(this.schema, model, modelAlias, field, relationModel);
|
|
1170
1143
|
return join.on((eb) => this.and(...joinPairs.map(([left, right]) => eb(this.eb.ref(left), "=", this.eb.ref(right)))));
|
|
1171
1144
|
});
|
|
1172
|
-
result = this.buildOrderBy(result, fieldDef.type, relationModel, value,
|
|
1145
|
+
result = this.buildOrderBy(result, fieldDef.type, relationModel, value, negated);
|
|
1173
1146
|
}
|
|
1174
1147
|
}
|
|
1175
1148
|
}
|
|
@@ -1216,13 +1189,9 @@ var BaseCrudDialect = class {
|
|
|
1216
1189
|
}
|
|
1217
1190
|
buildSelectField(query, model, modelAlias, field) {
|
|
1218
1191
|
const fieldDef = requireField(this.schema, model, field);
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
return query.select(this.eb.ref(`${modelAlias}.${field}`).as(field));
|
|
1223
|
-
} else {
|
|
1224
|
-
return this.buildSelectField(query, fieldDef.originModel, fieldDef.originModel, field);
|
|
1225
|
-
}
|
|
1192
|
+
const fieldModel = fieldDef.originModel ?? model;
|
|
1193
|
+
const alias = fieldDef.originModel ?? modelAlias;
|
|
1194
|
+
return query.select(() => this.fieldRef(fieldModel, field, alias).as(field));
|
|
1226
1195
|
}
|
|
1227
1196
|
buildDelegateJoin(thisModel, thisModelAlias, otherModelAlias, query) {
|
|
1228
1197
|
const idFields = requireIdFields(this.schema, thisModel);
|
|
@@ -1311,7 +1280,25 @@ var BaseCrudDialect = class {
|
|
|
1311
1280
|
return this.eb.not(this.and(...args));
|
|
1312
1281
|
}
|
|
1313
1282
|
fieldRef(model, field, modelAlias, inlineComputedField = true) {
|
|
1314
|
-
|
|
1283
|
+
const fieldDef = requireField(this.schema, model, field);
|
|
1284
|
+
if (!fieldDef.computed) {
|
|
1285
|
+
return this.eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
1286
|
+
} else {
|
|
1287
|
+
if (!inlineComputedField) {
|
|
1288
|
+
return this.eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
|
|
1289
|
+
}
|
|
1290
|
+
let computer;
|
|
1291
|
+
if ("computedFields" in this.options) {
|
|
1292
|
+
const computedFields = this.options.computedFields;
|
|
1293
|
+
computer = computedFields?.[fieldDef.originModel ?? model]?.[field];
|
|
1294
|
+
}
|
|
1295
|
+
if (!computer) {
|
|
1296
|
+
throw new QueryError(`Computed field "${field}" implementation not provided for model "${model}"`);
|
|
1297
|
+
}
|
|
1298
|
+
return computer(this.eb, {
|
|
1299
|
+
modelAlias
|
|
1300
|
+
});
|
|
1301
|
+
}
|
|
1315
1302
|
}
|
|
1316
1303
|
canJoinWithoutNestedSelect(modelDef, payload) {
|
|
1317
1304
|
if (modelDef.computedFields) {
|
|
@@ -3155,7 +3142,7 @@ var AggregateOperationHandler = class extends BaseOperationHandler {
|
|
|
3155
3142
|
take = -take;
|
|
3156
3143
|
}
|
|
3157
3144
|
subQuery = this.dialect.buildSkipTake(subQuery, skip, take);
|
|
3158
|
-
subQuery = this.dialect.buildOrderBy(subQuery, this.model, this.model, parsedArgs.orderBy,
|
|
3145
|
+
subQuery = this.dialect.buildOrderBy(subQuery, this.model, this.model, parsedArgs.orderBy, negateOrderBy);
|
|
3159
3146
|
return subQuery.as("$sub");
|
|
3160
3147
|
});
|
|
3161
3148
|
for (const [key, value] of Object.entries(parsedArgs)) {
|
|
@@ -3386,29 +3373,23 @@ var GroupByOperationHandler = class extends BaseOperationHandler {
|
|
|
3386
3373
|
async handle(_operation, args) {
|
|
3387
3374
|
const normalizedArgs = this.normalizeArgs(args);
|
|
3388
3375
|
const parsedArgs = this.inputValidator.validateGroupByArgs(this.model, normalizedArgs);
|
|
3389
|
-
let query = this.kysely.selectFrom((
|
|
3390
|
-
|
|
3391
|
-
const skip = parsedArgs?.skip;
|
|
3392
|
-
let take = parsedArgs?.take;
|
|
3393
|
-
let negateOrderBy = false;
|
|
3394
|
-
if (take !== void 0 && take < 0) {
|
|
3395
|
-
negateOrderBy = true;
|
|
3396
|
-
take = -take;
|
|
3397
|
-
}
|
|
3398
|
-
subQuery = this.dialect.buildSkipTake(subQuery, skip, take);
|
|
3399
|
-
subQuery = this.dialect.buildOrderBy(subQuery, this.model, this.model, void 0, skip !== void 0 || take !== void 0, negateOrderBy);
|
|
3400
|
-
return subQuery.as("$sub");
|
|
3401
|
-
});
|
|
3402
|
-
const fieldRef = /* @__PURE__ */ __name((field) => this.dialect.fieldRef(this.model, field, "$sub"), "fieldRef");
|
|
3376
|
+
let query = this.kysely.selectFrom(this.model).where(() => this.dialect.buildFilter(this.model, this.model, parsedArgs?.where));
|
|
3377
|
+
const fieldRef = /* @__PURE__ */ __name((field) => this.dialect.fieldRef(this.model, field), "fieldRef");
|
|
3403
3378
|
const bys = typeof parsedArgs.by === "string" ? [
|
|
3404
3379
|
parsedArgs.by
|
|
3405
3380
|
] : parsedArgs.by;
|
|
3406
3381
|
query = query.groupBy(bys.map((by) => fieldRef(by)));
|
|
3407
|
-
|
|
3408
|
-
|
|
3382
|
+
const skip = parsedArgs?.skip;
|
|
3383
|
+
let take = parsedArgs?.take;
|
|
3384
|
+
let negateOrderBy = false;
|
|
3385
|
+
if (take !== void 0 && take < 0) {
|
|
3386
|
+
negateOrderBy = true;
|
|
3387
|
+
take = -take;
|
|
3409
3388
|
}
|
|
3389
|
+
query = this.dialect.buildSkipTake(query, skip, take);
|
|
3390
|
+
query = this.dialect.buildOrderBy(query, this.model, this.model, parsedArgs.orderBy, negateOrderBy);
|
|
3410
3391
|
if (parsedArgs.having) {
|
|
3411
|
-
query = query.having(() => this.dialect.buildFilter(this.model,
|
|
3392
|
+
query = query.having(() => this.dialect.buildFilter(this.model, this.model, parsedArgs.having));
|
|
3412
3393
|
}
|
|
3413
3394
|
for (const by of bys) {
|
|
3414
3395
|
query = query.select(() => fieldRef(by).as(by));
|
|
@@ -3932,12 +3913,12 @@ function evalCall(data, expr) {
|
|
|
3932
3913
|
__name(evalCall, "evalCall");
|
|
3933
3914
|
|
|
3934
3915
|
// src/client/crud/validator/index.ts
|
|
3916
|
+
var schemaCache = /* @__PURE__ */ new WeakMap();
|
|
3935
3917
|
var InputValidator = class {
|
|
3936
3918
|
static {
|
|
3937
3919
|
__name(this, "InputValidator");
|
|
3938
3920
|
}
|
|
3939
3921
|
client;
|
|
3940
|
-
schemaCache = /* @__PURE__ */ new Map();
|
|
3941
3922
|
constructor(client) {
|
|
3942
3923
|
this.client = client;
|
|
3943
3924
|
}
|
|
@@ -3986,16 +3967,34 @@ var InputValidator = class {
|
|
|
3986
3967
|
validateGroupByArgs(model, args) {
|
|
3987
3968
|
return this.validate(model, "groupBy", void 0, (model2) => this.makeGroupBySchema(model2), args);
|
|
3988
3969
|
}
|
|
3970
|
+
getSchemaCache(cacheKey) {
|
|
3971
|
+
let thisCache = schemaCache.get(this.schema);
|
|
3972
|
+
if (!thisCache) {
|
|
3973
|
+
thisCache = /* @__PURE__ */ new Map();
|
|
3974
|
+
schemaCache.set(this.schema, thisCache);
|
|
3975
|
+
}
|
|
3976
|
+
return thisCache.get(cacheKey);
|
|
3977
|
+
}
|
|
3978
|
+
setSchemaCache(cacheKey, schema) {
|
|
3979
|
+
let thisCache = schemaCache.get(this.schema);
|
|
3980
|
+
if (!thisCache) {
|
|
3981
|
+
thisCache = /* @__PURE__ */ new Map();
|
|
3982
|
+
schemaCache.set(this.schema, thisCache);
|
|
3983
|
+
}
|
|
3984
|
+
return thisCache.set(cacheKey, schema);
|
|
3985
|
+
}
|
|
3989
3986
|
validate(model, operation, options, getSchema, args) {
|
|
3990
3987
|
const cacheKey = stableStringify({
|
|
3988
|
+
type: "model",
|
|
3991
3989
|
model,
|
|
3992
3990
|
operation,
|
|
3993
|
-
options
|
|
3991
|
+
options,
|
|
3992
|
+
extraValidationsEnabled: this.extraValidationsEnabled
|
|
3994
3993
|
});
|
|
3995
|
-
let schema = this.
|
|
3994
|
+
let schema = this.getSchemaCache(cacheKey);
|
|
3996
3995
|
if (!schema) {
|
|
3997
3996
|
schema = getSchema(model, options);
|
|
3998
|
-
this.
|
|
3997
|
+
this.setSchemaCache(cacheKey, schema);
|
|
3999
3998
|
}
|
|
4000
3999
|
const { error, data } = schema.safeParse(args);
|
|
4001
4000
|
if (error) {
|
|
@@ -4054,8 +4053,12 @@ var InputValidator = class {
|
|
|
4054
4053
|
}
|
|
4055
4054
|
}
|
|
4056
4055
|
makeTypeDefSchema(type) {
|
|
4057
|
-
const key =
|
|
4058
|
-
|
|
4056
|
+
const key = stableStringify({
|
|
4057
|
+
type: "typedef",
|
|
4058
|
+
name: type,
|
|
4059
|
+
extraValidationsEnabled: this.extraValidationsEnabled
|
|
4060
|
+
});
|
|
4061
|
+
let schema = this.getSchemaCache(key);
|
|
4059
4062
|
if (schema) {
|
|
4060
4063
|
return schema;
|
|
4061
4064
|
}
|
|
@@ -4074,7 +4077,7 @@ var InputValidator = class {
|
|
|
4074
4077
|
fieldSchema
|
|
4075
4078
|
];
|
|
4076
4079
|
}))).passthrough();
|
|
4077
|
-
this.
|
|
4080
|
+
this.setSchemaCache(key, schema);
|
|
4078
4081
|
return schema;
|
|
4079
4082
|
}
|
|
4080
4083
|
makeWhereSchema(model, unique, withoutRelationFields = false, withAggregations = false) {
|
|
@@ -5523,7 +5526,18 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends DefaultQueryExe
|
|
|
5523
5526
|
nameMapper;
|
|
5524
5527
|
constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false) {
|
|
5525
5528
|
super(compiler, adapter, connectionProvider, plugins), this.client = client, this.driver = driver, this.compiler = compiler, this.connectionProvider = connectionProvider, this.suppressMutationHooks = suppressMutationHooks;
|
|
5526
|
-
this.
|
|
5529
|
+
if (this.schemaHasMappedNames(client.$schema)) {
|
|
5530
|
+
this.nameMapper = new QueryNameMapper(client.$schema);
|
|
5531
|
+
}
|
|
5532
|
+
}
|
|
5533
|
+
schemaHasMappedNames(schema) {
|
|
5534
|
+
const hasMapAttr = /* @__PURE__ */ __name((decl) => {
|
|
5535
|
+
if (decl.attributes?.some((attr) => attr.name === "@@map")) {
|
|
5536
|
+
return true;
|
|
5537
|
+
}
|
|
5538
|
+
return Object.values(decl.fields).some((field) => field.attributes?.some((attr) => attr.name === "@map"));
|
|
5539
|
+
}, "hasMapAttr");
|
|
5540
|
+
return Object.values(schema.models).some(hasMapAttr) || Object.values(schema.typeDefs ?? []).some(hasMapAttr);
|
|
5527
5541
|
}
|
|
5528
5542
|
get kysely() {
|
|
5529
5543
|
return this.client.$qb;
|
|
@@ -5605,7 +5619,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends DefaultQueryExe
|
|
|
5605
5619
|
async proceedQuery(connection, query, parameters, queryId) {
|
|
5606
5620
|
let compiled;
|
|
5607
5621
|
if (this.suppressMutationHooks || !this.isMutationNode(query) || !this.hasEntityMutationPlugins) {
|
|
5608
|
-
const finalQuery2 = this.
|
|
5622
|
+
const finalQuery2 = this.processNameMapping(query);
|
|
5609
5623
|
compiled = this.compileQuery(finalQuery2);
|
|
5610
5624
|
if (parameters) {
|
|
5611
5625
|
compiled = {
|
|
@@ -5623,7 +5637,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends DefaultQueryExe
|
|
|
5623
5637
|
])
|
|
5624
5638
|
};
|
|
5625
5639
|
}
|
|
5626
|
-
const finalQuery = this.
|
|
5640
|
+
const finalQuery = this.processNameMapping(query);
|
|
5627
5641
|
compiled = this.compileQuery(finalQuery);
|
|
5628
5642
|
if (parameters) {
|
|
5629
5643
|
compiled = {
|
|
@@ -5651,6 +5665,9 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends DefaultQueryExe
|
|
|
5651
5665
|
}
|
|
5652
5666
|
return result;
|
|
5653
5667
|
}
|
|
5668
|
+
processNameMapping(query) {
|
|
5669
|
+
return this.nameMapper?.transformNode(query) ?? query;
|
|
5670
|
+
}
|
|
5654
5671
|
createClientForConnection(connection, inTx) {
|
|
5655
5672
|
const innerExecutor = this.withConnectionProvider(new SingleConnectionProvider(connection));
|
|
5656
5673
|
innerExecutor.suppressMutationHooks = true;
|
|
@@ -6435,6 +6452,11 @@ var ClientImpl = class _ClientImpl {
|
|
|
6435
6452
|
...args
|
|
6436
6453
|
]);
|
|
6437
6454
|
}
|
|
6455
|
+
async $connect() {
|
|
6456
|
+
await this.kysely.connection().execute(async (conn) => {
|
|
6457
|
+
await conn.executeQuery(sql7`select 1`.compile(this.kysely));
|
|
6458
|
+
});
|
|
6459
|
+
}
|
|
6438
6460
|
async $disconnect() {
|
|
6439
6461
|
await this.kysely.destroy();
|
|
6440
6462
|
}
|