@zenstackhq/orm 3.9.0 → 3.9.2
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 +82 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -12
- package/dist/index.d.mts +56 -14
- package/dist/index.mjs +280 -232
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
|
|
|
2
2
|
import { AnyNull, AnyNullClass, DbNull, DbNullClass, JsonNull, JsonNullClass } from "./common-types.mjs";
|
|
3
3
|
import { clone, enumerate, invariant, isPlainObject, lowerCaseFirst, upperCaseFirst } from "@zenstackhq/common-helpers";
|
|
4
4
|
import { AliasNode, AndNode, ColumnNode, ColumnUpdateNode, CompiledQuery, DefaultConnectionProvider, DefaultQueryExecutor, DeleteQueryNode, ExpressionWrapper, FromNode, IdentifierNode, InsertQueryNode, Kysely, Log, OperationNodeTransformer, OperationNodeVisitor, PrimitiveValueListNode, ReferenceNode, ReturningNode, SelectAllNode, SelectionNode, SingleConnectionProvider, TableNode, Transaction, UpdateQueryNode, ValueListNode, ValueNode, ValuesNode, WhereNode, createQueryId, expressionBuilder, sql } from "kysely";
|
|
5
|
-
import z, { ZodObject, ZodType, z
|
|
5
|
+
import z$1, { ZodObject, ZodType, z } from "zod";
|
|
6
6
|
import { fromError } from "zod-validation-error/v4";
|
|
7
7
|
import { P, match } from "ts-pattern";
|
|
8
8
|
import { ExpressionUtils } from "@zenstackhq/schema";
|
|
@@ -215,6 +215,7 @@ var query_utils_exports = /* @__PURE__ */ __exportAll({
|
|
|
215
215
|
requireIdFields: () => requireIdFields,
|
|
216
216
|
requireModel: () => requireModel,
|
|
217
217
|
requireTypeDef: () => requireTypeDef,
|
|
218
|
+
schemaHasMappedNames: () => schemaHasMappedNames,
|
|
218
219
|
stripAlias: () => stripAlias,
|
|
219
220
|
tmpAlias: () => tmpAlias
|
|
220
221
|
});
|
|
@@ -244,6 +245,22 @@ function getModel(schema, model) {
|
|
|
244
245
|
function getTypeDef(schema, type) {
|
|
245
246
|
return schema.typeDefs?.[type];
|
|
246
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* Whether any model, type def, or field in the schema carries `@@map`/`@map`. Answering it walks
|
|
250
|
+
* every model and field, and it is asked once per query-executor construction, so the (immutable)
|
|
251
|
+
* answer is memoized per schema alongside the other structural lookups. See issue #2773.
|
|
252
|
+
*/
|
|
253
|
+
function schemaHasMappedNames(schema) {
|
|
254
|
+
const cache = getSchemaLookupCache(schema);
|
|
255
|
+
if (cache.hasMappedNames === void 0) {
|
|
256
|
+
const hasMapAttr = (decl) => {
|
|
257
|
+
if (decl.attributes?.some((attr) => attr.name === "@@map")) return true;
|
|
258
|
+
return Object.values(decl.fields ?? {}).some((field) => field.attributes?.some((attr) => attr.name === "@map"));
|
|
259
|
+
};
|
|
260
|
+
cache.hasMappedNames = Object.values(schema.models).some(hasMapAttr) || Object.values(schema.typeDefs ?? {}).some(hasMapAttr) || Object.values(schema.enums ?? {}).some(hasMapAttr);
|
|
261
|
+
}
|
|
262
|
+
return cache.hasMappedNames;
|
|
263
|
+
}
|
|
247
264
|
function requireModel(schema, model) {
|
|
248
265
|
const modelDef = getModel(schema, model);
|
|
249
266
|
if (!modelDef) throw createInternalError(`Model "${model}" not found in schema`, model);
|
|
@@ -635,9 +652,23 @@ const CRUD_EXT = [...CRUD, "post-update"];
|
|
|
635
652
|
//#region src/client/crud/dialects/base-dialect.ts
|
|
636
653
|
var BaseCrudDialect = class {
|
|
637
654
|
eb = expressionBuilder();
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
655
|
+
schema;
|
|
656
|
+
options;
|
|
657
|
+
/**
|
|
658
|
+
* The client executing the query. Unset only when the dialect was constructed from a
|
|
659
|
+
* standalone schema/options pair, in which case it cannot evaluate computed fields.
|
|
660
|
+
*/
|
|
661
|
+
client;
|
|
662
|
+
constructor(...args) {
|
|
663
|
+
if (args.length === 1) {
|
|
664
|
+
const [client] = args;
|
|
665
|
+
this.client = client;
|
|
666
|
+
this.schema = client.$schema;
|
|
667
|
+
this.options = client.$options;
|
|
668
|
+
} else {
|
|
669
|
+
[this.schema, this.options] = args;
|
|
670
|
+
this.client = void 0;
|
|
671
|
+
}
|
|
641
672
|
}
|
|
642
673
|
/**
|
|
643
674
|
* Transforms input value before sending to database.
|
|
@@ -1355,7 +1386,11 @@ var BaseCrudDialect = class {
|
|
|
1355
1386
|
computer = computedFields?.[lowerCaseFirst(computedModel)]?.[field] ?? computedFields?.[computedModel]?.[field];
|
|
1356
1387
|
}
|
|
1357
1388
|
if (!computer) throw createConfigError(`Computed field "${field}" implementation not provided for model "${model}"`);
|
|
1358
|
-
|
|
1389
|
+
invariant(this.client, `computed field "${field}" of model "${model}" needs a client to be evaluated`);
|
|
1390
|
+
return this.eb.parens(computer(this.eb, {
|
|
1391
|
+
modelAlias,
|
|
1392
|
+
client: this.client
|
|
1393
|
+
}, computedArgs));
|
|
1359
1394
|
}
|
|
1360
1395
|
}
|
|
1361
1396
|
canJoinWithoutNestedSelect(modelDef, payload) {
|
|
@@ -1524,9 +1559,6 @@ var LateralJoinDialectBase = class extends BaseCrudDialect {
|
|
|
1524
1559
|
//#endregion
|
|
1525
1560
|
//#region src/client/crud/dialects/mysql.ts
|
|
1526
1561
|
var MySqlCrudDialect = class extends LateralJoinDialectBase {
|
|
1527
|
-
constructor(schema, options) {
|
|
1528
|
-
super(schema, options);
|
|
1529
|
-
}
|
|
1530
1562
|
get provider() {
|
|
1531
1563
|
return "mysql";
|
|
1532
1564
|
}
|
|
@@ -1754,8 +1786,8 @@ var PostgresCrudDialect = class PostgresCrudDialect extends LateralJoinDialectBa
|
|
|
1754
1786
|
"@db.Decimal": "decimal",
|
|
1755
1787
|
"@db.Boolean": "boolean"
|
|
1756
1788
|
};
|
|
1757
|
-
constructor(
|
|
1758
|
-
super(
|
|
1789
|
+
constructor(...args) {
|
|
1790
|
+
super(...args);
|
|
1759
1791
|
this.overrideTypeParsers();
|
|
1760
1792
|
}
|
|
1761
1793
|
get provider() {
|
|
@@ -2304,8 +2336,8 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
2304
2336
|
};
|
|
2305
2337
|
//#endregion
|
|
2306
2338
|
//#region src/client/crud/dialects/index.ts
|
|
2307
|
-
function getCrudDialect(
|
|
2308
|
-
return match(schema.provider.type).with("sqlite", () => new SqliteCrudDialect(
|
|
2339
|
+
function getCrudDialect(...args) {
|
|
2340
|
+
return match((args.length === 1 ? args[0].$schema : args[0]).provider.type).with("sqlite", () => new SqliteCrudDialect(...args)).with("postgresql", () => new PostgresCrudDialect(...args)).with("mysql", () => new MySqlCrudDialect(...args)).exhaustive();
|
|
2309
2341
|
}
|
|
2310
2342
|
//#endregion
|
|
2311
2343
|
//#region src/client/zod/cache-decorator.ts
|
|
@@ -2367,7 +2399,7 @@ function createQuerySchemaFactory(clientOrSchema, options) {
|
|
|
2367
2399
|
* @see https://github.com/zenstackhq/zenstack/issues/2631
|
|
2368
2400
|
*/
|
|
2369
2401
|
function coercedDateTimeSchema() {
|
|
2370
|
-
return z
|
|
2402
|
+
return z.preprocess((val) => {
|
|
2371
2403
|
if (typeof val !== "string") return val;
|
|
2372
2404
|
if (/^\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d\d(?::\d\d)?)?$/.test(val)) {
|
|
2373
2405
|
const hasTz = val.endsWith("Z") || /[+-]\d\d(?::\d\d)?$/.test(val);
|
|
@@ -2376,10 +2408,10 @@ function coercedDateTimeSchema() {
|
|
|
2376
2408
|
}
|
|
2377
2409
|
const d = new Date(val);
|
|
2378
2410
|
return isNaN(d.getTime()) ? val : d;
|
|
2379
|
-
}, z
|
|
2380
|
-
z
|
|
2381
|
-
z
|
|
2382
|
-
z
|
|
2411
|
+
}, z.union([
|
|
2412
|
+
z.iso.datetime(),
|
|
2413
|
+
z.iso.date(),
|
|
2414
|
+
z.date()
|
|
2383
2415
|
]));
|
|
2384
2416
|
}
|
|
2385
2417
|
/**
|
|
@@ -2387,7 +2419,7 @@ function coercedDateTimeSchema() {
|
|
|
2387
2419
|
*/
|
|
2388
2420
|
var ZodSchemaFactory = class {
|
|
2389
2421
|
schemaCache = /* @__PURE__ */ new Map();
|
|
2390
|
-
schemaRegistry = z
|
|
2422
|
+
schemaRegistry = z.registry();
|
|
2391
2423
|
allFilterKinds = [...new Set(Object.values(FILTER_PROPERTY_TO_KIND))];
|
|
2392
2424
|
schema;
|
|
2393
2425
|
options;
|
|
@@ -2479,7 +2511,7 @@ var ZodSchemaFactory = class {
|
|
|
2479
2511
|
this.makeGroupBySchema(m);
|
|
2480
2512
|
}
|
|
2481
2513
|
for (const procName of Object.keys(this.schema.procedures ?? {})) if (this.isProcedureAllowed(procName)) this.makeProcedureArgsSchema(procName);
|
|
2482
|
-
return z
|
|
2514
|
+
return z.toJSONSchema(this.schemaRegistry, { unrepresentable: "any" });
|
|
2483
2515
|
}
|
|
2484
2516
|
get cacheStats() {
|
|
2485
2517
|
return {
|
|
@@ -2508,13 +2540,13 @@ var ZodSchemaFactory = class {
|
|
|
2508
2540
|
fields["omit"] = this.makeOmitSchema(model).optional().nullable();
|
|
2509
2541
|
if (!unique) {
|
|
2510
2542
|
fields["skip"] = this.makeSkipSchema().optional();
|
|
2511
|
-
if (findOne) fields["take"] = z
|
|
2543
|
+
if (findOne) fields["take"] = z.literal(1).optional();
|
|
2512
2544
|
else fields["take"] = this.makeTakeSchema().optional();
|
|
2513
2545
|
fields["orderBy"] = this.orArray(this.makeOrderBySchema(model, true, false, options), true).optional();
|
|
2514
2546
|
fields["cursor"] = this.makeCursorSchema(model, options).optional();
|
|
2515
2547
|
fields["distinct"] = this.makeDistinctSchema(model).optional();
|
|
2516
2548
|
}
|
|
2517
|
-
const baseSchema = z
|
|
2549
|
+
const baseSchema = z.strictObject(fields);
|
|
2518
2550
|
let result = this.mergePluginArgsSchema(baseSchema, operation);
|
|
2519
2551
|
result = this.refineForSelectIncludeMutuallyExclusive(result);
|
|
2520
2552
|
result = this.refineForSelectOmitMutuallyExclusive(result);
|
|
@@ -2524,7 +2556,7 @@ var ZodSchemaFactory = class {
|
|
|
2524
2556
|
return result;
|
|
2525
2557
|
}
|
|
2526
2558
|
makeExistsSchema(model, options) {
|
|
2527
|
-
const baseSchema = z
|
|
2559
|
+
const baseSchema = z.strictObject({ where: this.makeWhereSchema(model, false, false, false, options).optional() });
|
|
2528
2560
|
const result = this.mergePluginArgsSchema(baseSchema, "exists").optional();
|
|
2529
2561
|
this.registerSchema(`${model}ExistsArgs`, result);
|
|
2530
2562
|
return result;
|
|
@@ -2532,19 +2564,19 @@ var ZodSchemaFactory = class {
|
|
|
2532
2564
|
makeScalarSchema(type, attributes) {
|
|
2533
2565
|
if (this.schema.typeDefs && type in this.schema.typeDefs) return this.makeTypeDefSchema(type);
|
|
2534
2566
|
else if (this.schema.enums && type in this.schema.enums) return this.makeEnumSchema(type);
|
|
2535
|
-
else return match(type).with("String", () => this.extraValidationsEnabled ? ZodUtils.addStringValidation(z
|
|
2536
|
-
return z
|
|
2537
|
-
this.extraValidationsEnabled ? ZodUtils.addNumberValidation(z
|
|
2538
|
-
ZodUtils.addDecimalValidation(z
|
|
2539
|
-
ZodUtils.addDecimalValidation(z
|
|
2567
|
+
else return match(type).with("String", () => this.extraValidationsEnabled ? ZodUtils.addStringValidation(z.string(), attributes) : z.string()).with("Int", () => this.extraValidationsEnabled ? ZodUtils.addNumberValidation(z.number().int(), attributes) : z.number().int()).with("Float", () => this.extraValidationsEnabled ? ZodUtils.addNumberValidation(z.number(), attributes) : z.number()).with("Boolean", () => z.boolean()).with("BigInt", () => z.union([this.extraValidationsEnabled ? ZodUtils.addNumberValidation(z.number().int(), attributes) : z.number().int(), this.extraValidationsEnabled ? ZodUtils.addBigIntValidation(z.bigint(), attributes) : z.bigint()])).with("Decimal", () => {
|
|
2568
|
+
return z.union([
|
|
2569
|
+
this.extraValidationsEnabled ? ZodUtils.addNumberValidation(z.number(), attributes) : z.number(),
|
|
2570
|
+
ZodUtils.addDecimalValidation(z.instanceof(Decimal), attributes, this.extraValidationsEnabled),
|
|
2571
|
+
ZodUtils.addDecimalValidation(z.string(), attributes, this.extraValidationsEnabled)
|
|
2540
2572
|
]);
|
|
2541
|
-
}).with("DateTime", () => this.makeDateTimeValueSchema()).with("Bytes", () => z
|
|
2573
|
+
}).with("DateTime", () => this.makeDateTimeValueSchema()).with("Bytes", () => z.instanceof(Uint8Array)).with("Json", () => this.makeJsonValueSchema()).otherwise(() => z.unknown());
|
|
2542
2574
|
}
|
|
2543
2575
|
isParameterizedComputedField(fieldDef) {
|
|
2544
2576
|
return !!(fieldDef.computed && fieldDef.params);
|
|
2545
2577
|
}
|
|
2546
2578
|
makeFieldArgsSchema(params) {
|
|
2547
|
-
return z
|
|
2579
|
+
return z.strictObject(Object.fromEntries(Object.entries(params).map(([name, param]) => {
|
|
2548
2580
|
let paramSchema = this.makeScalarSchema(param.type);
|
|
2549
2581
|
if (param.array) paramSchema = paramSchema.array();
|
|
2550
2582
|
if (param.optional) paramSchema = paramSchema.optional();
|
|
@@ -2552,9 +2584,9 @@ var ZodSchemaFactory = class {
|
|
|
2552
2584
|
})));
|
|
2553
2585
|
}
|
|
2554
2586
|
addComputedArgsToFilter(filterSchema, params) {
|
|
2555
|
-
const objectMembers = (filterSchema instanceof z
|
|
2587
|
+
const objectMembers = (filterSchema instanceof z.ZodUnion ? filterSchema.options : [filterSchema]).filter((m) => m instanceof z.ZodObject);
|
|
2556
2588
|
const components = Object.assign({}, ...objectMembers.map((o) => o.shape));
|
|
2557
|
-
return z
|
|
2589
|
+
return z.strictObject({
|
|
2558
2590
|
args: this.makeFieldArgsSchema(params),
|
|
2559
2591
|
...components
|
|
2560
2592
|
});
|
|
@@ -2562,20 +2594,20 @@ var ZodSchemaFactory = class {
|
|
|
2562
2594
|
makeEnumSchema(_enum) {
|
|
2563
2595
|
const enumDef = getEnum(this.schema, _enum);
|
|
2564
2596
|
invariant(enumDef, `Enum "${_enum}" not found in schema`);
|
|
2565
|
-
const schema = z
|
|
2597
|
+
const schema = z.enum(Object.keys(enumDef.values));
|
|
2566
2598
|
this.registerSchema(_enum, schema);
|
|
2567
2599
|
return schema;
|
|
2568
2600
|
}
|
|
2569
2601
|
makeTypeDefSchema(type) {
|
|
2570
2602
|
const typeDef = getTypeDef(this.schema, type);
|
|
2571
2603
|
invariant(typeDef, `Type definition "${type}" not found in schema`);
|
|
2572
|
-
const schema = z
|
|
2573
|
-
let fieldSchema = isTypeDef(this.schema, def.type) ? z
|
|
2604
|
+
const schema = (typeDef.strict ? z.strictObject : z.looseObject)(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
|
|
2605
|
+
let fieldSchema = isTypeDef(this.schema, def.type) ? z.lazy(() => this.makeTypeDefSchema(def.type)) : this.makeScalarSchema(def.type);
|
|
2574
2606
|
if (def.array) fieldSchema = fieldSchema.array();
|
|
2575
2607
|
if (def.optional) fieldSchema = fieldSchema.nullish();
|
|
2576
2608
|
return [field, fieldSchema];
|
|
2577
2609
|
})));
|
|
2578
|
-
const finalSchema = z
|
|
2610
|
+
const finalSchema = z.any().superRefine((value, ctx) => {
|
|
2579
2611
|
const parseResult = schema.safeParse(value);
|
|
2580
2612
|
if (!parseResult.success) parseResult.error.issues.forEach((issue) => ctx.addIssue(issue));
|
|
2581
2613
|
});
|
|
@@ -2591,17 +2623,17 @@ var ZodSchemaFactory = class {
|
|
|
2591
2623
|
if (fieldDef.relation) {
|
|
2592
2624
|
if (withoutRelationFields || !this.shouldIncludeRelations(options)) continue;
|
|
2593
2625
|
const allowedFilterKinds = this.getEffectiveFilterKinds(model, field);
|
|
2594
|
-
if (allowedFilterKinds && !allowedFilterKinds.includes("Relation")) fieldSchema = z
|
|
2626
|
+
if (allowedFilterKinds && !allowedFilterKinds.includes("Relation")) fieldSchema = z.never();
|
|
2595
2627
|
else {
|
|
2596
|
-
fieldSchema = z
|
|
2597
|
-
if (fieldDef.array) fieldSchema = z
|
|
2628
|
+
fieldSchema = z.lazy(() => this.makeWhereSchema(fieldDef.type, false, false, false, nextOpts).optional());
|
|
2629
|
+
if (fieldDef.array) fieldSchema = z.strictObject({
|
|
2598
2630
|
some: fieldSchema.optional(),
|
|
2599
2631
|
every: fieldSchema.optional(),
|
|
2600
2632
|
none: fieldSchema.optional()
|
|
2601
2633
|
});
|
|
2602
2634
|
else {
|
|
2603
2635
|
fieldSchema = this.nullableIf(fieldSchema, !fieldDef.array && !!fieldDef.optional);
|
|
2604
|
-
fieldSchema = z
|
|
2636
|
+
fieldSchema = z.union([fieldSchema, z.strictObject({
|
|
2605
2637
|
is: fieldSchema.optional(),
|
|
2606
2638
|
isNot: fieldSchema.optional()
|
|
2607
2639
|
})]);
|
|
@@ -2621,21 +2653,21 @@ var ZodSchemaFactory = class {
|
|
|
2621
2653
|
}
|
|
2622
2654
|
if (unique) {
|
|
2623
2655
|
const uniqueFields = getUniqueFields(this.schema, model);
|
|
2624
|
-
for (const uniqueField of uniqueFields) if ("defs" in uniqueField) fields[uniqueField.name] = z
|
|
2656
|
+
for (const uniqueField of uniqueFields) if ("defs" in uniqueField) fields[uniqueField.name] = z.object(Object.fromEntries(Object.entries(uniqueField.defs).map(([key, def]) => {
|
|
2625
2657
|
invariant(!def.relation, "unique field cannot be a relation");
|
|
2626
2658
|
let fieldSchema;
|
|
2627
2659
|
const enumDef = getEnum(this.schema, def.type);
|
|
2628
2660
|
if (enumDef) if (Object.keys(enumDef.values).length > 0) fieldSchema = this.makeEnumFilterSchema(def.type, !!def.optional, !!def.array, false, void 0);
|
|
2629
|
-
else fieldSchema = z
|
|
2661
|
+
else fieldSchema = z.never();
|
|
2630
2662
|
else fieldSchema = this.makePrimitiveFilterSchema(def.type, !!def.optional, false, void 0);
|
|
2631
2663
|
return [key, fieldSchema];
|
|
2632
2664
|
}))).optional();
|
|
2633
2665
|
}
|
|
2634
|
-
fields["$expr"] = z
|
|
2635
|
-
fields["AND"] = this.orArray(z
|
|
2636
|
-
fields["OR"] = z
|
|
2637
|
-
fields["NOT"] = this.orArray(z
|
|
2638
|
-
const baseWhere = z
|
|
2666
|
+
fields["$expr"] = z.custom((v) => typeof v === "function", { error: "\"$expr\" must be a function" }).optional();
|
|
2667
|
+
fields["AND"] = this.orArray(z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields, false, options)), true).optional();
|
|
2668
|
+
fields["OR"] = z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields, false, options)).array().optional();
|
|
2669
|
+
fields["NOT"] = this.orArray(z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields, false, options)), true).optional();
|
|
2670
|
+
const baseWhere = z.strictObject(fields);
|
|
2639
2671
|
let result = baseWhere;
|
|
2640
2672
|
if (unique) {
|
|
2641
2673
|
const uniqueFields = getUniqueFields(this.schema, model);
|
|
@@ -2661,26 +2693,26 @@ var ZodSchemaFactory = class {
|
|
|
2661
2693
|
else if (getEnum(this.schema, fieldDef.type)) fieldSchemas[fieldName] = this.makeEnumFilterSchema(fieldDef.type, !!fieldDef.optional, !!fieldDef.array, false, void 0).optional();
|
|
2662
2694
|
else if (fieldDef.array) fieldSchemas[fieldName] = this.makeArrayFilterSchema(fieldDef.type, void 0).optional();
|
|
2663
2695
|
else fieldSchemas[fieldName] = this.makePrimitiveFilterSchema(fieldDef.type, !!fieldDef.optional, false, void 0).optional();
|
|
2664
|
-
candidates.push(z
|
|
2696
|
+
candidates.push(z.strictObject(fieldSchemas));
|
|
2665
2697
|
}
|
|
2666
|
-
const recursiveSchema = z
|
|
2667
|
-
if (array) candidates.push(z
|
|
2698
|
+
const recursiveSchema = z.lazy(() => this.makeTypedJsonFilterSchema(type, optional, false, allowedFilterKinds)).optional();
|
|
2699
|
+
if (array) candidates.push(z.strictObject({
|
|
2668
2700
|
some: recursiveSchema,
|
|
2669
2701
|
every: recursiveSchema,
|
|
2670
2702
|
none: recursiveSchema
|
|
2671
2703
|
}));
|
|
2672
|
-
else candidates.push(z
|
|
2704
|
+
else candidates.push(z.strictObject({
|
|
2673
2705
|
is: recursiveSchema,
|
|
2674
2706
|
isNot: recursiveSchema
|
|
2675
2707
|
}));
|
|
2676
2708
|
candidates.push(this.makeJsonFilterSchema(optional, allowedFilterKinds));
|
|
2677
2709
|
if (optional) {
|
|
2678
|
-
candidates.push(z
|
|
2679
|
-
candidates.push(z
|
|
2680
|
-
candidates.push(z
|
|
2681
|
-
candidates.push(z
|
|
2710
|
+
candidates.push(z.null());
|
|
2711
|
+
candidates.push(z.instanceof(DbNullClass));
|
|
2712
|
+
candidates.push(z.instanceof(JsonNullClass));
|
|
2713
|
+
candidates.push(z.instanceof(AnyNullClass));
|
|
2682
2714
|
}
|
|
2683
|
-
const result = z
|
|
2715
|
+
const result = z.union(candidates);
|
|
2684
2716
|
this.registerSchema(`${type}Filter${this.filterSchemaSuffix({
|
|
2685
2717
|
optional,
|
|
2686
2718
|
array,
|
|
@@ -2689,7 +2721,7 @@ var ZodSchemaFactory = class {
|
|
|
2689
2721
|
return result;
|
|
2690
2722
|
}
|
|
2691
2723
|
makeNullableTypedJsonMutationSchema(fieldSchema) {
|
|
2692
|
-
return z
|
|
2724
|
+
return z.any().superRefine((value, ctx) => {
|
|
2693
2725
|
if (value instanceof DbNullClass || value instanceof JsonNullClass || value === null || value === void 0) return;
|
|
2694
2726
|
const parseResult = fieldSchema.safeParse(value);
|
|
2695
2727
|
if (!parseResult.success) parseResult.error.issues.forEach((issue) => ctx.addIssue(issue));
|
|
@@ -2701,11 +2733,11 @@ var ZodSchemaFactory = class {
|
|
|
2701
2733
|
makeEnumFilterSchema(enumName, optional, array, withAggregations, allowedFilterKinds) {
|
|
2702
2734
|
const enumDef = getEnum(this.schema, enumName);
|
|
2703
2735
|
invariant(enumDef, `Enum "${enumName}" not found in schema`);
|
|
2704
|
-
const baseSchema = z
|
|
2736
|
+
const baseSchema = z.enum(Object.keys(enumDef.values));
|
|
2705
2737
|
let schema;
|
|
2706
2738
|
if (array) schema = this.internalMakeArrayFilterSchema(baseSchema, allowedFilterKinds);
|
|
2707
2739
|
else {
|
|
2708
|
-
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z
|
|
2740
|
+
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z.lazy(() => this.makeEnumFilterSchema(enumName, optional, array, withAggregations, allowedFilterKinds)), [
|
|
2709
2741
|
"equals",
|
|
2710
2742
|
"in",
|
|
2711
2743
|
"notIn",
|
|
@@ -2739,42 +2771,42 @@ var ZodSchemaFactory = class {
|
|
|
2739
2771
|
has: elementSchema.optional(),
|
|
2740
2772
|
hasEvery: elementSchema.array().optional(),
|
|
2741
2773
|
hasSome: elementSchema.array().optional(),
|
|
2742
|
-
isEmpty: z
|
|
2774
|
+
isEmpty: z.boolean().optional()
|
|
2743
2775
|
};
|
|
2744
2776
|
const filteredOperators = this.trimFilterOperators(operators, allowedFilterKinds);
|
|
2745
|
-
return z
|
|
2777
|
+
return z.strictObject(filteredOperators);
|
|
2746
2778
|
}
|
|
2747
2779
|
makePrimitiveFilterSchema(type, optional, withAggregations, allowedFilterKinds, withFuzzy = false, withFullText = false) {
|
|
2748
|
-
return match(type).with("String", () => this.makeStringFilterSchema(optional, withAggregations, allowedFilterKinds, withFuzzy, withFullText)).with(P.union("Int", "Float", "Decimal", "BigInt"), (type) => this.makeNumberFilterSchema(type, optional, withAggregations, allowedFilterKinds)).with("Boolean", () => this.makeBooleanFilterSchema(optional, withAggregations, allowedFilterKinds)).with("DateTime", () => this.makeDateTimeFilterSchema(optional, withAggregations, allowedFilterKinds)).with("Bytes", () => this.makeBytesFilterSchema(optional, withAggregations, allowedFilterKinds)).with("Json", () => this.makeJsonFilterSchema(optional, allowedFilterKinds)).with("Unsupported", () => z
|
|
2780
|
+
return match(type).with("String", () => this.makeStringFilterSchema(optional, withAggregations, allowedFilterKinds, withFuzzy, withFullText)).with(P.union("Int", "Float", "Decimal", "BigInt"), (type) => this.makeNumberFilterSchema(type, optional, withAggregations, allowedFilterKinds)).with("Boolean", () => this.makeBooleanFilterSchema(optional, withAggregations, allowedFilterKinds)).with("DateTime", () => this.makeDateTimeFilterSchema(optional, withAggregations, allowedFilterKinds)).with("Bytes", () => this.makeBytesFilterSchema(optional, withAggregations, allowedFilterKinds)).with("Json", () => this.makeJsonFilterSchema(optional, allowedFilterKinds)).with("Unsupported", () => z.never()).exhaustive();
|
|
2749
2781
|
}
|
|
2750
2782
|
makeJsonValueSchema() {
|
|
2751
|
-
const schema = z
|
|
2752
|
-
z
|
|
2753
|
-
z
|
|
2754
|
-
z
|
|
2755
|
-
z
|
|
2756
|
-
z
|
|
2757
|
-
z
|
|
2783
|
+
const schema = z.union([
|
|
2784
|
+
z.string(),
|
|
2785
|
+
z.number(),
|
|
2786
|
+
z.boolean(),
|
|
2787
|
+
z.instanceof(JsonNullClass),
|
|
2788
|
+
z.lazy(() => z.union([this.makeJsonValueSchema(), z.null()]).array()),
|
|
2789
|
+
z.record(z.string(), z.lazy(() => z.union([this.makeJsonValueSchema(), z.null()])))
|
|
2758
2790
|
]);
|
|
2759
2791
|
this.registerSchema("JsonValue", schema);
|
|
2760
2792
|
return schema;
|
|
2761
2793
|
}
|
|
2762
2794
|
makeJsonFilterSchema(optional, allowedFilterKinds) {
|
|
2763
|
-
if (allowedFilterKinds && !allowedFilterKinds.includes("Json")) return z
|
|
2795
|
+
if (allowedFilterKinds && !allowedFilterKinds.includes("Json")) return z.never();
|
|
2764
2796
|
const filterMembers = [
|
|
2765
2797
|
this.makeJsonValueSchema(),
|
|
2766
|
-
z
|
|
2767
|
-
z
|
|
2798
|
+
z.instanceof(DbNullClass),
|
|
2799
|
+
z.instanceof(AnyNullClass)
|
|
2768
2800
|
];
|
|
2769
|
-
if (optional) filterMembers.push(z
|
|
2770
|
-
const filterValueSchema = z
|
|
2771
|
-
const schema = z
|
|
2772
|
-
path: z
|
|
2801
|
+
if (optional) filterMembers.push(z.null());
|
|
2802
|
+
const filterValueSchema = z.union(filterMembers);
|
|
2803
|
+
const schema = z.strictObject({
|
|
2804
|
+
path: z.string().optional(),
|
|
2773
2805
|
equals: filterValueSchema.optional(),
|
|
2774
2806
|
not: filterValueSchema.optional(),
|
|
2775
|
-
string_contains: z
|
|
2776
|
-
string_starts_with: z
|
|
2777
|
-
string_ends_with: z
|
|
2807
|
+
string_contains: z.string().optional(),
|
|
2808
|
+
string_starts_with: z.string().optional(),
|
|
2809
|
+
string_ends_with: z.string().optional(),
|
|
2778
2810
|
mode: this.makeStringModeSchema().optional(),
|
|
2779
2811
|
array_contains: filterValueSchema.optional(),
|
|
2780
2812
|
array_starts_with: filterValueSchema.optional(),
|
|
@@ -2793,7 +2825,7 @@ var ZodSchemaFactory = class {
|
|
|
2793
2825
|
}
|
|
2794
2826
|
makeDateTimeFilterSchema(optional, withAggregations, allowedFilterKinds) {
|
|
2795
2827
|
const filterValueSchema = this.makeDateTimeValueSchema();
|
|
2796
|
-
const schema = this.makeCommonPrimitiveFilterSchema(filterValueSchema, optional, () => z
|
|
2828
|
+
const schema = this.makeCommonPrimitiveFilterSchema(filterValueSchema, optional, () => z.lazy(() => this.makeDateTimeFilterSchema(optional, withAggregations, allowedFilterKinds)), withAggregations ? [
|
|
2797
2829
|
"_count",
|
|
2798
2830
|
"_min",
|
|
2799
2831
|
"_max"
|
|
@@ -2806,12 +2838,12 @@ var ZodSchemaFactory = class {
|
|
|
2806
2838
|
return schema;
|
|
2807
2839
|
}
|
|
2808
2840
|
makeBooleanFilterSchema(optional, withAggregations, allowedFilterKinds) {
|
|
2809
|
-
const components = this.makeCommonPrimitiveFilterComponents(z
|
|
2841
|
+
const components = this.makeCommonPrimitiveFilterComponents(z.boolean(), optional, () => z.lazy(() => this.makeBooleanFilterSchema(optional, withAggregations, allowedFilterKinds)), ["equals", "not"], withAggregations ? [
|
|
2810
2842
|
"_count",
|
|
2811
2843
|
"_min",
|
|
2812
2844
|
"_max"
|
|
2813
2845
|
] : void 0, allowedFilterKinds);
|
|
2814
|
-
const schema = this.createUnionFilterSchema(z
|
|
2846
|
+
const schema = this.createUnionFilterSchema(z.boolean(), optional, components, allowedFilterKinds);
|
|
2815
2847
|
this.registerSchema(`BooleanFilter${this.filterSchemaSuffix({
|
|
2816
2848
|
optional,
|
|
2817
2849
|
allowedFilterKinds,
|
|
@@ -2820,8 +2852,8 @@ var ZodSchemaFactory = class {
|
|
|
2820
2852
|
return schema;
|
|
2821
2853
|
}
|
|
2822
2854
|
makeBytesFilterSchema(optional, withAggregations, allowedFilterKinds) {
|
|
2823
|
-
const baseSchema = z
|
|
2824
|
-
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z
|
|
2855
|
+
const baseSchema = z.instanceof(Uint8Array);
|
|
2856
|
+
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z.instanceof(Uint8Array), [
|
|
2825
2857
|
"equals",
|
|
2826
2858
|
"in",
|
|
2827
2859
|
"notIn",
|
|
@@ -2869,7 +2901,7 @@ var ZodSchemaFactory = class {
|
|
|
2869
2901
|
return this.createUnionFilterSchema(baseSchema, optional, components, allowedFilterKinds);
|
|
2870
2902
|
}
|
|
2871
2903
|
makeNumberFilterSchema(type, optional, withAggregations, allowedFilterKinds) {
|
|
2872
|
-
const schema = this.makeCommonPrimitiveFilterSchema(this.makeScalarSchema(type), optional, () => z
|
|
2904
|
+
const schema = this.makeCommonPrimitiveFilterSchema(this.makeScalarSchema(type), optional, () => z.lazy(() => this.makeNumberFilterSchema(type, optional, withAggregations, allowedFilterKinds)), withAggregations ? [
|
|
2873
2905
|
"_count",
|
|
2874
2906
|
"_avg",
|
|
2875
2907
|
"_sum",
|
|
@@ -2884,15 +2916,15 @@ var ZodSchemaFactory = class {
|
|
|
2884
2916
|
return schema;
|
|
2885
2917
|
}
|
|
2886
2918
|
makeStringFilterSchema(optional, withAggregations, allowedFilterKinds, withFuzzy = false, withFullText = false) {
|
|
2887
|
-
const baseComponents = this.makeCommonPrimitiveFilterComponents(z
|
|
2919
|
+
const baseComponents = this.makeCommonPrimitiveFilterComponents(z.string(), optional, () => z.lazy(() => this.makeStringFilterSchema(optional, withAggregations, allowedFilterKinds, withFuzzy, withFullText)), void 0, withAggregations ? [
|
|
2888
2920
|
"_count",
|
|
2889
2921
|
"_min",
|
|
2890
2922
|
"_max"
|
|
2891
2923
|
] : void 0, allowedFilterKinds);
|
|
2892
2924
|
const stringSpecificOperators = {
|
|
2893
|
-
startsWith: z
|
|
2894
|
-
endsWith: z
|
|
2895
|
-
contains: z
|
|
2925
|
+
startsWith: z.string().optional(),
|
|
2926
|
+
endsWith: z.string().optional(),
|
|
2927
|
+
contains: z.string().optional(),
|
|
2896
2928
|
...withFuzzy && this.providerSupportsFuzzySearch ? { fuzzy: this.makeFuzzyFilterSchema().optional() } : {},
|
|
2897
2929
|
...withFullText && this.providerSupportsFullTextSearch ? { fts: this.makeFullTextFilterSchema().optional() } : {},
|
|
2898
2930
|
...this.providerSupportsCaseSensitivity ? { mode: this.makeStringModeSchema().optional() } : {}
|
|
@@ -2902,7 +2934,7 @@ var ZodSchemaFactory = class {
|
|
|
2902
2934
|
...baseComponents,
|
|
2903
2935
|
...filteredStringOperators
|
|
2904
2936
|
};
|
|
2905
|
-
const schema = this.createUnionFilterSchema(z
|
|
2937
|
+
const schema = this.createUnionFilterSchema(z.string(), optional, allComponents, allowedFilterKinds);
|
|
2906
2938
|
const featureSuffix = `${withFuzzy ? "Fuzzy" : ""}${withFullText ? "FullText" : ""}`;
|
|
2907
2939
|
this.registerSchema(`StringFilter${this.filterSchemaSuffix({
|
|
2908
2940
|
optional,
|
|
@@ -2912,45 +2944,45 @@ var ZodSchemaFactory = class {
|
|
|
2912
2944
|
return schema;
|
|
2913
2945
|
}
|
|
2914
2946
|
makeStringModeSchema() {
|
|
2915
|
-
return z
|
|
2947
|
+
return z.union([z.literal("default"), z.literal("insensitive")]);
|
|
2916
2948
|
}
|
|
2917
2949
|
makeFuzzyFilterSchema() {
|
|
2918
|
-
return z
|
|
2919
|
-
search: z
|
|
2920
|
-
mode: z
|
|
2921
|
-
z
|
|
2922
|
-
z
|
|
2923
|
-
z
|
|
2950
|
+
return z.strictObject({
|
|
2951
|
+
search: z.string().min(1),
|
|
2952
|
+
mode: z.union([
|
|
2953
|
+
z.literal("simple"),
|
|
2954
|
+
z.literal("word"),
|
|
2955
|
+
z.literal("strictWord")
|
|
2924
2956
|
]).default("simple"),
|
|
2925
|
-
threshold: z
|
|
2926
|
-
unaccent: z
|
|
2957
|
+
threshold: z.number().min(0).max(1).optional(),
|
|
2958
|
+
unaccent: z.boolean().default(false)
|
|
2927
2959
|
});
|
|
2928
2960
|
}
|
|
2929
2961
|
makeFullTextFilterSchema() {
|
|
2930
|
-
return z
|
|
2931
|
-
search: z
|
|
2932
|
-
config: z
|
|
2962
|
+
return z.strictObject({
|
|
2963
|
+
search: z.string().min(1),
|
|
2964
|
+
config: z.string().min(1).optional()
|
|
2933
2965
|
});
|
|
2934
2966
|
}
|
|
2935
2967
|
makeSelectSchema(model, options) {
|
|
2936
2968
|
const fields = {};
|
|
2937
2969
|
for (const [field, fieldDef] of this.getModelFields(model)) {
|
|
2938
2970
|
if (this.isParameterizedComputedField(fieldDef)) {
|
|
2939
|
-
fields[field] = z
|
|
2971
|
+
fields[field] = z.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional();
|
|
2940
2972
|
continue;
|
|
2941
2973
|
}
|
|
2942
2974
|
if (fieldDef.relation) {
|
|
2943
2975
|
if (!this.shouldIncludeRelations(options)) continue;
|
|
2944
2976
|
if (this.isModelAllowed(fieldDef.type)) fields[field] = this.makeRelationSelectIncludeSchema(model, field, options).optional();
|
|
2945
|
-
} else if (this.options.allowQueryTimeOmitOverride === false && this.isFieldOmittedByConfig(model, field)) fields[field] = z
|
|
2946
|
-
else fields[field] = z
|
|
2977
|
+
} else if (this.options.allowQueryTimeOmitOverride === false && this.isFieldOmittedByConfig(model, field)) fields[field] = z.literal(false).optional();
|
|
2978
|
+
else fields[field] = z.boolean().optional();
|
|
2947
2979
|
}
|
|
2948
2980
|
if (this.shouldIncludeRelations(options)) {
|
|
2949
2981
|
const _countSchema = this.makeCountSelectionSchema(model, options);
|
|
2950
|
-
if (!(_countSchema instanceof z
|
|
2982
|
+
if (!(_countSchema instanceof z.ZodNever)) fields["_count"] = _countSchema;
|
|
2951
2983
|
}
|
|
2952
2984
|
this.addExtResultFields(model, fields);
|
|
2953
|
-
const result = z
|
|
2985
|
+
const result = z.strictObject(fields);
|
|
2954
2986
|
this.registerSchema(`${model}Select`, result);
|
|
2955
2987
|
return result;
|
|
2956
2988
|
}
|
|
@@ -2959,24 +2991,24 @@ var ZodSchemaFactory = class {
|
|
|
2959
2991
|
const toManyRelations = Object.values(modelDef.fields).filter((def) => def.relation && def.array);
|
|
2960
2992
|
if (toManyRelations.length > 0) {
|
|
2961
2993
|
const nextOpts = this.nextOptions(options);
|
|
2962
|
-
const schema = z
|
|
2994
|
+
const schema = z.union([z.literal(true), z.strictObject({ select: z.strictObject(toManyRelations.reduce((acc, fieldDef) => ({
|
|
2963
2995
|
...acc,
|
|
2964
|
-
[fieldDef.name]: z
|
|
2996
|
+
[fieldDef.name]: z.union([z.boolean(), z.strictObject({ where: this.makeWhereSchema(fieldDef.type, false, false, false, nextOpts) })]).optional()
|
|
2965
2997
|
}), {})) })]).optional();
|
|
2966
2998
|
this.registerSchema(`${model}CountSelection`, schema);
|
|
2967
2999
|
return schema;
|
|
2968
|
-
} else return z
|
|
3000
|
+
} else return z.never();
|
|
2969
3001
|
}
|
|
2970
3002
|
makeRelationSelectIncludeSchema(model, field, options) {
|
|
2971
3003
|
const fieldDef = requireField(this.schema, model, field);
|
|
2972
3004
|
const nextOpts = this.nextOptions(options);
|
|
2973
|
-
let objSchema = z
|
|
2974
|
-
...fieldDef.array || fieldDef.optional ? { where: z
|
|
2975
|
-
select: z
|
|
2976
|
-
include: z
|
|
2977
|
-
omit: z
|
|
3005
|
+
let objSchema = z.strictObject({
|
|
3006
|
+
...fieldDef.array || fieldDef.optional ? { where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false, false, false, nextOpts)).optional() } : {},
|
|
3007
|
+
select: z.lazy(() => this.makeSelectSchema(fieldDef.type, nextOpts)).optional().nullable(),
|
|
3008
|
+
include: z.lazy(() => this.makeIncludeSchema(fieldDef.type, nextOpts)).optional().nullable(),
|
|
3009
|
+
omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional().nullable(),
|
|
2978
3010
|
...fieldDef.array ? {
|
|
2979
|
-
orderBy: z
|
|
3011
|
+
orderBy: z.lazy(() => this.orArray(this.makeOrderBySchema(fieldDef.type, true, false, nextOpts), true)).optional(),
|
|
2980
3012
|
skip: this.makeSkipSchema().optional(),
|
|
2981
3013
|
take: this.makeTakeSchema().optional(),
|
|
2982
3014
|
cursor: this.makeCursorSchema(fieldDef.type, nextOpts).optional(),
|
|
@@ -2986,7 +3018,7 @@ var ZodSchemaFactory = class {
|
|
|
2986
3018
|
objSchema = this.refineForSelectIncludeMutuallyExclusive(objSchema);
|
|
2987
3019
|
objSchema = this.refineForSelectOmitMutuallyExclusive(objSchema);
|
|
2988
3020
|
objSchema = this.refineForSelectHasTruthyField(objSchema);
|
|
2989
|
-
const result = z
|
|
3021
|
+
const result = z.union([z.boolean(), objSchema]);
|
|
2990
3022
|
this.registerSchema(`${model}${upperCaseFirst(field)}RelationInput`, result);
|
|
2991
3023
|
return result;
|
|
2992
3024
|
}
|
|
@@ -3003,11 +3035,11 @@ var ZodSchemaFactory = class {
|
|
|
3003
3035
|
const fields = {};
|
|
3004
3036
|
for (const [field, fieldDef] of this.getModelFields(model)) {
|
|
3005
3037
|
if (this.isParameterizedComputedField(fieldDef)) continue;
|
|
3006
|
-
if (!fieldDef.relation) if (this.options.allowQueryTimeOmitOverride !== false) fields[field] = z
|
|
3007
|
-
else fields[field] = z
|
|
3038
|
+
if (!fieldDef.relation) if (this.options.allowQueryTimeOmitOverride !== false) fields[field] = z.boolean().optional();
|
|
3039
|
+
else fields[field] = z.literal(true).optional();
|
|
3008
3040
|
}
|
|
3009
3041
|
this.addExtResultFields(model, fields);
|
|
3010
|
-
const result = z
|
|
3042
|
+
const result = z.strictObject(fields);
|
|
3011
3043
|
this.registerSchema(`${model}OmitInput`, result);
|
|
3012
3044
|
return result;
|
|
3013
3045
|
}
|
|
@@ -3016,7 +3048,7 @@ var ZodSchemaFactory = class {
|
|
|
3016
3048
|
const resultConfig = plugin.result;
|
|
3017
3049
|
if (resultConfig) {
|
|
3018
3050
|
const modelConfig = resultConfig[lowerCaseFirst(model)];
|
|
3019
|
-
if (modelConfig) for (const field of Object.keys(modelConfig)) fields[field] = z
|
|
3051
|
+
if (modelConfig) for (const field of Object.keys(modelConfig)) fields[field] = z.boolean().optional();
|
|
3020
3052
|
}
|
|
3021
3053
|
}
|
|
3022
3054
|
}
|
|
@@ -3028,35 +3060,35 @@ var ZodSchemaFactory = class {
|
|
|
3028
3060
|
if (fieldDef.relation) {
|
|
3029
3061
|
if (!this.shouldIncludeRelations(options)) continue;
|
|
3030
3062
|
if (this.isModelAllowed(fieldDef.type)) fields[field] = this.makeRelationSelectIncludeSchema(model, field, options).optional();
|
|
3031
|
-
} else if (this.isParameterizedComputedField(fieldDef)) fields[field] = z
|
|
3063
|
+
} else if (this.isParameterizedComputedField(fieldDef)) fields[field] = z.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional();
|
|
3032
3064
|
}
|
|
3033
3065
|
if (this.shouldIncludeRelations(options)) {
|
|
3034
3066
|
const _countSchema = this.makeCountSelectionSchema(model, options);
|
|
3035
|
-
if (!(_countSchema instanceof z
|
|
3067
|
+
if (!(_countSchema instanceof z.ZodNever)) fields["_count"] = _countSchema;
|
|
3036
3068
|
}
|
|
3037
|
-
const result = z
|
|
3069
|
+
const result = z.strictObject(fields);
|
|
3038
3070
|
this.registerSchema(`${model}Include`, result);
|
|
3039
3071
|
return result;
|
|
3040
3072
|
}
|
|
3041
3073
|
makeOrderBySchema(model, withRelation, WithAggregation, options) {
|
|
3042
3074
|
const fields = {};
|
|
3043
|
-
const sort = z
|
|
3075
|
+
const sort = z.union([z.literal("asc"), z.literal("desc")]);
|
|
3044
3076
|
const refineAtMostOneKey = (s) => s.refine((v) => Object.keys(v).length <= 1, { message: "Each orderBy element must have at most one key" });
|
|
3045
3077
|
const nextOpts = this.nextOptions(options);
|
|
3046
3078
|
for (const [field, fieldDef] of this.getModelFields(model)) if (fieldDef.relation) {
|
|
3047
|
-
if (withRelation && this.shouldIncludeRelations(options)) fields[field] = z
|
|
3079
|
+
if (withRelation && this.shouldIncludeRelations(options)) fields[field] = z.lazy(() => {
|
|
3048
3080
|
let relationOrderBy = this.makeOrderBySchema(fieldDef.type, withRelation, WithAggregation, nextOpts);
|
|
3049
3081
|
if (fieldDef.array) relationOrderBy = refineAtMostOneKey(relationOrderBy.safeExtend({ _count: sort }));
|
|
3050
3082
|
return relationOrderBy;
|
|
3051
3083
|
}).optional();
|
|
3052
|
-
} else if (fieldDef.computed && fieldDef.params) fields[field] = z
|
|
3084
|
+
} else if (fieldDef.computed && fieldDef.params) fields[field] = z.strictObject({
|
|
3053
3085
|
args: this.makeFieldArgsSchema(fieldDef.params),
|
|
3054
3086
|
sort,
|
|
3055
|
-
nulls: z
|
|
3087
|
+
nulls: z.union([z.literal("first"), z.literal("last")]).optional()
|
|
3056
3088
|
}).optional();
|
|
3057
|
-
else if (fieldDef.optional) fields[field] = z
|
|
3089
|
+
else if (fieldDef.optional) fields[field] = z.union([sort, z.strictObject({
|
|
3058
3090
|
sort,
|
|
3059
|
-
nulls: z
|
|
3091
|
+
nulls: z.union([z.literal("first"), z.literal("last")]).optional()
|
|
3060
3092
|
})]).optional();
|
|
3061
3093
|
else fields[field] = sort.optional();
|
|
3062
3094
|
if (WithAggregation) for (const agg of [
|
|
@@ -3065,31 +3097,31 @@ var ZodSchemaFactory = class {
|
|
|
3065
3097
|
"_sum",
|
|
3066
3098
|
"_min",
|
|
3067
3099
|
"_max"
|
|
3068
|
-
]) fields[agg] = z
|
|
3100
|
+
]) fields[agg] = z.lazy(() => this.makeOrderBySchema(model, true, false, options).optional());
|
|
3069
3101
|
if (this.providerSupportsFuzzySearch) {
|
|
3070
3102
|
const fuzzyFieldNames = this.getModelFields(model).filter(([, def]) => !def.relation && def.type === "String" && def.fuzzy === true).map(([name]) => name);
|
|
3071
|
-
if (fuzzyFieldNames.length > 0) fields["_fuzzyRelevance"] = z
|
|
3072
|
-
fields: z
|
|
3073
|
-
search: z
|
|
3074
|
-
mode: z
|
|
3075
|
-
z
|
|
3076
|
-
z
|
|
3077
|
-
z
|
|
3103
|
+
if (fuzzyFieldNames.length > 0) fields["_fuzzyRelevance"] = z.strictObject({
|
|
3104
|
+
fields: z.array(z.enum(fuzzyFieldNames)).min(1),
|
|
3105
|
+
search: z.string(),
|
|
3106
|
+
mode: z.union([
|
|
3107
|
+
z.literal("simple"),
|
|
3108
|
+
z.literal("word"),
|
|
3109
|
+
z.literal("strictWord")
|
|
3078
3110
|
]).default("simple"),
|
|
3079
|
-
unaccent: z
|
|
3111
|
+
unaccent: z.boolean().default(false),
|
|
3080
3112
|
sort
|
|
3081
3113
|
}).optional();
|
|
3082
3114
|
}
|
|
3083
3115
|
if (this.providerSupportsFullTextSearch) {
|
|
3084
3116
|
const fullTextFieldNames = this.getModelFields(model).filter(([, def]) => !def.relation && def.type === "String" && def.fullText === true).map(([name]) => name);
|
|
3085
|
-
if (fullTextFieldNames.length > 0) fields["_ftsRelevance"] = z
|
|
3086
|
-
fields: z
|
|
3087
|
-
search: z
|
|
3088
|
-
config: z
|
|
3117
|
+
if (fullTextFieldNames.length > 0) fields["_ftsRelevance"] = z.strictObject({
|
|
3118
|
+
fields: z.array(z.enum(fullTextFieldNames)).min(1),
|
|
3119
|
+
search: z.string().min(1),
|
|
3120
|
+
config: z.string().min(1).optional(),
|
|
3089
3121
|
sort
|
|
3090
3122
|
}).optional();
|
|
3091
3123
|
}
|
|
3092
|
-
const schema = refineAtMostOneKey(z
|
|
3124
|
+
const schema = refineAtMostOneKey(z.strictObject(fields));
|
|
3093
3125
|
let schemaId = `${model}OrderBy`;
|
|
3094
3126
|
if (withRelation) schemaId += "WithRelation";
|
|
3095
3127
|
if (WithAggregation) schemaId += "WithAggregation";
|
|
@@ -3099,7 +3131,7 @@ var ZodSchemaFactory = class {
|
|
|
3099
3131
|
}
|
|
3100
3132
|
makeDistinctSchema(model) {
|
|
3101
3133
|
const nonRelationFields = this.getModelFields(model).filter(([, def]) => !def.relation && !this.isParameterizedComputedField(def)).map(([name]) => name);
|
|
3102
|
-
const schema = nonRelationFields.length > 0 ? this.orArray(z
|
|
3134
|
+
const schema = nonRelationFields.length > 0 ? this.orArray(z.enum(nonRelationFields), true) : z.never();
|
|
3103
3135
|
this.registerSchema(`${model}DistinctInput`, schema);
|
|
3104
3136
|
return schema;
|
|
3105
3137
|
}
|
|
@@ -3110,7 +3142,7 @@ var ZodSchemaFactory = class {
|
|
|
3110
3142
|
}
|
|
3111
3143
|
makeCreateSchema(model, options) {
|
|
3112
3144
|
const dataSchema = this.makeCreateDataSchema(model, false, [], false, options);
|
|
3113
|
-
const baseSchema = z
|
|
3145
|
+
const baseSchema = z.strictObject({
|
|
3114
3146
|
data: dataSchema,
|
|
3115
3147
|
select: this.makeSelectSchema(model, options).optional().nullable(),
|
|
3116
3148
|
include: this.makeIncludeSchema(model, options).optional().nullable(),
|
|
@@ -3159,7 +3191,7 @@ var ZodSchemaFactory = class {
|
|
|
3159
3191
|
const oppositeFieldDef = requireField(this.schema, fieldDef.type, oppositeField);
|
|
3160
3192
|
if (oppositeFieldDef.relation?.fields) excludeFields.push(...oppositeFieldDef.relation.fields);
|
|
3161
3193
|
}
|
|
3162
|
-
let fieldSchema = z
|
|
3194
|
+
let fieldSchema = z.lazy(() => this.makeRelationManipulationSchema(model, field, excludeFields, "create", nextOpts));
|
|
3163
3195
|
if (fieldDef.optional || fieldDef.array) fieldSchema = fieldSchema.optional();
|
|
3164
3196
|
else {
|
|
3165
3197
|
let allFksOptional = false;
|
|
@@ -3176,23 +3208,23 @@ var ZodSchemaFactory = class {
|
|
|
3176
3208
|
let fieldSchema = this.makeScalarSchema(fieldDef.type, fieldDef.attributes);
|
|
3177
3209
|
if (fieldDef.array) {
|
|
3178
3210
|
fieldSchema = ZodUtils.addListValidation(fieldSchema.array(), fieldDef.attributes);
|
|
3179
|
-
fieldSchema = z
|
|
3211
|
+
fieldSchema = z.union([fieldSchema, z.strictObject({ set: fieldSchema })]).optional();
|
|
3180
3212
|
}
|
|
3181
3213
|
if (fieldDef.optional || fieldHasDefaultValue(fieldDef)) fieldSchema = fieldSchema.optional();
|
|
3182
|
-
if (fieldDef.optional) if (fieldDef.type === "Json") fieldSchema = z
|
|
3214
|
+
if (fieldDef.optional) if (fieldDef.type === "Json") fieldSchema = z.union([fieldSchema, z.instanceof(DbNullClass)]);
|
|
3183
3215
|
else if (this.isTypeDefType(fieldDef.type)) fieldSchema = this.makeNullableTypedJsonMutationSchema(fieldSchema);
|
|
3184
3216
|
else fieldSchema = fieldSchema.nullable();
|
|
3185
3217
|
uncheckedVariantFields[field] = fieldSchema;
|
|
3186
3218
|
if (!fieldDef.foreignKeyFor) checkedVariantFields[field] = fieldSchema;
|
|
3187
3219
|
}
|
|
3188
3220
|
});
|
|
3189
|
-
const uncheckedCreateSchema = this.extraValidationsEnabled ? ZodUtils.addCustomValidation(z
|
|
3190
|
-
const checkedCreateSchema = this.extraValidationsEnabled ? ZodUtils.addCustomValidation(z
|
|
3191
|
-
const result = !hasRelation ? this.orArray(uncheckedCreateSchema, canBeArray) : z
|
|
3221
|
+
const uncheckedCreateSchema = this.extraValidationsEnabled ? ZodUtils.addCustomValidation(z.strictObject(uncheckedVariantFields), modelDef.attributes) : z.strictObject(uncheckedVariantFields);
|
|
3222
|
+
const checkedCreateSchema = this.extraValidationsEnabled ? ZodUtils.addCustomValidation(z.strictObject(checkedVariantFields), modelDef.attributes) : z.strictObject(checkedVariantFields);
|
|
3223
|
+
const result = !hasRelation ? this.orArray(uncheckedCreateSchema, canBeArray) : z.union([
|
|
3192
3224
|
uncheckedCreateSchema,
|
|
3193
3225
|
checkedCreateSchema,
|
|
3194
|
-
...canBeArray ? [z
|
|
3195
|
-
...canBeArray ? [z
|
|
3226
|
+
...canBeArray ? [z.array(uncheckedCreateSchema)] : [],
|
|
3227
|
+
...canBeArray ? [z.array(checkedCreateSchema)] : []
|
|
3196
3228
|
]);
|
|
3197
3229
|
const idParts = [`${model}CreateData`];
|
|
3198
3230
|
if (canBeArray) idParts.push("Array");
|
|
@@ -3217,17 +3249,17 @@ var ZodSchemaFactory = class {
|
|
|
3217
3249
|
fields["disconnect"] = this.makeDisconnectDataSchema(fieldType, array, options).optional();
|
|
3218
3250
|
fields["delete"] = this.makeDeleteRelationDataSchema(fieldType, array, true, options).optional();
|
|
3219
3251
|
}
|
|
3220
|
-
fields["update"] = array ? this.orArray(z
|
|
3252
|
+
fields["update"] = array ? this.orArray(z.strictObject({
|
|
3221
3253
|
where: this.makeWhereSchema(fieldType, true, false, false, options),
|
|
3222
3254
|
data: this.makeUpdateDataSchema(fieldType, withoutFields, false, options)
|
|
3223
|
-
}), true).optional() : z
|
|
3255
|
+
}), true).optional() : z.union([z.strictObject({
|
|
3224
3256
|
where: this.makeWhereSchema(fieldType, false, false, false, options).optional(),
|
|
3225
3257
|
data: this.makeUpdateDataSchema(fieldType, withoutFields, false, options)
|
|
3226
3258
|
}), this.makeUpdateDataSchema(fieldType, withoutFields, false, options)]).optional();
|
|
3227
3259
|
if (canCreateModel) {
|
|
3228
3260
|
let upsertWhere = this.makeWhereSchema(fieldType, true, false, false, options);
|
|
3229
3261
|
if (!fieldDef.array) upsertWhere = upsertWhere.optional();
|
|
3230
|
-
fields["upsert"] = this.orArray(z
|
|
3262
|
+
fields["upsert"] = this.orArray(z.strictObject({
|
|
3231
3263
|
where: upsertWhere,
|
|
3232
3264
|
create: this.makeCreateDataSchema(fieldType, false, withoutFields, false, options),
|
|
3233
3265
|
update: this.makeUpdateDataSchema(fieldType, withoutFields, false, options)
|
|
@@ -3235,14 +3267,14 @@ var ZodSchemaFactory = class {
|
|
|
3235
3267
|
}
|
|
3236
3268
|
if (array) {
|
|
3237
3269
|
fields["set"] = this.makeSetDataSchema(fieldType, true, options).optional();
|
|
3238
|
-
fields["updateMany"] = this.orArray(z
|
|
3270
|
+
fields["updateMany"] = this.orArray(z.strictObject({
|
|
3239
3271
|
where: this.makeWhereSchema(fieldType, false, true, false, options),
|
|
3240
3272
|
data: this.makeUpdateDataSchema(fieldType, withoutFields, false, options)
|
|
3241
3273
|
}), true).optional();
|
|
3242
3274
|
fields["deleteMany"] = this.makeDeleteRelationDataSchema(fieldType, true, false, options).optional();
|
|
3243
3275
|
}
|
|
3244
3276
|
}
|
|
3245
|
-
return z
|
|
3277
|
+
return z.strictObject(fields);
|
|
3246
3278
|
}
|
|
3247
3279
|
makeSetDataSchema(model, canBeArray, options) {
|
|
3248
3280
|
return this.orArray(this.makeWhereSchema(model, true, false, false, options), canBeArray);
|
|
@@ -3252,23 +3284,23 @@ var ZodSchemaFactory = class {
|
|
|
3252
3284
|
}
|
|
3253
3285
|
makeDisconnectDataSchema(model, canBeArray, options) {
|
|
3254
3286
|
if (canBeArray) return this.orArray(this.makeWhereSchema(model, true, false, false, options), canBeArray);
|
|
3255
|
-
else return z
|
|
3287
|
+
else return z.union([z.boolean(), this.makeWhereSchema(model, false, false, false, options)]);
|
|
3256
3288
|
}
|
|
3257
3289
|
makeDeleteRelationDataSchema(model, toManyRelation, uniqueFilter, options) {
|
|
3258
|
-
return toManyRelation ? this.orArray(this.makeWhereSchema(model, uniqueFilter, false, false, options), true) : z
|
|
3290
|
+
return toManyRelation ? this.orArray(this.makeWhereSchema(model, uniqueFilter, false, false, options), true) : z.union([z.boolean(), this.makeWhereSchema(model, uniqueFilter, false, false, options)]);
|
|
3259
3291
|
}
|
|
3260
3292
|
makeConnectOrCreateDataSchema(model, canBeArray, withoutFields, options) {
|
|
3261
3293
|
const whereSchema = this.makeWhereSchema(model, true, false, false, options);
|
|
3262
3294
|
const createSchema = this.makeCreateDataSchema(model, false, withoutFields, false, options);
|
|
3263
|
-
return this.orArray(z
|
|
3295
|
+
return this.orArray(z.strictObject({
|
|
3264
3296
|
where: whereSchema,
|
|
3265
3297
|
create: createSchema
|
|
3266
3298
|
}), canBeArray);
|
|
3267
3299
|
}
|
|
3268
3300
|
makeCreateManyPayloadSchema(model, withoutFields, options) {
|
|
3269
|
-
const schema = z
|
|
3301
|
+
const schema = z.strictObject({
|
|
3270
3302
|
data: this.makeCreateDataSchema(model, true, withoutFields, true, options),
|
|
3271
|
-
skipDuplicates: z
|
|
3303
|
+
skipDuplicates: z.boolean().optional()
|
|
3272
3304
|
});
|
|
3273
3305
|
const idParts = [`${model}CreateManyPayload`];
|
|
3274
3306
|
if (withoutFields.length) idParts.push(`Without${withoutFields.slice().sort().join("")}`);
|
|
@@ -3276,7 +3308,7 @@ var ZodSchemaFactory = class {
|
|
|
3276
3308
|
return schema;
|
|
3277
3309
|
}
|
|
3278
3310
|
makeUpdateSchema(model, options) {
|
|
3279
|
-
const baseSchema = z
|
|
3311
|
+
const baseSchema = z.strictObject({
|
|
3280
3312
|
where: this.makeWhereSchema(model, true, false, false, options),
|
|
3281
3313
|
data: this.makeUpdateDataSchema(model, [], false, options),
|
|
3282
3314
|
select: this.makeSelectSchema(model, options).optional().nullable(),
|
|
@@ -3291,10 +3323,10 @@ var ZodSchemaFactory = class {
|
|
|
3291
3323
|
return schema;
|
|
3292
3324
|
}
|
|
3293
3325
|
makeUpdateManySchema(model, options) {
|
|
3294
|
-
const result = this.mergePluginArgsSchema(z
|
|
3326
|
+
const result = this.mergePluginArgsSchema(z.strictObject({
|
|
3295
3327
|
where: this.makeWhereSchema(model, false, false, false, options).optional(),
|
|
3296
3328
|
data: this.makeUpdateDataSchema(model, [], true, options),
|
|
3297
|
-
limit: z
|
|
3329
|
+
limit: z.number().int().nonnegative().optional()
|
|
3298
3330
|
}), "updateMany");
|
|
3299
3331
|
this.registerSchema(`${model}UpdateManyArgs`, result);
|
|
3300
3332
|
return result;
|
|
@@ -3310,7 +3342,7 @@ var ZodSchemaFactory = class {
|
|
|
3310
3342
|
return schema;
|
|
3311
3343
|
}
|
|
3312
3344
|
makeUpsertSchema(model, options) {
|
|
3313
|
-
const baseSchema = z
|
|
3345
|
+
const baseSchema = z.strictObject({
|
|
3314
3346
|
where: this.makeWhereSchema(model, true, false, false, options),
|
|
3315
3347
|
create: this.makeCreateDataSchema(model, false, [], false, options),
|
|
3316
3348
|
update: this.makeUpdateDataSchema(model, [], false, options),
|
|
@@ -3346,27 +3378,27 @@ var ZodSchemaFactory = class {
|
|
|
3346
3378
|
const oppositeFieldDef = requireField(this.schema, fieldDef.type, oppositeField);
|
|
3347
3379
|
if (oppositeFieldDef.relation?.fields) excludeFields.push(...oppositeFieldDef.relation.fields);
|
|
3348
3380
|
}
|
|
3349
|
-
let fieldSchema = z
|
|
3381
|
+
let fieldSchema = z.lazy(() => this.makeRelationManipulationSchema(model, field, excludeFields, "update", nextOpts)).optional();
|
|
3350
3382
|
if (fieldDef.optional && !fieldDef.array) fieldSchema = fieldSchema.nullable();
|
|
3351
3383
|
checkedVariantFields[field] = fieldSchema;
|
|
3352
3384
|
if (fieldDef.array || !fieldDef.relation.references) uncheckedVariantFields[field] = fieldSchema;
|
|
3353
3385
|
} else {
|
|
3354
3386
|
let fieldSchema = this.makeScalarSchema(fieldDef.type, fieldDef.attributes);
|
|
3355
|
-
if (this.isNumericField(fieldDef)) fieldSchema = z
|
|
3356
|
-
set: this.nullableIf(z
|
|
3357
|
-
increment: z
|
|
3358
|
-
decrement: z
|
|
3359
|
-
multiply: z
|
|
3360
|
-
divide: z
|
|
3387
|
+
if (this.isNumericField(fieldDef)) fieldSchema = z.union([fieldSchema, z.object({
|
|
3388
|
+
set: this.nullableIf(z.number().optional(), !!fieldDef.optional).optional(),
|
|
3389
|
+
increment: z.number().optional(),
|
|
3390
|
+
decrement: z.number().optional(),
|
|
3391
|
+
multiply: z.number().optional(),
|
|
3392
|
+
divide: z.number().optional()
|
|
3361
3393
|
}).refine((v) => Object.keys(v).length === 1, "Only one of \"set\", \"increment\", \"decrement\", \"multiply\", or \"divide\" can be provided")]);
|
|
3362
3394
|
if (fieldDef.array) {
|
|
3363
3395
|
const arraySchema = ZodUtils.addListValidation(fieldSchema.array(), fieldDef.attributes);
|
|
3364
|
-
fieldSchema = z
|
|
3396
|
+
fieldSchema = z.union([arraySchema, z.object({
|
|
3365
3397
|
set: arraySchema.optional(),
|
|
3366
|
-
push: z
|
|
3398
|
+
push: z.union([fieldSchema, fieldSchema.array()]).optional()
|
|
3367
3399
|
}).refine((v) => Object.keys(v).length === 1, "Only one of \"set\", \"push\" can be provided")]);
|
|
3368
3400
|
}
|
|
3369
|
-
if (fieldDef.optional) if (fieldDef.type === "Json") fieldSchema = z
|
|
3401
|
+
if (fieldDef.optional) if (fieldDef.type === "Json") fieldSchema = z.union([fieldSchema, z.instanceof(DbNullClass)]);
|
|
3370
3402
|
else if (this.isTypeDefType(fieldDef.type)) fieldSchema = this.makeNullableTypedJsonMutationSchema(fieldSchema);
|
|
3371
3403
|
else fieldSchema = fieldSchema.nullable();
|
|
3372
3404
|
fieldSchema = fieldSchema.optional();
|
|
@@ -3374,9 +3406,9 @@ var ZodSchemaFactory = class {
|
|
|
3374
3406
|
if (!fieldDef.foreignKeyFor) checkedVariantFields[field] = fieldSchema;
|
|
3375
3407
|
}
|
|
3376
3408
|
});
|
|
3377
|
-
const uncheckedUpdateSchema = this.extraValidationsEnabled ? ZodUtils.addCustomValidation(z
|
|
3378
|
-
const checkedUpdateSchema = this.extraValidationsEnabled ? ZodUtils.addCustomValidation(z
|
|
3379
|
-
const result = !hasRelation ? uncheckedUpdateSchema : z
|
|
3409
|
+
const uncheckedUpdateSchema = this.extraValidationsEnabled ? ZodUtils.addCustomValidation(z.strictObject(uncheckedVariantFields), modelDef.attributes) : z.strictObject(uncheckedVariantFields);
|
|
3410
|
+
const checkedUpdateSchema = this.extraValidationsEnabled ? ZodUtils.addCustomValidation(z.strictObject(checkedVariantFields), modelDef.attributes) : z.strictObject(checkedVariantFields);
|
|
3411
|
+
const result = !hasRelation ? uncheckedUpdateSchema : z.union([uncheckedUpdateSchema, checkedUpdateSchema]);
|
|
3380
3412
|
const idParts = [`${model}UpdateData`];
|
|
3381
3413
|
if (withoutRelationFields) idParts.push("WithoutRelation");
|
|
3382
3414
|
if (withoutFields.length) idParts.push(`Without${withoutFields.slice().sort().join("")}`);
|
|
@@ -3384,7 +3416,7 @@ var ZodSchemaFactory = class {
|
|
|
3384
3416
|
return result;
|
|
3385
3417
|
}
|
|
3386
3418
|
makeDeleteSchema(model, options) {
|
|
3387
|
-
const baseSchema = z
|
|
3419
|
+
const baseSchema = z.strictObject({
|
|
3388
3420
|
where: this.makeWhereSchema(model, true, false, false, options),
|
|
3389
3421
|
select: this.makeSelectSchema(model, options).optional().nullable(),
|
|
3390
3422
|
include: this.makeIncludeSchema(model, options).optional().nullable(),
|
|
@@ -3398,15 +3430,15 @@ var ZodSchemaFactory = class {
|
|
|
3398
3430
|
return schema;
|
|
3399
3431
|
}
|
|
3400
3432
|
makeDeleteManySchema(model, options) {
|
|
3401
|
-
const result = this.mergePluginArgsSchema(z
|
|
3433
|
+
const result = this.mergePluginArgsSchema(z.strictObject({
|
|
3402
3434
|
where: this.makeWhereSchema(model, false, false, false, options).optional(),
|
|
3403
|
-
limit: z
|
|
3435
|
+
limit: z.number().int().nonnegative().optional()
|
|
3404
3436
|
}), "deleteMany").optional();
|
|
3405
3437
|
this.registerSchema(`${model}DeleteManyArgs`, result);
|
|
3406
3438
|
return result;
|
|
3407
3439
|
}
|
|
3408
3440
|
makeCountSchema(model, options) {
|
|
3409
|
-
const result = this.mergePluginArgsSchema(z
|
|
3441
|
+
const result = this.mergePluginArgsSchema(z.strictObject({
|
|
3410
3442
|
where: this.makeWhereSchema(model, false, false, false, options).optional(),
|
|
3411
3443
|
skip: this.makeSkipSchema().optional(),
|
|
3412
3444
|
take: this.makeTakeSchema().optional(),
|
|
@@ -3417,10 +3449,10 @@ var ZodSchemaFactory = class {
|
|
|
3417
3449
|
return result;
|
|
3418
3450
|
}
|
|
3419
3451
|
makeCountAggregateInputSchema(model) {
|
|
3420
|
-
const schema = z
|
|
3421
|
-
_all: z
|
|
3452
|
+
const schema = z.union([z.literal(true), z.strictObject({
|
|
3453
|
+
_all: z.literal(true).optional(),
|
|
3422
3454
|
...this.getModelFields(model).reduce((acc, [field, fieldDef]) => {
|
|
3423
|
-
acc[field] = this.isParameterizedComputedField(fieldDef) ? z
|
|
3455
|
+
acc[field] = this.isParameterizedComputedField(fieldDef) ? z.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional() : z.literal(true).optional();
|
|
3424
3456
|
return acc;
|
|
3425
3457
|
}, {})
|
|
3426
3458
|
})]);
|
|
@@ -3428,7 +3460,7 @@ var ZodSchemaFactory = class {
|
|
|
3428
3460
|
return schema;
|
|
3429
3461
|
}
|
|
3430
3462
|
makeAggregateSchema(model, options) {
|
|
3431
|
-
const result = this.mergePluginArgsSchema(z
|
|
3463
|
+
const result = this.mergePluginArgsSchema(z.strictObject({
|
|
3432
3464
|
where: this.makeWhereSchema(model, false, false, false, options).optional(),
|
|
3433
3465
|
skip: this.makeSkipSchema().optional(),
|
|
3434
3466
|
take: this.makeTakeSchema().optional(),
|
|
@@ -3443,16 +3475,16 @@ var ZodSchemaFactory = class {
|
|
|
3443
3475
|
return result;
|
|
3444
3476
|
}
|
|
3445
3477
|
makeSumAvgInputSchema(model) {
|
|
3446
|
-
const schema = z
|
|
3447
|
-
if (this.isNumericField(fieldDef)) acc[field] = this.isParameterizedComputedField(fieldDef) ? z
|
|
3478
|
+
const schema = z.strictObject(this.getModelFields(model).reduce((acc, [field, fieldDef]) => {
|
|
3479
|
+
if (this.isNumericField(fieldDef)) acc[field] = this.isParameterizedComputedField(fieldDef) ? z.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional() : z.literal(true).optional();
|
|
3448
3480
|
return acc;
|
|
3449
3481
|
}, {}));
|
|
3450
3482
|
this.registerSchema(`${model}SumAvgAggregateInput`, schema);
|
|
3451
3483
|
return schema;
|
|
3452
3484
|
}
|
|
3453
3485
|
makeMinMaxInputSchema(model) {
|
|
3454
|
-
const schema = z
|
|
3455
|
-
if (!fieldDef.relation && !fieldDef.array) acc[field] = this.isParameterizedComputedField(fieldDef) ? z
|
|
3486
|
+
const schema = z.strictObject(this.getModelFields(model).reduce((acc, [field, fieldDef]) => {
|
|
3487
|
+
if (!fieldDef.relation && !fieldDef.array) acc[field] = this.isParameterizedComputedField(fieldDef) ? z.strictObject({ args: this.makeFieldArgsSchema(fieldDef.params) }).optional() : z.literal(true).optional();
|
|
3456
3488
|
return acc;
|
|
3457
3489
|
}, {}));
|
|
3458
3490
|
this.registerSchema(`${model}MinMaxAggregateInput`, schema);
|
|
@@ -3461,14 +3493,14 @@ var ZodSchemaFactory = class {
|
|
|
3461
3493
|
makeGroupBySchema(model, options) {
|
|
3462
3494
|
const nonRelationFields = this.getModelFields(model).filter(([, def]) => !def.relation && !this.isParameterizedComputedField(def)).map(([name]) => name);
|
|
3463
3495
|
const byMembers = [];
|
|
3464
|
-
if (nonRelationFields.length > 0) byMembers.push(z
|
|
3465
|
-
for (const [field, fieldDef] of this.getModelFields(model)) if (this.isParameterizedComputedField(fieldDef)) byMembers.push(z
|
|
3466
|
-
field: z
|
|
3496
|
+
if (nonRelationFields.length > 0) byMembers.push(z.enum(nonRelationFields));
|
|
3497
|
+
for (const [field, fieldDef] of this.getModelFields(model)) if (this.isParameterizedComputedField(fieldDef)) byMembers.push(z.strictObject({
|
|
3498
|
+
field: z.literal(field),
|
|
3467
3499
|
args: this.makeFieldArgsSchema(fieldDef.params)
|
|
3468
3500
|
}));
|
|
3469
|
-
const byElement = byMembers.length === 0 ? void 0 : byMembers.length === 1 ? byMembers[0] : z
|
|
3470
|
-
const bySchema = byElement ? this.orArray(byElement, true) : z
|
|
3471
|
-
const baseSchema = z
|
|
3501
|
+
const byElement = byMembers.length === 0 ? void 0 : byMembers.length === 1 ? byMembers[0] : z.union(byMembers);
|
|
3502
|
+
const bySchema = byElement ? this.orArray(byElement, true) : z.never();
|
|
3503
|
+
const baseSchema = z.strictObject({
|
|
3472
3504
|
where: this.makeWhereSchema(model, false, false, false, options).optional(),
|
|
3473
3505
|
orderBy: this.orArray(this.makeOrderBySchema(model, false, true, options), true).optional(),
|
|
3474
3506
|
by: bySchema,
|
|
@@ -3520,7 +3552,7 @@ var ZodSchemaFactory = class {
|
|
|
3520
3552
|
if (!procDef) throw createInternalError(`Procedure not found: ${procName}`);
|
|
3521
3553
|
const shape = {};
|
|
3522
3554
|
for (const param of Object.values(procDef.params ?? {})) shape[param.name] = this.makeProcedureParamSchema(param);
|
|
3523
|
-
const schema = z
|
|
3555
|
+
const schema = z.object(shape);
|
|
3524
3556
|
this.registerSchema(`${procName}ProcArgs`, schema);
|
|
3525
3557
|
return schema;
|
|
3526
3558
|
}
|
|
@@ -3528,10 +3560,10 @@ var ZodSchemaFactory = class {
|
|
|
3528
3560
|
let schema;
|
|
3529
3561
|
if (isTypeDef(this.schema, param.type)) schema = this.makeTypeDefSchema(param.type);
|
|
3530
3562
|
else if (isEnum(this.schema, param.type)) schema = this.makeEnumSchema(param.type);
|
|
3531
|
-
else if (param.type in (this.schema.models ?? {})) schema = z
|
|
3563
|
+
else if (param.type in (this.schema.models ?? {})) schema = z.record(z.string(), z.unknown());
|
|
3532
3564
|
else {
|
|
3533
3565
|
schema = this.makeScalarSchema(param.type);
|
|
3534
|
-
if (schema instanceof z
|
|
3566
|
+
if (schema instanceof z.ZodUnknown) throw createInternalError(`Unsupported procedure parameter type: ${param.type}`);
|
|
3535
3567
|
}
|
|
3536
3568
|
if (param.array) schema = schema.array();
|
|
3537
3569
|
if (param.optional) schema = schema.optional();
|
|
@@ -3567,10 +3599,10 @@ var ZodSchemaFactory = class {
|
|
|
3567
3599
|
return result;
|
|
3568
3600
|
}
|
|
3569
3601
|
makeSkipSchema() {
|
|
3570
|
-
return z
|
|
3602
|
+
return z.number().int().nonnegative();
|
|
3571
3603
|
}
|
|
3572
3604
|
makeTakeSchema() {
|
|
3573
|
-
return z
|
|
3605
|
+
return z.number().int();
|
|
3574
3606
|
}
|
|
3575
3607
|
refineForSelectIncludeMutuallyExclusive(schema) {
|
|
3576
3608
|
return schema.refine((value) => !(value["select"] && value["include"]), "\"select\" and \"include\" cannot be used together");
|
|
@@ -3589,7 +3621,7 @@ var ZodSchemaFactory = class {
|
|
|
3589
3621
|
return nullable ? schema.nullable() : schema;
|
|
3590
3622
|
}
|
|
3591
3623
|
orArray(schema, canBeArray) {
|
|
3592
|
-
return canBeArray ? z
|
|
3624
|
+
return canBeArray ? z.union([schema, z.array(schema)]) : schema;
|
|
3593
3625
|
}
|
|
3594
3626
|
isNumericField(fieldDef) {
|
|
3595
3627
|
return NUMERIC_FIELD_TYPES.includes(fieldDef.type) && !fieldDef.array;
|
|
@@ -3651,10 +3683,10 @@ var ZodSchemaFactory = class {
|
|
|
3651
3683
|
createUnionFilterSchema(valueSchema, optional, components, allowedFilterKinds) {
|
|
3652
3684
|
if (Object.keys(components).length === 0) {
|
|
3653
3685
|
if (!allowedFilterKinds || allowedFilterKinds.includes("Equality")) return this.nullableIf(valueSchema, optional);
|
|
3654
|
-
return z
|
|
3686
|
+
return z.never();
|
|
3655
3687
|
}
|
|
3656
|
-
if (!allowedFilterKinds || allowedFilterKinds.includes("Equality")) return z
|
|
3657
|
-
else return z
|
|
3688
|
+
if (!allowedFilterKinds || allowedFilterKinds.includes("Equality")) return z.union([this.nullableIf(valueSchema, optional), z.strictObject(components)]);
|
|
3689
|
+
else return z.strictObject(components);
|
|
3658
3690
|
}
|
|
3659
3691
|
canCreateModel(model) {
|
|
3660
3692
|
const modelDef = requireModel(this.schema, model);
|
|
@@ -4311,7 +4343,7 @@ var BaseOperationHandler = class {
|
|
|
4311
4343
|
this.client = client;
|
|
4312
4344
|
this.model = model;
|
|
4313
4345
|
this.inputValidator = inputValidator;
|
|
4314
|
-
this.dialect = getCrudDialect(this.
|
|
4346
|
+
this.dialect = getCrudDialect(this.client);
|
|
4315
4347
|
}
|
|
4316
4348
|
get schema() {
|
|
4317
4349
|
return this.client.$schema;
|
|
@@ -5944,7 +5976,7 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5944
5976
|
constructor(client) {
|
|
5945
5977
|
super();
|
|
5946
5978
|
this.client = client;
|
|
5947
|
-
this.dialect = getCrudDialect(client
|
|
5979
|
+
this.dialect = getCrudDialect(client);
|
|
5948
5980
|
for (const [modelName, modelDef] of Object.entries(client.$schema.models)) {
|
|
5949
5981
|
const mappedName = this.getMappedName(modelDef);
|
|
5950
5982
|
if (mappedName) this.modelToTableMap.set(modelName, mappedName);
|
|
@@ -6407,22 +6439,22 @@ const DEFAULT_MAX_SLOW_RECORDS = 100;
|
|
|
6407
6439
|
var ZenStackQueryExecutor = class ZenStackQueryExecutor extends DefaultQueryExecutor {
|
|
6408
6440
|
nameMapper;
|
|
6409
6441
|
dialect;
|
|
6410
|
-
constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false) {
|
|
6442
|
+
constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false, nameMapper) {
|
|
6411
6443
|
super(compiler, adapter, connectionProvider, plugins);
|
|
6412
6444
|
this.client = client;
|
|
6413
6445
|
this.driver = driver;
|
|
6414
6446
|
this.compiler = compiler;
|
|
6415
6447
|
this.connectionProvider = connectionProvider;
|
|
6416
6448
|
this.suppressMutationHooks = suppressMutationHooks;
|
|
6417
|
-
|
|
6418
|
-
this.dialect = getCrudDialect(client.$
|
|
6449
|
+
this.nameMapper = nameMapper ?? (client.$schema.provider.type === "postgresql" || schemaHasMappedNames(client.$schema) ? new QueryNameMapper(client.$contract) : void 0);
|
|
6450
|
+
this.dialect = getCrudDialect(client.$contract);
|
|
6419
6451
|
}
|
|
6420
|
-
|
|
6421
|
-
|
|
6422
|
-
|
|
6423
|
-
|
|
6424
|
-
|
|
6425
|
-
return
|
|
6452
|
+
/**
|
|
6453
|
+
* The name mapper built for this executor's schema, if the schema needs one. Exposed so that
|
|
6454
|
+
* derived clients built from the same schema and options can reuse it instead of rebuilding it.
|
|
6455
|
+
*/
|
|
6456
|
+
getNameMapper() {
|
|
6457
|
+
return this.nameMapper;
|
|
6426
6458
|
}
|
|
6427
6459
|
get kysely() {
|
|
6428
6460
|
return this.client.$qb;
|
|
@@ -6469,7 +6501,7 @@ var ZenStackQueryExecutor = class ZenStackQueryExecutor extends DefaultQueryExec
|
|
|
6469
6501
|
proceed = async (query) => {
|
|
6470
6502
|
const _p = (q) => _proceed(q);
|
|
6471
6503
|
return await hook({
|
|
6472
|
-
client: this.client,
|
|
6504
|
+
client: this.client.$contract,
|
|
6473
6505
|
schema: this.client.$schema,
|
|
6474
6506
|
query,
|
|
6475
6507
|
proceed: _p
|
|
@@ -6707,7 +6739,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
6707
6739
|
innerExecutor.suppressMutationHooks = true;
|
|
6708
6740
|
const innerClient = this.client.withExecutor(innerExecutor);
|
|
6709
6741
|
if (inTx) innerClient.forceTransaction();
|
|
6710
|
-
return innerClient;
|
|
6742
|
+
return innerClient.$contract;
|
|
6711
6743
|
}
|
|
6712
6744
|
andNodes(condition1, condition2) {
|
|
6713
6745
|
if (condition1 && condition2) return WhereNode.create(AndNode.create(condition1, condition2));
|
|
@@ -6759,19 +6791,19 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
6759
6791
|
return finalResult;
|
|
6760
6792
|
}
|
|
6761
6793
|
withPlugin(plugin) {
|
|
6762
|
-
return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [...this.plugins, plugin], this.suppressMutationHooks);
|
|
6794
|
+
return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [...this.plugins, plugin], this.suppressMutationHooks, this.nameMapper);
|
|
6763
6795
|
}
|
|
6764
6796
|
withPlugins(plugins) {
|
|
6765
|
-
return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [...this.plugins, ...plugins], this.suppressMutationHooks);
|
|
6797
|
+
return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [...this.plugins, ...plugins], this.suppressMutationHooks, this.nameMapper);
|
|
6766
6798
|
}
|
|
6767
6799
|
withPluginAtFront(plugin) {
|
|
6768
|
-
return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [plugin, ...this.plugins], this.suppressMutationHooks);
|
|
6800
|
+
return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [plugin, ...this.plugins], this.suppressMutationHooks, this.nameMapper);
|
|
6769
6801
|
}
|
|
6770
6802
|
withoutPlugins() {
|
|
6771
|
-
return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [], this.suppressMutationHooks);
|
|
6803
|
+
return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [], this.suppressMutationHooks, this.nameMapper);
|
|
6772
6804
|
}
|
|
6773
6805
|
withConnectionProvider(connectionProvider) {
|
|
6774
|
-
const newExecutor = new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, connectionProvider, this.plugins, this.suppressMutationHooks);
|
|
6806
|
+
const newExecutor = new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, connectionProvider, this.plugins, this.suppressMutationHooks, this.nameMapper);
|
|
6775
6807
|
newExecutor.client = this.client.withExecutor(newExecutor);
|
|
6776
6808
|
return newExecutor;
|
|
6777
6809
|
}
|
|
@@ -7227,6 +7259,12 @@ var ResultProcessor = class {
|
|
|
7227
7259
|
//#endregion
|
|
7228
7260
|
//#region src/client/client-impl.ts
|
|
7229
7261
|
/**
|
|
7262
|
+
* Returns the name mapper held by a ZenStack executor, or undefined for a plain kysely one.
|
|
7263
|
+
*/
|
|
7264
|
+
function getExecutorNameMapper(executor) {
|
|
7265
|
+
return executor instanceof ZenStackQueryExecutor ? executor.getNameMapper() : void 0;
|
|
7266
|
+
}
|
|
7267
|
+
/**
|
|
7230
7268
|
* ZenStack ORM client.
|
|
7231
7269
|
*/
|
|
7232
7270
|
const ZenStackClient = function(schema, options) {
|
|
@@ -7254,7 +7292,7 @@ var ClientImpl = class ClientImpl {
|
|
|
7254
7292
|
if (baseClient) {
|
|
7255
7293
|
this.kyselyProps = {
|
|
7256
7294
|
...baseClient.kyselyProps,
|
|
7257
|
-
executor: executor ?? new ZenStackQueryExecutor(this, baseClient.kyselyProps.driver, baseClient.kyselyProps.dialect.createQueryCompiler(), baseClient.kyselyProps.dialect.createAdapter(), new DefaultConnectionProvider(baseClient.kyselyProps.driver))
|
|
7295
|
+
executor: executor ?? new ZenStackQueryExecutor(this, baseClient.kyselyProps.driver, baseClient.kyselyProps.dialect.createQueryCompiler(), baseClient.kyselyProps.dialect.createAdapter(), new DefaultConnectionProvider(baseClient.kyselyProps.driver), [], false, baseClient.$schema === schema && baseClient.$options === options ? getExecutorNameMapper(baseClient.kyselyProps.executor) : void 0)
|
|
7258
7296
|
};
|
|
7259
7297
|
this.kyselyRaw = baseClient.kyselyRaw;
|
|
7260
7298
|
this.auth = baseClient.auth;
|
|
@@ -7286,9 +7324,9 @@ var ClientImpl = class ClientImpl {
|
|
|
7286
7324
|
validateOptions(baseClient, options) {
|
|
7287
7325
|
if (!baseClient && !options.skipValidationForComputedFields) this.validateComputedFieldsConfig(options);
|
|
7288
7326
|
if (options.diagnostics) {
|
|
7289
|
-
const parseResult = z.object({
|
|
7290
|
-
slowQueryThresholdMs: z.number().nonnegative().optional(),
|
|
7291
|
-
slowQueryMaxRecords: z.int().nonnegative().or(z.literal(Infinity)).optional()
|
|
7327
|
+
const parseResult = z$1.object({
|
|
7328
|
+
slowQueryThresholdMs: z$1.number().nonnegative().optional(),
|
|
7329
|
+
slowQueryMaxRecords: z$1.int().nonnegative().or(z$1.literal(Infinity)).optional()
|
|
7292
7330
|
}).safeParse(options.diagnostics);
|
|
7293
7331
|
if (!parseResult.success) throw createConfigError(`Invalid diagnostics configuration: ${formatError(parseResult.error)}`);
|
|
7294
7332
|
}
|
|
@@ -7331,14 +7369,14 @@ var ClientImpl = class ClientImpl {
|
|
|
7331
7369
|
if (!this.kysely.isTransaction) this.kysely = new Transaction(this.kyselyProps);
|
|
7332
7370
|
}
|
|
7333
7371
|
async interactiveTransaction(callback, options) {
|
|
7334
|
-
if (this.kysely.isTransaction) return callback(this);
|
|
7372
|
+
if (this.kysely.isTransaction) return callback(this.$contract);
|
|
7335
7373
|
else {
|
|
7336
7374
|
let txBuilder = this.kysely.transaction();
|
|
7337
7375
|
if (options?.isolationLevel) txBuilder = txBuilder.setIsolationLevel(options.isolationLevel);
|
|
7338
7376
|
return txBuilder.execute((tx) => {
|
|
7339
7377
|
const txClient = new ClientImpl(this.schema, this.$options, this);
|
|
7340
7378
|
txClient.kysely = tx;
|
|
7341
|
-
return callback(txClient);
|
|
7379
|
+
return callback(txClient.$contract);
|
|
7342
7380
|
});
|
|
7343
7381
|
}
|
|
7344
7382
|
}
|
|
@@ -7355,7 +7393,7 @@ var ClientImpl = class ClientImpl {
|
|
|
7355
7393
|
const result = [];
|
|
7356
7394
|
for (const promise of arg) {
|
|
7357
7395
|
const cb = this.getPromiseCallback(promise);
|
|
7358
|
-
result.push(await cb(txClient));
|
|
7396
|
+
result.push(await cb(txClient.$contract));
|
|
7359
7397
|
}
|
|
7360
7398
|
return result;
|
|
7361
7399
|
};
|
|
@@ -7456,6 +7494,16 @@ var ClientImpl = class ClientImpl {
|
|
|
7456
7494
|
get $auth() {
|
|
7457
7495
|
return this.auth;
|
|
7458
7496
|
}
|
|
7497
|
+
/**
|
|
7498
|
+
* This client viewed through its public typed contract. `ClientImpl` is intentionally
|
|
7499
|
+
* untyped internally — the model accessors are added by the runtime proxy — so this
|
|
7500
|
+
* getter is the single sanctioned bridge to `ClientContract`. The proxy invokes it
|
|
7501
|
+
* with the proxy as `this` (`Reflect.get` with receiver), so the returned reference
|
|
7502
|
+
* keeps the model accessors.
|
|
7503
|
+
*/
|
|
7504
|
+
get $contract() {
|
|
7505
|
+
return this;
|
|
7506
|
+
}
|
|
7459
7507
|
$setOptions(options) {
|
|
7460
7508
|
const newClient = new ClientImpl(this.schema, options, this);
|
|
7461
7509
|
newClient.inputValidator = new InputValidator(newClient, { enabled: newClient.$options.validateInput !== false });
|