@danielhritcu/zenstack-orm 3.5.4 → 3.5.6
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 +348 -90
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -1
- package/dist/index.d.ts +33 -1
- package/dist/index.js +271 -13
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -68,7 +68,7 @@ __export(src_exports, {
|
|
|
68
68
|
module.exports = __toCommonJS(src_exports);
|
|
69
69
|
|
|
70
70
|
// src/client/client-impl.ts
|
|
71
|
-
var
|
|
71
|
+
var import_common_helpers15 = require("@zenstackhq/common-helpers");
|
|
72
72
|
var import_kysely12 = require("kysely");
|
|
73
73
|
var import_zod3 = __toESM(require("zod"), 1);
|
|
74
74
|
|
|
@@ -666,7 +666,7 @@ __name(tmpAlias, "tmpAlias");
|
|
|
666
666
|
|
|
667
667
|
// src/client/crud/operations/base.ts
|
|
668
668
|
var import_cuid2 = require("@paralleldrive/cuid2");
|
|
669
|
-
var
|
|
669
|
+
var import_common_helpers8 = require("@zenstackhq/common-helpers");
|
|
670
670
|
var import_cuid = __toESM(require("cuid"), 1);
|
|
671
671
|
var import_kysely6 = require("kysely");
|
|
672
672
|
var import_nanoid = require("nanoid");
|
|
@@ -2824,6 +2824,224 @@ function getCrudDialect(schema, options) {
|
|
|
2824
2824
|
}
|
|
2825
2825
|
__name(getCrudDialect, "getCrudDialect");
|
|
2826
2826
|
|
|
2827
|
+
// src/client/plugins/search.ts
|
|
2828
|
+
var import_common_helpers7 = require("@zenstackhq/common-helpers");
|
|
2829
|
+
function isSearchPayload(value) {
|
|
2830
|
+
return (0, import_common_helpers7.isPlainObject)(value) && typeof value.q === "string" && (value.fields !== void 0 || value.profile !== void 0);
|
|
2831
|
+
}
|
|
2832
|
+
__name(isSearchPayload, "isSearchPayload");
|
|
2833
|
+
function buildStringFilter(mode, token) {
|
|
2834
|
+
if (mode === "equals") return {
|
|
2835
|
+
equals: token,
|
|
2836
|
+
mode: "insensitive"
|
|
2837
|
+
};
|
|
2838
|
+
if (mode === "startsWith") return {
|
|
2839
|
+
startsWith: token,
|
|
2840
|
+
mode: "insensitive"
|
|
2841
|
+
};
|
|
2842
|
+
return {
|
|
2843
|
+
contains: token,
|
|
2844
|
+
mode: "insensitive"
|
|
2845
|
+
};
|
|
2846
|
+
}
|
|
2847
|
+
__name(buildStringFilter, "buildStringFilter");
|
|
2848
|
+
function unique(arr) {
|
|
2849
|
+
return [
|
|
2850
|
+
...new Set(arr)
|
|
2851
|
+
];
|
|
2852
|
+
}
|
|
2853
|
+
__name(unique, "unique");
|
|
2854
|
+
function extractSearch(where) {
|
|
2855
|
+
if (Array.isArray(where)) {
|
|
2856
|
+
let found2;
|
|
2857
|
+
const cleaned2 = [];
|
|
2858
|
+
for (const item of where) {
|
|
2859
|
+
const result = extractSearch(item);
|
|
2860
|
+
if (result.search) found2 = result.search;
|
|
2861
|
+
cleaned2.push(result.where);
|
|
2862
|
+
}
|
|
2863
|
+
return {
|
|
2864
|
+
where: cleaned2,
|
|
2865
|
+
search: found2
|
|
2866
|
+
};
|
|
2867
|
+
}
|
|
2868
|
+
if (!(0, import_common_helpers7.isPlainObject)(where)) {
|
|
2869
|
+
return {
|
|
2870
|
+
where
|
|
2871
|
+
};
|
|
2872
|
+
}
|
|
2873
|
+
const obj = where;
|
|
2874
|
+
let found;
|
|
2875
|
+
const cleaned = {};
|
|
2876
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
2877
|
+
if (key === "search" && isSearchPayload(value)) {
|
|
2878
|
+
found = value;
|
|
2879
|
+
continue;
|
|
2880
|
+
}
|
|
2881
|
+
if (key === "AND" || key === "OR") {
|
|
2882
|
+
const result = extractSearch(value);
|
|
2883
|
+
if (result.search) found = result.search;
|
|
2884
|
+
cleaned[key] = result.where;
|
|
2885
|
+
} else if (key === "NOT") {
|
|
2886
|
+
const result = extractSearch(value);
|
|
2887
|
+
if (result.search) found = result.search;
|
|
2888
|
+
cleaned[key] = result.where;
|
|
2889
|
+
} else {
|
|
2890
|
+
cleaned[key] = value;
|
|
2891
|
+
}
|
|
2892
|
+
}
|
|
2893
|
+
return {
|
|
2894
|
+
where: cleaned,
|
|
2895
|
+
search: found
|
|
2896
|
+
};
|
|
2897
|
+
}
|
|
2898
|
+
__name(extractSearch, "extractSearch");
|
|
2899
|
+
function buildTokenFilter(model, fields, token, mode, models, virtualRelations) {
|
|
2900
|
+
const clauses = [];
|
|
2901
|
+
const modelDef = models[model];
|
|
2902
|
+
if (!modelDef) return void 0;
|
|
2903
|
+
for (const [fieldName, fieldSpec] of Object.entries(fields)) {
|
|
2904
|
+
const schemaDef = modelDef.fields[fieldName];
|
|
2905
|
+
if (schemaDef) {
|
|
2906
|
+
if (schemaDef.relation) {
|
|
2907
|
+
const childFields = fieldSpec === true ? void 0 : fieldSpec;
|
|
2908
|
+
if (!childFields) continue;
|
|
2909
|
+
const childFilter = buildTokenFilter(schemaDef.type, childFields, token, mode, models, virtualRelations);
|
|
2910
|
+
if (childFilter) {
|
|
2911
|
+
if (schemaDef.array) {
|
|
2912
|
+
clauses.push({
|
|
2913
|
+
[fieldName]: {
|
|
2914
|
+
some: childFilter
|
|
2915
|
+
}
|
|
2916
|
+
});
|
|
2917
|
+
} else {
|
|
2918
|
+
clauses.push({
|
|
2919
|
+
[fieldName]: childFilter
|
|
2920
|
+
});
|
|
2921
|
+
}
|
|
2922
|
+
}
|
|
2923
|
+
} else if (schemaDef.type === "String") {
|
|
2924
|
+
clauses.push({
|
|
2925
|
+
[fieldName]: buildStringFilter(mode, token)
|
|
2926
|
+
});
|
|
2927
|
+
}
|
|
2928
|
+
} else if (virtualRelations) {
|
|
2929
|
+
const modelVRs = virtualRelations[model];
|
|
2930
|
+
const vr = modelVRs?.[fieldName];
|
|
2931
|
+
if (!vr) continue;
|
|
2932
|
+
const childFields = fieldSpec === true ? void 0 : fieldSpec;
|
|
2933
|
+
if (!childFields) continue;
|
|
2934
|
+
if (vr.kind === "filtered" && vr.relation) {
|
|
2935
|
+
const sourceFieldDef = modelDef.fields[vr.relation];
|
|
2936
|
+
if (!sourceFieldDef) continue;
|
|
2937
|
+
const childFilter = buildTokenFilter(sourceFieldDef.type, childFields, token, mode, models, virtualRelations);
|
|
2938
|
+
if (childFilter) {
|
|
2939
|
+
const mergedChild = vr.where ? {
|
|
2940
|
+
AND: [
|
|
2941
|
+
childFilter,
|
|
2942
|
+
vr.where
|
|
2943
|
+
]
|
|
2944
|
+
} : childFilter;
|
|
2945
|
+
if (sourceFieldDef.array) {
|
|
2946
|
+
clauses.push({
|
|
2947
|
+
[vr.relation]: {
|
|
2948
|
+
some: mergedChild
|
|
2949
|
+
}
|
|
2950
|
+
});
|
|
2951
|
+
} else {
|
|
2952
|
+
clauses.push({
|
|
2953
|
+
[vr.relation]: mergedChild
|
|
2954
|
+
});
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
} else if (vr.kind === "through" && vr.path) {
|
|
2958
|
+
const [sourceField, ...restPath] = vr.path;
|
|
2959
|
+
if (!sourceField) continue;
|
|
2960
|
+
const sourceFieldDef = modelDef.fields[sourceField];
|
|
2961
|
+
if (!sourceFieldDef) continue;
|
|
2962
|
+
let currentModel2 = sourceFieldDef.type;
|
|
2963
|
+
for (const segment of restPath) {
|
|
2964
|
+
const m = models[currentModel2];
|
|
2965
|
+
if (!m) break;
|
|
2966
|
+
const f = m.fields[segment];
|
|
2967
|
+
if (!f) break;
|
|
2968
|
+
currentModel2 = f.type;
|
|
2969
|
+
}
|
|
2970
|
+
const childFilter = buildTokenFilter(currentModel2, childFields, token, mode, models, virtualRelations);
|
|
2971
|
+
if (childFilter) {
|
|
2972
|
+
let wrapped = childFilter;
|
|
2973
|
+
for (let i = restPath.length - 1; i >= 0; i--) {
|
|
2974
|
+
const seg = restPath[i];
|
|
2975
|
+
wrapped = {
|
|
2976
|
+
[seg]: wrapped
|
|
2977
|
+
};
|
|
2978
|
+
}
|
|
2979
|
+
if (sourceFieldDef.array) {
|
|
2980
|
+
clauses.push({
|
|
2981
|
+
[sourceField]: {
|
|
2982
|
+
some: wrapped
|
|
2983
|
+
}
|
|
2984
|
+
});
|
|
2985
|
+
} else {
|
|
2986
|
+
clauses.push({
|
|
2987
|
+
[sourceField]: wrapped
|
|
2988
|
+
});
|
|
2989
|
+
}
|
|
2990
|
+
}
|
|
2991
|
+
}
|
|
2992
|
+
}
|
|
2993
|
+
}
|
|
2994
|
+
if (clauses.length === 0) return void 0;
|
|
2995
|
+
if (clauses.length === 1) return clauses[0];
|
|
2996
|
+
return {
|
|
2997
|
+
OR: clauses
|
|
2998
|
+
};
|
|
2999
|
+
}
|
|
3000
|
+
__name(buildTokenFilter, "buildTokenFilter");
|
|
3001
|
+
function expandSearch(model, where, searchDefaults, schemaModels, virtualRelations) {
|
|
3002
|
+
if (!where || !(0, import_common_helpers7.isPlainObject)(where)) return {
|
|
3003
|
+
where
|
|
3004
|
+
};
|
|
3005
|
+
const { where: remaining, search: search2 } = extractSearch(where);
|
|
3006
|
+
if (!search2) return {
|
|
3007
|
+
where
|
|
3008
|
+
};
|
|
3009
|
+
const profile = search2.profile ? searchDefaults?.[model]?.[search2.profile] : void 0;
|
|
3010
|
+
const fields = search2.fields ?? profile?.fields;
|
|
3011
|
+
if (!fields) return {
|
|
3012
|
+
where: remaining
|
|
3013
|
+
};
|
|
3014
|
+
const mode = search2.mode ?? profile?.mode ?? "contains";
|
|
3015
|
+
const strategy = search2.strategy ?? profile?.strategy ?? "all";
|
|
3016
|
+
const tokens = unique(search2.q.trim().split(/\s+/).map((t) => t.toLowerCase()).filter(Boolean));
|
|
3017
|
+
if (tokens.length === 0) return {
|
|
3018
|
+
where: remaining
|
|
3019
|
+
};
|
|
3020
|
+
const tokenClauses = tokens.map((t) => buildTokenFilter(model, fields, t, mode, schemaModels, virtualRelations)).filter((c) => c !== void 0);
|
|
3021
|
+
if (tokenClauses.length === 0) return {
|
|
3022
|
+
where: remaining
|
|
3023
|
+
};
|
|
3024
|
+
const searchWhere = tokenClauses.length === 1 ? tokenClauses[0] : strategy === "any" ? {
|
|
3025
|
+
OR: tokenClauses
|
|
3026
|
+
} : {
|
|
3027
|
+
AND: tokenClauses
|
|
3028
|
+
};
|
|
3029
|
+
const remainingObj = remaining;
|
|
3030
|
+
const hasRemaining = Object.keys(remainingObj).length > 0;
|
|
3031
|
+
if (!hasRemaining) return {
|
|
3032
|
+
where: searchWhere
|
|
3033
|
+
};
|
|
3034
|
+
return {
|
|
3035
|
+
where: {
|
|
3036
|
+
AND: [
|
|
3037
|
+
remainingObj,
|
|
3038
|
+
searchWhere
|
|
3039
|
+
]
|
|
3040
|
+
}
|
|
3041
|
+
};
|
|
3042
|
+
}
|
|
3043
|
+
__name(expandSearch, "expandSearch");
|
|
3044
|
+
|
|
2827
3045
|
// src/client/crud/operations/base.ts
|
|
2828
3046
|
var CoreCrudOperations2 = [
|
|
2829
3047
|
"findMany",
|
|
@@ -3182,9 +3400,49 @@ var BaseOperationHandler = class {
|
|
|
3182
3400
|
return result;
|
|
3183
3401
|
}
|
|
3184
3402
|
// --- End schema plugin defaults ---
|
|
3403
|
+
// --- Search expansion ---
|
|
3404
|
+
applySearchExpansion(model, args) {
|
|
3405
|
+
const searchDefaults = this.schema.plugins?.searchDefaults;
|
|
3406
|
+
if (!searchDefaults) return args;
|
|
3407
|
+
if (!args?.where) return args;
|
|
3408
|
+
const schemaModels = this.schema.models;
|
|
3409
|
+
if (!schemaModels) return args;
|
|
3410
|
+
const virtualRelations = this.schema.plugins?.virtualRelations;
|
|
3411
|
+
const { where } = expandSearch(model, args.where, searchDefaults, schemaModels, virtualRelations);
|
|
3412
|
+
return {
|
|
3413
|
+
...args,
|
|
3414
|
+
where
|
|
3415
|
+
};
|
|
3416
|
+
}
|
|
3417
|
+
// --- End search expansion ---
|
|
3418
|
+
applyDefaultWhere(model, args) {
|
|
3419
|
+
const defaultOpt = this.client?.$options?.default?.where;
|
|
3420
|
+
if (!defaultOpt) return args;
|
|
3421
|
+
const config = typeof defaultOpt === "function" ? defaultOpt() : defaultOpt;
|
|
3422
|
+
if (!config) return args;
|
|
3423
|
+
const modelWhere = config[model] ?? config.$all;
|
|
3424
|
+
if (!modelWhere || typeof modelWhere !== "object") return args;
|
|
3425
|
+
let result = args ? {
|
|
3426
|
+
...args
|
|
3427
|
+
} : {};
|
|
3428
|
+
const userWhere = result.where ?? {};
|
|
3429
|
+
const mergedWhere = {
|
|
3430
|
+
...userWhere
|
|
3431
|
+
};
|
|
3432
|
+
const modelFields = this.schema.models?.[model]?.fields;
|
|
3433
|
+
for (const [key, value] of Object.entries(modelWhere)) {
|
|
3434
|
+
if (key in userWhere) continue;
|
|
3435
|
+
if (modelFields && !(key in modelFields)) continue;
|
|
3436
|
+
mergedWhere[key] = value;
|
|
3437
|
+
}
|
|
3438
|
+
result.where = Object.keys(mergedWhere).length > 0 ? mergedWhere : void 0;
|
|
3439
|
+
return result;
|
|
3440
|
+
}
|
|
3185
3441
|
async read(kysely, model, args) {
|
|
3186
|
-
const
|
|
3187
|
-
const
|
|
3442
|
+
const defaultWhereArgs = this.applyDefaultWhere(model, args);
|
|
3443
|
+
const defaultedArgs = this.applySchemaPluginDefaults(model, defaultWhereArgs);
|
|
3444
|
+
const searchExpandedArgs = this.applySearchExpansion(model, defaultedArgs);
|
|
3445
|
+
const { args: rewrittenArgs, plan } = this.rewriteVirtualRelations(model, searchExpandedArgs);
|
|
3188
3446
|
const query = this.buildReadQuery(model, rewrittenArgs);
|
|
3189
3447
|
let result = [];
|
|
3190
3448
|
const compiled = kysely.getExecutor().compileQuery(query.toOperationNode(), (0, import_kysely6.createQueryId)());
|
|
@@ -3350,7 +3608,7 @@ var BaseOperationHandler = class {
|
|
|
3350
3608
|
}
|
|
3351
3609
|
});
|
|
3352
3610
|
const discriminatorField = getDiscriminatorField(this.schema, model);
|
|
3353
|
-
(0,
|
|
3611
|
+
(0, import_common_helpers8.invariant)(discriminatorField, `Base model "${model}" must have a discriminator field`);
|
|
3354
3612
|
thisCreateFields[discriminatorField] = forModel;
|
|
3355
3613
|
const baseEntity = await this.create(kysely, model, thisCreateFields, void 0, true);
|
|
3356
3614
|
const idValues = extractIdFields(baseEntity, this.schema, model);
|
|
@@ -3362,8 +3620,8 @@ var BaseOperationHandler = class {
|
|
|
3362
3620
|
}
|
|
3363
3621
|
async buildFkAssignments(kysely, model, relationField, entity) {
|
|
3364
3622
|
const parentFkFields = {};
|
|
3365
|
-
(0,
|
|
3366
|
-
(0,
|
|
3623
|
+
(0, import_common_helpers8.invariant)(relationField, "parentField must be defined if parentModel is defined");
|
|
3624
|
+
(0, import_common_helpers8.invariant)(entity, "parentEntity must be defined if parentModel is defined");
|
|
3367
3625
|
const { keyPairs } = getRelationForeignKeyFieldPairs(this.schema, model, relationField);
|
|
3368
3626
|
for (const pair of keyPairs) {
|
|
3369
3627
|
if (!(pair.pk in entity)) {
|
|
@@ -3392,8 +3650,8 @@ var BaseOperationHandler = class {
|
|
|
3392
3650
|
const leftFirst = leftModel !== rightModel ? leftModel.localeCompare(rightModel) <= 0 : leftField.localeCompare(rightField) <= 0;
|
|
3393
3651
|
const leftIdField = requireIdFields(this.schema, leftModel);
|
|
3394
3652
|
const rightIdField = requireIdFields(this.schema, rightModel);
|
|
3395
|
-
(0,
|
|
3396
|
-
(0,
|
|
3653
|
+
(0, import_common_helpers8.invariant)(leftIdField.length === 1, "many-to-many relation must have exactly one id field");
|
|
3654
|
+
(0, import_common_helpers8.invariant)(rightIdField.length === 1, "many-to-many relation must have exactly one id field");
|
|
3397
3655
|
const leftIdValue = leftEntity[leftIdField[0]];
|
|
3398
3656
|
const rightIdValues = rightEntities.map((e) => e[rightIdField[0]]);
|
|
3399
3657
|
if (action === "connect") {
|
|
@@ -3418,10 +3676,10 @@ var BaseOperationHandler = class {
|
|
|
3418
3676
|
}
|
|
3419
3677
|
}
|
|
3420
3678
|
resetManyToManyRelation(kysely, model, field, parentIds) {
|
|
3421
|
-
(0,
|
|
3679
|
+
(0, import_common_helpers8.invariant)(Object.keys(parentIds).length === 1, "parentIds must have exactly one field");
|
|
3422
3680
|
const parentId = Object.values(parentIds)[0];
|
|
3423
3681
|
const m2m = getManyToManyRelation(this.schema, model, field);
|
|
3424
|
-
(0,
|
|
3682
|
+
(0, import_common_helpers8.invariant)(m2m, "not a many-to-many relation");
|
|
3425
3683
|
const eb = (0, import_kysely6.expressionBuilder)();
|
|
3426
3684
|
return kysely.deleteFrom(m2m.joinTable).where(eb(`${m2m.joinTable}.${m2m.parentFkName}`, "=", parentId)).execute();
|
|
3427
3685
|
}
|
|
@@ -3443,7 +3701,7 @@ var BaseOperationHandler = class {
|
|
|
3443
3701
|
}
|
|
3444
3702
|
case "connect": {
|
|
3445
3703
|
const referencedPkFields = relationField.relation.references;
|
|
3446
|
-
(0,
|
|
3704
|
+
(0, import_common_helpers8.invariant)(referencedPkFields, "relation must have fields info");
|
|
3447
3705
|
const extractedFks = extractFields(subPayload, referencedPkFields);
|
|
3448
3706
|
if (Object.keys(extractedFks).length === referencedPkFields.length) {
|
|
3449
3707
|
result = extractedFks;
|
|
@@ -3490,13 +3748,13 @@ var BaseOperationHandler = class {
|
|
|
3490
3748
|
}
|
|
3491
3749
|
switch (action) {
|
|
3492
3750
|
case "create": {
|
|
3493
|
-
for (const item of (0,
|
|
3751
|
+
for (const item of (0, import_common_helpers8.enumerate)(subPayload)) {
|
|
3494
3752
|
await this.create(kysely, relationModel, item, fromRelationContext);
|
|
3495
3753
|
}
|
|
3496
3754
|
break;
|
|
3497
3755
|
}
|
|
3498
3756
|
case "createMany": {
|
|
3499
|
-
(0,
|
|
3757
|
+
(0, import_common_helpers8.invariant)(relationFieldDef.array, "relation must be an array for createMany");
|
|
3500
3758
|
await this.createMany(kysely, relationModel, subPayload, false, fromRelationContext);
|
|
3501
3759
|
break;
|
|
3502
3760
|
}
|
|
@@ -3505,7 +3763,7 @@ var BaseOperationHandler = class {
|
|
|
3505
3763
|
break;
|
|
3506
3764
|
}
|
|
3507
3765
|
case "connectOrCreate": {
|
|
3508
|
-
for (const item of (0,
|
|
3766
|
+
for (const item of (0, import_common_helpers8.enumerate)(subPayload)) {
|
|
3509
3767
|
const found = await this.exists(kysely, relationModel, item.where);
|
|
3510
3768
|
if (!found) {
|
|
3511
3769
|
await this.create(kysely, relationModel, item.create, fromRelationContext);
|
|
@@ -3535,11 +3793,11 @@ var BaseOperationHandler = class {
|
|
|
3535
3793
|
}
|
|
3536
3794
|
relationKeyPairs.push(...keyPairs);
|
|
3537
3795
|
}
|
|
3538
|
-
let createData = (0,
|
|
3796
|
+
let createData = (0, import_common_helpers8.enumerate)(input.data).map((item) => {
|
|
3539
3797
|
const newItem = {};
|
|
3540
3798
|
for (const [name, value] of Object.entries(item)) {
|
|
3541
3799
|
const fieldDef = this.requireField(model, name);
|
|
3542
|
-
(0,
|
|
3800
|
+
(0, import_common_helpers8.invariant)(!fieldDef.relation, "createMany does not support relations");
|
|
3543
3801
|
newItem[name] = this.dialect.transformInput(value, fieldDef.type, !!fieldDef.array);
|
|
3544
3802
|
}
|
|
3545
3803
|
if (fromRelation) {
|
|
@@ -3602,7 +3860,7 @@ var BaseOperationHandler = class {
|
|
|
3602
3860
|
const thisCreateRows = [];
|
|
3603
3861
|
const remainingFieldRows = [];
|
|
3604
3862
|
const discriminatorField = getDiscriminatorField(this.schema, model);
|
|
3605
|
-
(0,
|
|
3863
|
+
(0, import_common_helpers8.invariant)(discriminatorField, `Base model "${model}" must have a discriminator field`);
|
|
3606
3864
|
for (const createFields of createRows) {
|
|
3607
3865
|
const thisCreateFields = {};
|
|
3608
3866
|
const remainingFields = {};
|
|
@@ -3641,7 +3899,7 @@ var BaseOperationHandler = class {
|
|
|
3641
3899
|
}
|
|
3642
3900
|
fillGeneratedAndDefaultValues(modelDef, data) {
|
|
3643
3901
|
const fields = modelDef.fields;
|
|
3644
|
-
const values = (0,
|
|
3902
|
+
const values = (0, import_common_helpers8.clone)(data);
|
|
3645
3903
|
for (const [field, fieldDef] of Object.entries(fields)) {
|
|
3646
3904
|
if (fieldDef.originModel) {
|
|
3647
3905
|
continue;
|
|
@@ -3732,7 +3990,7 @@ var BaseOperationHandler = class {
|
|
|
3732
3990
|
const hasNonIgnoredFields = Object.keys(data).some((field) => (isScalarField(this.schema, modelDef.name, field) || isForeignKeyField(this.schema, modelDef.name, field)) && !ignoredFields.has(field));
|
|
3733
3991
|
if (hasNonIgnoredFields) {
|
|
3734
3992
|
if (finalData === data) {
|
|
3735
|
-
finalData = (0,
|
|
3993
|
+
finalData = (0, import_common_helpers8.clone)(data);
|
|
3736
3994
|
}
|
|
3737
3995
|
finalData[fieldName] = this.dialect.transformInput(/* @__PURE__ */ new Date(), "DateTime", false);
|
|
3738
3996
|
autoUpdatedFields.push(fieldName);
|
|
@@ -3835,7 +4093,7 @@ var BaseOperationHandler = class {
|
|
|
3835
4093
|
const updatingIdFields = idFields.some((idField) => idField in updateFields);
|
|
3836
4094
|
if (updatingIdFields) {
|
|
3837
4095
|
const origIdValues = await loadThisEntity();
|
|
3838
|
-
(0,
|
|
4096
|
+
(0, import_common_helpers8.invariant)(origIdValues, "Original entity should have been loaded for update without RETURNING");
|
|
3839
4097
|
readFilter = {
|
|
3840
4098
|
...origIdValues
|
|
3841
4099
|
};
|
|
@@ -3894,7 +4152,7 @@ var BaseOperationHandler = class {
|
|
|
3894
4152
|
}
|
|
3895
4153
|
} else {
|
|
3896
4154
|
const fromRelationFieldDef = this.requireField(fromRelation.model, fromRelation.field);
|
|
3897
|
-
(0,
|
|
4155
|
+
(0, import_common_helpers8.invariant)(fromRelationFieldDef.relation?.opposite);
|
|
3898
4156
|
parentWhere[fromRelationFieldDef.relation.opposite] = {
|
|
3899
4157
|
some: fromRelation.ids
|
|
3900
4158
|
};
|
|
@@ -3945,7 +4203,7 @@ var BaseOperationHandler = class {
|
|
|
3945
4203
|
};
|
|
3946
4204
|
}
|
|
3947
4205
|
transformIncrementalUpdate(model, field, fieldDef, payload) {
|
|
3948
|
-
(0,
|
|
4206
|
+
(0, import_common_helpers8.invariant)(Object.keys(payload).length === 1, 'Only one of "set", "increment", "decrement", "multiply", or "divide" can be provided');
|
|
3949
4207
|
const key = Object.keys(payload)[0];
|
|
3950
4208
|
const value = this.dialect.transformInput(payload[key], fieldDef.type, false);
|
|
3951
4209
|
const eb = (0, import_kysely6.expressionBuilder)();
|
|
@@ -3955,7 +4213,7 @@ var BaseOperationHandler = class {
|
|
|
3955
4213
|
});
|
|
3956
4214
|
}
|
|
3957
4215
|
transformScalarListUpdate(model, field, fieldDef, payload) {
|
|
3958
|
-
(0,
|
|
4216
|
+
(0, import_common_helpers8.invariant)(Object.keys(payload).length === 1, 'Only one of "set", "push" can be provided');
|
|
3959
4217
|
const key = Object.keys(payload)[0];
|
|
3960
4218
|
const value = this.dialect.transformInput(payload[key], fieldDef.type, true);
|
|
3961
4219
|
const eb = (0, import_kysely6.expressionBuilder)();
|
|
@@ -4115,14 +4373,14 @@ var BaseOperationHandler = class {
|
|
|
4115
4373
|
for (const [key, value] of Object.entries(args)) {
|
|
4116
4374
|
switch (key) {
|
|
4117
4375
|
case "create": {
|
|
4118
|
-
(0,
|
|
4119
|
-
for (const item of (0,
|
|
4376
|
+
(0, import_common_helpers8.invariant)(!Array.isArray(value) || fieldDef.array, "relation must be an array if create is an array");
|
|
4377
|
+
for (const item of (0, import_common_helpers8.enumerate)(value)) {
|
|
4120
4378
|
await this.create(kysely, fieldModel, item, fromRelationContext);
|
|
4121
4379
|
}
|
|
4122
4380
|
break;
|
|
4123
4381
|
}
|
|
4124
4382
|
case "createMany": {
|
|
4125
|
-
(0,
|
|
4383
|
+
(0, import_common_helpers8.invariant)(fieldDef.array, "relation must be an array for createMany");
|
|
4126
4384
|
await this.createMany(kysely, fieldModel, value, false, fromRelationContext);
|
|
4127
4385
|
break;
|
|
4128
4386
|
}
|
|
@@ -4139,12 +4397,12 @@ var BaseOperationHandler = class {
|
|
|
4139
4397
|
break;
|
|
4140
4398
|
}
|
|
4141
4399
|
case "set": {
|
|
4142
|
-
(0,
|
|
4400
|
+
(0, import_common_helpers8.invariant)(fieldDef.array, "relation must be an array");
|
|
4143
4401
|
await this.setRelation(kysely, fieldModel, value, fromRelationContext);
|
|
4144
4402
|
break;
|
|
4145
4403
|
}
|
|
4146
4404
|
case "update": {
|
|
4147
|
-
for (const _item of (0,
|
|
4405
|
+
for (const _item of (0, import_common_helpers8.enumerate)(value)) {
|
|
4148
4406
|
const item = _item;
|
|
4149
4407
|
let where;
|
|
4150
4408
|
let data;
|
|
@@ -4161,7 +4419,7 @@ var BaseOperationHandler = class {
|
|
|
4161
4419
|
break;
|
|
4162
4420
|
}
|
|
4163
4421
|
case "upsert": {
|
|
4164
|
-
for (const _item of (0,
|
|
4422
|
+
for (const _item of (0, import_common_helpers8.enumerate)(value)) {
|
|
4165
4423
|
const item = _item;
|
|
4166
4424
|
const updated = await this.update(kysely, fieldModel, item.where, item.update, fromRelationContext, true, false);
|
|
4167
4425
|
if (!updated) {
|
|
@@ -4171,7 +4429,7 @@ var BaseOperationHandler = class {
|
|
|
4171
4429
|
break;
|
|
4172
4430
|
}
|
|
4173
4431
|
case "updateMany": {
|
|
4174
|
-
for (const _item of (0,
|
|
4432
|
+
for (const _item of (0, import_common_helpers8.enumerate)(value)) {
|
|
4175
4433
|
const item = _item;
|
|
4176
4434
|
await this.updateMany(kysely, fieldModel, item.where, item.data, item.limit, false, fieldModel, fromRelationContext);
|
|
4177
4435
|
}
|
|
@@ -4208,7 +4466,7 @@ var BaseOperationHandler = class {
|
|
|
4208
4466
|
} else {
|
|
4209
4467
|
const { ownedByModel, keyPairs } = getRelationForeignKeyFieldPairs(this.schema, fromRelation.model, fromRelation.field);
|
|
4210
4468
|
if (ownedByModel) {
|
|
4211
|
-
(0,
|
|
4469
|
+
(0, import_common_helpers8.invariant)(_data.length === 1, "only one entity can be connected");
|
|
4212
4470
|
const target = await this.readUnique(kysely, model, {
|
|
4213
4471
|
where: _data[0]
|
|
4214
4472
|
});
|
|
@@ -4245,7 +4503,7 @@ var BaseOperationHandler = class {
|
|
|
4245
4503
|
}
|
|
4246
4504
|
}
|
|
4247
4505
|
async connectOrCreateRelation(kysely, model, data, fromRelation) {
|
|
4248
|
-
const _data = (0,
|
|
4506
|
+
const _data = (0, import_common_helpers8.enumerate)(data);
|
|
4249
4507
|
if (_data.length === 0) {
|
|
4250
4508
|
return;
|
|
4251
4509
|
}
|
|
@@ -4289,7 +4547,7 @@ var BaseOperationHandler = class {
|
|
|
4289
4547
|
const { ownedByModel, keyPairs } = getRelationForeignKeyFieldPairs(this.schema, fromRelation.model, fromRelation.field);
|
|
4290
4548
|
const eb = (0, import_kysely6.expressionBuilder)();
|
|
4291
4549
|
if (ownedByModel) {
|
|
4292
|
-
(0,
|
|
4550
|
+
(0, import_common_helpers8.invariant)(disconnectConditions.length === 1, "only one entity can be disconnected");
|
|
4293
4551
|
const condition = disconnectConditions[0];
|
|
4294
4552
|
if (condition === true) {
|
|
4295
4553
|
for (const { fk } of keyPairs) {
|
|
@@ -4417,7 +4675,7 @@ var BaseOperationHandler = class {
|
|
|
4417
4675
|
if (m2m) {
|
|
4418
4676
|
deleteFromModel = model;
|
|
4419
4677
|
const fieldDef = this.requireField(fromRelation.model, fromRelation.field);
|
|
4420
|
-
(0,
|
|
4678
|
+
(0, import_common_helpers8.invariant)(fieldDef.relation?.opposite);
|
|
4421
4679
|
deleteResult = await this.delete(kysely, model, {
|
|
4422
4680
|
AND: [
|
|
4423
4681
|
{
|
|
@@ -4441,7 +4699,7 @@ var BaseOperationHandler = class {
|
|
|
4441
4699
|
throw createNotFoundError(fromRelation.model);
|
|
4442
4700
|
}
|
|
4443
4701
|
const fieldDef = this.requireField(fromRelation.model, fromRelation.field);
|
|
4444
|
-
(0,
|
|
4702
|
+
(0, import_common_helpers8.invariant)(fieldDef.relation?.opposite);
|
|
4445
4703
|
deleteResult = await this.delete(kysely, model, {
|
|
4446
4704
|
AND: [
|
|
4447
4705
|
// filter for parent
|
|
@@ -4474,7 +4732,7 @@ var BaseOperationHandler = class {
|
|
|
4474
4732
|
}
|
|
4475
4733
|
}
|
|
4476
4734
|
normalizeRelationManipulationInput(model, data) {
|
|
4477
|
-
return (0,
|
|
4735
|
+
return (0, import_common_helpers8.enumerate)(data).map((item) => flattenCompoundUniqueFilters(this.schema, model, item));
|
|
4478
4736
|
}
|
|
4479
4737
|
// #endregion
|
|
4480
4738
|
async delete(kysely, model, where, limit, filterModel, fieldsToReturn) {
|
|
@@ -4595,7 +4853,7 @@ var BaseOperationHandler = class {
|
|
|
4595
4853
|
if (!args) {
|
|
4596
4854
|
return;
|
|
4597
4855
|
}
|
|
4598
|
-
const newArgs = (0,
|
|
4856
|
+
const newArgs = (0, import_common_helpers8.clone)(args);
|
|
4599
4857
|
this.doNormalizeArgs(newArgs);
|
|
4600
4858
|
return newArgs;
|
|
4601
4859
|
}
|
|
@@ -4604,7 +4862,7 @@ var BaseOperationHandler = class {
|
|
|
4604
4862
|
for (const [key, value] of Object.entries(args)) {
|
|
4605
4863
|
if (value === void 0) {
|
|
4606
4864
|
delete args[key];
|
|
4607
|
-
} else if (value && (0,
|
|
4865
|
+
} else if (value && (0, import_common_helpers8.isPlainObject)(value)) {
|
|
4608
4866
|
this.doNormalizeArgs(value);
|
|
4609
4867
|
}
|
|
4610
4868
|
}
|
|
@@ -5266,11 +5524,11 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
5266
5524
|
};
|
|
5267
5525
|
|
|
5268
5526
|
// src/client/crud/validator/validator.ts
|
|
5269
|
-
var
|
|
5527
|
+
var import_common_helpers10 = require("@zenstackhq/common-helpers");
|
|
5270
5528
|
var import_ts_pattern11 = require("ts-pattern");
|
|
5271
5529
|
|
|
5272
5530
|
// src/client/zod/factory.ts
|
|
5273
|
-
var
|
|
5531
|
+
var import_common_helpers9 = require("@zenstackhq/common-helpers");
|
|
5274
5532
|
var import_zod = require("@zenstackhq/zod");
|
|
5275
5533
|
var import_decimal4 = __toESM(require("decimal.js"), 1);
|
|
5276
5534
|
var import_ts_pattern10 = require("ts-pattern");
|
|
@@ -5406,10 +5664,10 @@ var ZodSchemaFactory = class {
|
|
|
5406
5664
|
}
|
|
5407
5665
|
makeFindSchema(model, operation, options) {
|
|
5408
5666
|
const fields = {};
|
|
5409
|
-
const
|
|
5667
|
+
const unique2 = operation === "findUnique";
|
|
5410
5668
|
const findOne = operation === "findUnique" || operation === "findFirst";
|
|
5411
|
-
const where = this.makeWhereSchema(model,
|
|
5412
|
-
if (
|
|
5669
|
+
const where = this.makeWhereSchema(model, unique2, false, false, options);
|
|
5670
|
+
if (unique2) {
|
|
5413
5671
|
fields["where"] = where;
|
|
5414
5672
|
} else {
|
|
5415
5673
|
fields["where"] = where.optional();
|
|
@@ -5417,7 +5675,7 @@ var ZodSchemaFactory = class {
|
|
|
5417
5675
|
fields["select"] = this.makeSelectSchema(model, options).optional().nullable();
|
|
5418
5676
|
fields["include"] = this.makeIncludeSchema(model, options).optional().nullable();
|
|
5419
5677
|
fields["omit"] = this.makeOmitSchema(model).optional().nullable();
|
|
5420
|
-
if (!
|
|
5678
|
+
if (!unique2) {
|
|
5421
5679
|
fields["skip"] = this.makeSkipSchema().optional();
|
|
5422
5680
|
if (findOne) {
|
|
5423
5681
|
fields["take"] = import_zod2.z.literal(1).optional();
|
|
@@ -5433,7 +5691,7 @@ var ZodSchemaFactory = class {
|
|
|
5433
5691
|
result = this.refineForSelectIncludeMutuallyExclusive(result);
|
|
5434
5692
|
result = this.refineForSelectOmitMutuallyExclusive(result);
|
|
5435
5693
|
result = this.refineForSelectHasTruthyField(result);
|
|
5436
|
-
if (!
|
|
5694
|
+
if (!unique2) {
|
|
5437
5695
|
result = result.optional();
|
|
5438
5696
|
}
|
|
5439
5697
|
return result;
|
|
@@ -5467,12 +5725,12 @@ var ZodSchemaFactory = class {
|
|
|
5467
5725
|
}
|
|
5468
5726
|
makeEnumSchema(_enum) {
|
|
5469
5727
|
const enumDef = getEnum(this.schema, _enum);
|
|
5470
|
-
(0,
|
|
5728
|
+
(0, import_common_helpers9.invariant)(enumDef, `Enum "${_enum}" not found in schema`);
|
|
5471
5729
|
return import_zod2.z.enum(Object.keys(enumDef.values));
|
|
5472
5730
|
}
|
|
5473
5731
|
makeTypeDefSchema(type) {
|
|
5474
5732
|
const typeDef = getTypeDef(this.schema, type);
|
|
5475
|
-
(0,
|
|
5733
|
+
(0, import_common_helpers9.invariant)(typeDef, `Type definition "${type}" not found in schema`);
|
|
5476
5734
|
const schema = zLooseObject(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
|
|
5477
5735
|
let fieldSchema = this.makeScalarSchema(def.type);
|
|
5478
5736
|
if (def.array) {
|
|
@@ -5494,8 +5752,8 @@ var ZodSchemaFactory = class {
|
|
|
5494
5752
|
});
|
|
5495
5753
|
return finalSchema;
|
|
5496
5754
|
}
|
|
5497
|
-
makeWhereSchema(model,
|
|
5498
|
-
const uniqueFieldNames =
|
|
5755
|
+
makeWhereSchema(model, unique2, withoutRelationFields = false, withAggregations = false, options) {
|
|
5756
|
+
const uniqueFieldNames = unique2 ? getUniqueFields(this.schema, model).filter((uf) => (
|
|
5499
5757
|
// single-field unique
|
|
5500
5758
|
"def" in uf
|
|
5501
5759
|
)).map((uf) => uf.name) : void 0;
|
|
@@ -5551,12 +5809,12 @@ var ZodSchemaFactory = class {
|
|
|
5551
5809
|
fields[field] = fieldSchema.optional();
|
|
5552
5810
|
}
|
|
5553
5811
|
}
|
|
5554
|
-
if (
|
|
5812
|
+
if (unique2) {
|
|
5555
5813
|
const uniqueFields = getUniqueFields(this.schema, model);
|
|
5556
5814
|
for (const uniqueField of uniqueFields) {
|
|
5557
5815
|
if ("defs" in uniqueField) {
|
|
5558
5816
|
fields[uniqueField.name] = import_zod2.z.object(Object.fromEntries(Object.entries(uniqueField.defs).map(([key, def]) => {
|
|
5559
|
-
(0,
|
|
5817
|
+
(0, import_common_helpers9.invariant)(!def.relation, "unique field cannot be a relation");
|
|
5560
5818
|
let fieldSchema;
|
|
5561
5819
|
const enumDef = getEnum(this.schema, def.type);
|
|
5562
5820
|
if (enumDef) {
|
|
@@ -5588,7 +5846,7 @@ var ZodSchemaFactory = class {
|
|
|
5588
5846
|
fields["NOT"] = this.orArray(import_zod2.z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields, false, options)), true).optional();
|
|
5589
5847
|
const baseWhere = import_zod2.z.strictObject(fields);
|
|
5590
5848
|
let result = baseWhere;
|
|
5591
|
-
if (
|
|
5849
|
+
if (unique2) {
|
|
5592
5850
|
const uniqueFields = getUniqueFields(this.schema, model);
|
|
5593
5851
|
if (uniqueFields.length === 0) {
|
|
5594
5852
|
throw createInternalError(`Model "${model}" has no unique fields`);
|
|
@@ -5611,7 +5869,7 @@ var ZodSchemaFactory = class {
|
|
|
5611
5869
|
const optional = !!fieldInfo.optional;
|
|
5612
5870
|
const array = !!fieldInfo.array;
|
|
5613
5871
|
const typeDef = getTypeDef(this.schema, type);
|
|
5614
|
-
(0,
|
|
5872
|
+
(0, import_common_helpers9.invariant)(typeDef, `Type definition "${type}" not found in schema`);
|
|
5615
5873
|
const candidates = [];
|
|
5616
5874
|
if (!array) {
|
|
5617
5875
|
const fieldSchemas = {};
|
|
@@ -5663,7 +5921,7 @@ var ZodSchemaFactory = class {
|
|
|
5663
5921
|
const optional = !!fieldInfo.optional;
|
|
5664
5922
|
const array = !!fieldInfo.array;
|
|
5665
5923
|
const enumDef = getEnum(this.schema, enumName);
|
|
5666
|
-
(0,
|
|
5924
|
+
(0, import_common_helpers9.invariant)(enumDef, `Enum "${enumName}" not found in schema`);
|
|
5667
5925
|
const baseSchema = import_zod2.z.enum(Object.keys(enumDef.values));
|
|
5668
5926
|
if (array) {
|
|
5669
5927
|
return this.internalMakeArrayFilterSchema(model, fieldInfo.name, baseSchema);
|
|
@@ -5956,7 +6214,7 @@ var ZodSchemaFactory = class {
|
|
|
5956
6214
|
for (const plugin of this.plugins) {
|
|
5957
6215
|
const resultConfig = plugin.result;
|
|
5958
6216
|
if (resultConfig) {
|
|
5959
|
-
const modelConfig = resultConfig[(0,
|
|
6217
|
+
const modelConfig = resultConfig[(0, import_common_helpers9.lowerCaseFirst)(model)];
|
|
5960
6218
|
if (modelConfig) {
|
|
5961
6219
|
for (const field of Object.keys(modelConfig)) {
|
|
5962
6220
|
fields[field] = import_zod2.z.boolean().optional();
|
|
@@ -6499,7 +6757,7 @@ var ZodSchemaFactory = class {
|
|
|
6499
6757
|
});
|
|
6500
6758
|
let schema = this.mergePluginArgsSchema(baseSchema, "groupBy");
|
|
6501
6759
|
schema = schema.refine((value) => {
|
|
6502
|
-
const bys = (0,
|
|
6760
|
+
const bys = (0, import_common_helpers9.enumerate)(value.by);
|
|
6503
6761
|
if (value.having && typeof value.having === "object") {
|
|
6504
6762
|
for (const [key, val] of Object.entries(value.having)) {
|
|
6505
6763
|
if (AggregateOperators.includes(key)) {
|
|
@@ -6519,8 +6777,8 @@ var ZodSchemaFactory = class {
|
|
|
6519
6777
|
return true;
|
|
6520
6778
|
}, 'fields in "having" must be in "by"');
|
|
6521
6779
|
schema = schema.refine((value) => {
|
|
6522
|
-
const bys = (0,
|
|
6523
|
-
for (const orderBy of (0,
|
|
6780
|
+
const bys = (0, import_common_helpers9.enumerate)(value.by);
|
|
6781
|
+
for (const orderBy of (0, import_common_helpers9.enumerate)(value.orderBy)) {
|
|
6524
6782
|
if (orderBy && Object.keys(orderBy).filter((f) => !AggregateOperators.includes(f)).some((key) => !bys.includes(key))) {
|
|
6525
6783
|
return false;
|
|
6526
6784
|
}
|
|
@@ -6535,7 +6793,7 @@ var ZodSchemaFactory = class {
|
|
|
6535
6793
|
continue;
|
|
6536
6794
|
}
|
|
6537
6795
|
if (LOGICAL_COMBINATORS.includes(key)) {
|
|
6538
|
-
if ((0,
|
|
6796
|
+
if ((0, import_common_helpers9.enumerate)(value).every((v) => this.onlyAggregationFields(v))) {
|
|
6539
6797
|
continue;
|
|
6540
6798
|
}
|
|
6541
6799
|
}
|
|
@@ -6595,8 +6853,8 @@ var ZodSchemaFactory = class {
|
|
|
6595
6853
|
const createSchema = "$create" in plugin.queryArgs && plugin.queryArgs["$create"] ? plugin.queryArgs["$create"] : void 0;
|
|
6596
6854
|
const updateSchema = "$update" in plugin.queryArgs && plugin.queryArgs["$update"] ? plugin.queryArgs["$update"] : void 0;
|
|
6597
6855
|
if (createSchema && updateSchema) {
|
|
6598
|
-
(0,
|
|
6599
|
-
(0,
|
|
6856
|
+
(0, import_common_helpers9.invariant)(createSchema instanceof import_zod2.ZodObject, "Plugin extended query args schema must be a Zod object");
|
|
6857
|
+
(0, import_common_helpers9.invariant)(updateSchema instanceof import_zod2.ZodObject, "Plugin extended query args schema must be a Zod object");
|
|
6600
6858
|
result = createSchema.extend(updateSchema.shape);
|
|
6601
6859
|
} else if (createSchema) {
|
|
6602
6860
|
result = createSchema;
|
|
@@ -6617,7 +6875,7 @@ var ZodSchemaFactory = class {
|
|
|
6617
6875
|
} else if ("$all" in plugin.queryArgs && plugin.queryArgs["$all"]) {
|
|
6618
6876
|
result = plugin.queryArgs["$all"];
|
|
6619
6877
|
}
|
|
6620
|
-
(0,
|
|
6878
|
+
(0, import_common_helpers9.invariant)(result === void 0 || result instanceof import_zod2.ZodObject, "Plugin extended query args schema must be a Zod object");
|
|
6621
6879
|
return result;
|
|
6622
6880
|
}
|
|
6623
6881
|
// #endregion
|
|
@@ -6671,7 +6929,7 @@ var ZodSchemaFactory = class {
|
|
|
6671
6929
|
return void 0;
|
|
6672
6930
|
}
|
|
6673
6931
|
const modelsRecord = slicing.models;
|
|
6674
|
-
const modelConfig = modelsRecord[(0,
|
|
6932
|
+
const modelConfig = modelsRecord[(0, import_common_helpers9.lowerCaseFirst)(model)];
|
|
6675
6933
|
if (modelConfig?.fields) {
|
|
6676
6934
|
const fieldConfig = modelConfig.fields[field];
|
|
6677
6935
|
if (fieldConfig) {
|
|
@@ -7278,7 +7536,7 @@ var InputValidator = class {
|
|
|
7278
7536
|
return input;
|
|
7279
7537
|
}
|
|
7280
7538
|
const procDef = (this.client.$schema.procedures ?? {})[proc];
|
|
7281
|
-
(0,
|
|
7539
|
+
(0, import_common_helpers10.invariant)(procDef, `Procedure "${proc}" not found in schema`);
|
|
7282
7540
|
const params = Object.values(procDef.params ?? {});
|
|
7283
7541
|
if (typeof input === "undefined") {
|
|
7284
7542
|
if (params.length === 0) {
|
|
@@ -7539,12 +7797,12 @@ function performanceNow() {
|
|
|
7539
7797
|
__name(performanceNow, "performanceNow");
|
|
7540
7798
|
|
|
7541
7799
|
// src/client/executor/zenstack-query-executor.ts
|
|
7542
|
-
var
|
|
7800
|
+
var import_common_helpers12 = require("@zenstackhq/common-helpers");
|
|
7543
7801
|
var import_kysely9 = require("kysely");
|
|
7544
7802
|
var import_ts_pattern12 = require("ts-pattern");
|
|
7545
7803
|
|
|
7546
7804
|
// src/client/executor/name-mapper.ts
|
|
7547
|
-
var
|
|
7805
|
+
var import_common_helpers11 = require("@zenstackhq/common-helpers");
|
|
7548
7806
|
var import_kysely7 = require("kysely");
|
|
7549
7807
|
var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
7550
7808
|
static {
|
|
@@ -7617,7 +7875,7 @@ var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
|
7617
7875
|
return super.transformInsertQuery(node, queryId);
|
|
7618
7876
|
}
|
|
7619
7877
|
const model = extractModelName(node.into);
|
|
7620
|
-
(0,
|
|
7878
|
+
(0, import_common_helpers11.invariant)(model, 'InsertQueryNode must have a model name in the "into" clause');
|
|
7621
7879
|
return this.withScope({
|
|
7622
7880
|
model
|
|
7623
7881
|
}, () => {
|
|
@@ -7700,7 +7958,7 @@ var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
|
7700
7958
|
return super.transformUpdateQuery(node);
|
|
7701
7959
|
}
|
|
7702
7960
|
const model = extractModelName(innerTable);
|
|
7703
|
-
(0,
|
|
7961
|
+
(0, import_common_helpers11.invariant)(model, 'UpdateQueryNode must have a model name in the "table" clause');
|
|
7704
7962
|
return this.withScope({
|
|
7705
7963
|
model,
|
|
7706
7964
|
alias
|
|
@@ -8004,7 +8262,7 @@ var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
|
8004
8262
|
}
|
|
8005
8263
|
requireCurrentScope() {
|
|
8006
8264
|
const scope = this.scopes[this.scopes.length - 1];
|
|
8007
|
-
(0,
|
|
8265
|
+
(0, import_common_helpers11.invariant)(scope, "No scope available");
|
|
8008
8266
|
return scope;
|
|
8009
8267
|
}
|
|
8010
8268
|
// #endregion
|
|
@@ -8471,7 +8729,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8471
8729
|
for (const valuesItem of values.values) {
|
|
8472
8730
|
const rowIds = {};
|
|
8473
8731
|
if (import_kysely9.PrimitiveValueListNode.is(valuesItem)) {
|
|
8474
|
-
(0,
|
|
8732
|
+
(0, import_common_helpers12.invariant)(valuesItem.values.length === columns.length, "Values count must match columns count");
|
|
8475
8733
|
for (const idField of idFields) {
|
|
8476
8734
|
const colIndex = columns.findIndex((col) => col.column.name === idField);
|
|
8477
8735
|
if (colIndex === -1) {
|
|
@@ -8480,7 +8738,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8480
8738
|
rowIds[idField] = valuesItem.values[colIndex];
|
|
8481
8739
|
}
|
|
8482
8740
|
} else {
|
|
8483
|
-
(0,
|
|
8741
|
+
(0, import_common_helpers12.invariant)(valuesItem.values.length === columns.length, "Values count must match columns count");
|
|
8484
8742
|
for (const idField of idFields) {
|
|
8485
8743
|
const colIndex = columns.findIndex((col) => col.column.name === idField);
|
|
8486
8744
|
if (colIndex === -1) {
|
|
@@ -8537,17 +8795,17 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8537
8795
|
}
|
|
8538
8796
|
getMutationModel(queryNode) {
|
|
8539
8797
|
return (0, import_ts_pattern12.match)(queryNode).when(import_kysely9.InsertQueryNode.is, (node) => {
|
|
8540
|
-
(0,
|
|
8798
|
+
(0, import_common_helpers12.invariant)(node.into, "InsertQueryNode must have an into clause");
|
|
8541
8799
|
return node.into.table.identifier.name;
|
|
8542
8800
|
}).when(import_kysely9.UpdateQueryNode.is, (node) => {
|
|
8543
|
-
(0,
|
|
8801
|
+
(0, import_common_helpers12.invariant)(node.table, "UpdateQueryNode must have a table");
|
|
8544
8802
|
const { node: tableNode } = stripAlias(node.table);
|
|
8545
|
-
(0,
|
|
8803
|
+
(0, import_common_helpers12.invariant)(import_kysely9.TableNode.is(tableNode), "UpdateQueryNode must use a TableNode");
|
|
8546
8804
|
return tableNode.table.identifier.name;
|
|
8547
8805
|
}).when(import_kysely9.DeleteQueryNode.is, (node) => {
|
|
8548
|
-
(0,
|
|
8806
|
+
(0, import_common_helpers12.invariant)(node.from.froms.length === 1, "Delete query must have exactly one from table");
|
|
8549
8807
|
const { node: tableNode } = stripAlias(node.from.froms[0]);
|
|
8550
|
-
(0,
|
|
8808
|
+
(0, import_common_helpers12.invariant)(import_kysely9.TableNode.is(tableNode), "DeleteQueryNode must use a TableNode");
|
|
8551
8809
|
return tableNode.table.identifier.name;
|
|
8552
8810
|
}).otherwise((node) => {
|
|
8553
8811
|
throw createInternalError(`Invalid query node: ${node}`);
|
|
@@ -8692,7 +8950,7 @@ __export(functions_exports, {
|
|
|
8692
8950
|
search: () => search,
|
|
8693
8951
|
startsWith: () => startsWith
|
|
8694
8952
|
});
|
|
8695
|
-
var
|
|
8953
|
+
var import_common_helpers13 = require("@zenstackhq/common-helpers");
|
|
8696
8954
|
var import_kysely10 = require("kysely");
|
|
8697
8955
|
var import_ts_pattern13 = require("ts-pattern");
|
|
8698
8956
|
var contains = /* @__PURE__ */ __name((eb, args, context) => textMatch(eb, args, context, "contains"), "contains");
|
|
@@ -8802,8 +9060,8 @@ var currentOperation = /* @__PURE__ */ __name((_eb, args, { operation }) => {
|
|
|
8802
9060
|
}, "currentOperation");
|
|
8803
9061
|
function processCasing(casing, result, model) {
|
|
8804
9062
|
const opNode = casing.toOperationNode();
|
|
8805
|
-
(0,
|
|
8806
|
-
result = (0, import_ts_pattern13.match)(opNode.value).with("original", () => model).with("upper", () => result.toUpperCase()).with("lower", () => result.toLowerCase()).with("capitalize", () => (0,
|
|
9063
|
+
(0, import_common_helpers13.invariant)(import_kysely10.ValueNode.is(opNode) && typeof opNode.value === "string", '"casting" parameter must be a string value');
|
|
9064
|
+
result = (0, import_ts_pattern13.match)(opNode.value).with("original", () => model).with("upper", () => result.toUpperCase()).with("lower", () => result.toLowerCase()).with("capitalize", () => (0, import_common_helpers13.upperCaseFirst)(result)).with("uncapitalize", () => (0, import_common_helpers13.lowerCaseFirst)(result)).otherwise(() => {
|
|
8807
9065
|
throw new Error(`Invalid casing value: ${opNode.value}. Must be "original", "upper", "lower", "capitalize", or "uncapitalize".`);
|
|
8808
9066
|
});
|
|
8809
9067
|
return result;
|
|
@@ -8814,13 +9072,13 @@ function readBoolean(expr, defaultValue) {
|
|
|
8814
9072
|
return defaultValue;
|
|
8815
9073
|
}
|
|
8816
9074
|
const opNode = expr.toOperationNode();
|
|
8817
|
-
(0,
|
|
9075
|
+
(0, import_common_helpers13.invariant)(import_kysely10.ValueNode.is(opNode), "expression must be a literal value");
|
|
8818
9076
|
return !!opNode.value;
|
|
8819
9077
|
}
|
|
8820
9078
|
__name(readBoolean, "readBoolean");
|
|
8821
9079
|
|
|
8822
9080
|
// src/client/helpers/schema-db-pusher.ts
|
|
8823
|
-
var
|
|
9081
|
+
var import_common_helpers14 = require("@zenstackhq/common-helpers");
|
|
8824
9082
|
var import_kysely11 = require("kysely");
|
|
8825
9083
|
var import_toposort = __toESM(require("toposort"), 1);
|
|
8826
9084
|
var import_ts_pattern14 = require("ts-pattern");
|
|
@@ -8846,7 +9104,7 @@ var SchemaDbPusher = class {
|
|
|
8846
9104
|
return f.name;
|
|
8847
9105
|
} else {
|
|
8848
9106
|
const mappedName = schema_exports.ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
|
|
8849
|
-
(0,
|
|
9107
|
+
(0, import_common_helpers14.invariant)(mappedName && typeof mappedName === "string", `Invalid @map attribute for enum field ${f.name}`);
|
|
8850
9108
|
return mappedName;
|
|
8851
9109
|
}
|
|
8852
9110
|
});
|
|
@@ -8973,7 +9231,7 @@ var SchemaDbPusher = class {
|
|
|
8973
9231
|
}
|
|
8974
9232
|
addUniqueConstraint(table, modelDef) {
|
|
8975
9233
|
for (const [key, value] of Object.entries(modelDef.uniqueFields)) {
|
|
8976
|
-
(0,
|
|
9234
|
+
(0, import_common_helpers14.invariant)(typeof value === "object", "expecting an object");
|
|
8977
9235
|
if ("type" in value) {
|
|
8978
9236
|
const fieldDef = modelDef.fields[key];
|
|
8979
9237
|
if (fieldDef.unique) {
|
|
@@ -9048,7 +9306,7 @@ var SchemaDbPusher = class {
|
|
|
9048
9306
|
return f.name;
|
|
9049
9307
|
} else {
|
|
9050
9308
|
const mappedName = schema_exports.ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
|
|
9051
|
-
(0,
|
|
9309
|
+
(0, import_common_helpers14.invariant)(mappedName && typeof mappedName === "string", `Invalid @map attribute for enum field ${f.name}`);
|
|
9052
9310
|
return mappedName;
|
|
9053
9311
|
}
|
|
9054
9312
|
});
|
|
@@ -9083,7 +9341,7 @@ var SchemaDbPusher = class {
|
|
|
9083
9341
|
return fieldDef.default && schema_exports.ExpressionUtils.isCall(fieldDef.default) && fieldDef.default.function === "autoincrement";
|
|
9084
9342
|
}
|
|
9085
9343
|
addForeignKeyConstraint(table, model, fieldName, fieldDef) {
|
|
9086
|
-
(0,
|
|
9344
|
+
(0, import_common_helpers14.invariant)(fieldDef.relation, "field must be a relation");
|
|
9087
9345
|
if (!fieldDef.relation.fields || !fieldDef.relation.references) {
|
|
9088
9346
|
return table;
|
|
9089
9347
|
}
|
|
@@ -9411,7 +9669,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
9411
9669
|
for (const [modelName, modelDef] of Object.entries(this.$schema.models)) {
|
|
9412
9670
|
if (modelDef.computedFields) {
|
|
9413
9671
|
for (const fieldName of Object.keys(modelDef.computedFields)) {
|
|
9414
|
-
const modelConfig = computedFieldsConfig?.[(0,
|
|
9672
|
+
const modelConfig = computedFieldsConfig?.[(0, import_common_helpers15.lowerCaseFirst)(modelName)] ?? computedFieldsConfig?.[modelName];
|
|
9415
9673
|
const fieldConfig = modelConfig?.[fieldName];
|
|
9416
9674
|
if (fieldConfig === null || fieldConfig === void 0) {
|
|
9417
9675
|
throw createConfigError(`Computed field "${fieldName}" in model "${modelName}" does not have a configuration. Please provide an implementation in the computedFields option.`);
|
|
@@ -9443,7 +9701,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
9443
9701
|
}
|
|
9444
9702
|
// implementation
|
|
9445
9703
|
async $transaction(input, options) {
|
|
9446
|
-
(0,
|
|
9704
|
+
(0, import_common_helpers15.invariant)(typeof input === "function" || Array.isArray(input) && input.every((p) => p.then && p.cb), "Invalid transaction input, expected a function or an array of ZenStackPromise");
|
|
9447
9705
|
if (typeof input === "function") {
|
|
9448
9706
|
return this.interactiveTransaction(input, options);
|
|
9449
9707
|
} else {
|
|
@@ -9921,7 +10179,7 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
|
|
|
9921
10179
|
};
|
|
9922
10180
|
const slicing = client.$options.slicing;
|
|
9923
10181
|
if (slicing?.models) {
|
|
9924
|
-
const modelSlicing = slicing.models[(0,
|
|
10182
|
+
const modelSlicing = slicing.models[(0, import_common_helpers15.lowerCaseFirst)(model)];
|
|
9925
10183
|
const allSlicing = slicing.models.$all;
|
|
9926
10184
|
const includedOperations = modelSlicing?.includedOperations ?? allSlicing?.includedOperations;
|
|
9927
10185
|
const excludedOperations = modelSlicing?.excludedOperations ?? allSlicing?.excludedOperations;
|
|
@@ -9972,7 +10230,7 @@ function collectExtResultFieldDefs(model, schema, plugins) {
|
|
|
9972
10230
|
for (const plugin of plugins) {
|
|
9973
10231
|
const resultConfig = plugin.result;
|
|
9974
10232
|
if (resultConfig) {
|
|
9975
|
-
const modelConfig = resultConfig[(0,
|
|
10233
|
+
const modelConfig = resultConfig[(0, import_common_helpers15.lowerCaseFirst)(model)];
|
|
9976
10234
|
if (modelConfig) {
|
|
9977
10235
|
for (const [fieldName, fieldDef] of Object.entries(modelConfig)) {
|
|
9978
10236
|
if (getField(schema, model, fieldName)) {
|