Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit d0826ea

Browse files
committed
update
1 parent 1a038be commit d0826ea

2 files changed

Lines changed: 19 additions & 40 deletions

File tree

packages/orm/src/client/crud/validator/cache-decorator.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import stableStringify from 'json-stable-stringify';
22

3-
interface CacheOptions {
4-
/**
5-
* Instance property names to include in the cache key.
6-
* Useful when cache should be invalidated based on instance state.
7-
*/
8-
includeProperties?: string[];
9-
}
10-
113
/**
124
* Method decorator that caches the return value based on method name and arguments.
135
*
146
* Requirements:
157
* - Class must have a `getCache(key: string)` method
168
* - Class must have a `setCache(key: string, value: any)` method
179
*/
18-
export function cache(options: CacheOptions = {}) {
10+
export function cache() {
1911
return function (_target: any, propertyKey: string, descriptor: PropertyDescriptor) {
2012
const originalMethod = descriptor.value;
2113

@@ -32,13 +24,6 @@ export function cache(options: CacheOptions = {}) {
3224
...args,
3325
};
3426

35-
// Include specified instance properties
36-
if (options.includeProperties) {
37-
for (const prop of options.includeProperties) {
38-
cacheKeyObj['$' + prop] = this[prop];
39-
}
40-
}
41-
4227
// Generate stable string key
4328
const cacheKey = stableStringify(cacheKeyObj)!;
4429

packages/orm/src/client/crud/validator/index.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ import {
6565

6666
type GetSchemaFunc<Schema extends SchemaDef> = (model: GetModels<Schema>) => ZodType;
6767

68-
/**
69-
* Helper decorator that caches schema builders with class's state included
70-
* as part of the key (here the `extraValidationsEnabled` property).
71-
*/
72-
const cacheWithState = () => cache({ includeProperties: ['extraValidationsEnabled'] });
73-
7468
export class InputValidator<Schema extends SchemaDef> {
7569
private readonly schemaCache = new Map<string, ZodType>();
7670

@@ -247,7 +241,7 @@ export class InputValidator<Schema extends SchemaDef> {
247241
throw createInvalidInputError('Missing procedure arguments', `$procs.${proc}`);
248242
}
249243

250-
if (typeof input !== 'object') {
244+
if (typeof input !== 'object' || input === null || Array.isArray(input)) {
251245
throw createInvalidInputError('Procedure input must be an object', `$procs.${proc}`);
252246
}
253247

@@ -420,7 +414,7 @@ export class InputValidator<Schema extends SchemaDef> {
420414

421415
// #region Find
422416

423-
@cacheWithState()
417+
@cache()
424418
private makeFindSchema(model: string, operation: CoreCrudOperations) {
425419
const fields: Record<string, z.ZodSchema> = {};
426420
const unique = operation === 'findUnique';
@@ -459,7 +453,7 @@ export class InputValidator<Schema extends SchemaDef> {
459453
return result;
460454
}
461455

462-
@cacheWithState()
456+
@cache()
463457
private makeExistsSchema(model: string) {
464458
const baseSchema = z.strictObject({
465459
where: this.makeWhereSchema(model, false).optional(),
@@ -513,7 +507,7 @@ export class InputValidator<Schema extends SchemaDef> {
513507
return z.enum(Object.keys(enumDef.values) as [string, ...string[]]);
514508
}
515509

516-
@cacheWithState()
510+
@cache()
517511
private makeTypeDefSchema(type: string): z.ZodType {
518512
const typeDef = getTypeDef(this.schema, type);
519513
invariant(typeDef, `Type definition "${type}" not found in schema`);
@@ -1199,7 +1193,7 @@ export class InputValidator<Schema extends SchemaDef> {
11991193

12001194
// #region Create
12011195

1202-
@cacheWithState()
1196+
@cache()
12031197
private makeCreateSchema(model: string) {
12041198
const dataSchema = this.makeCreateDataSchema(model, false);
12051199
const baseSchema = z.strictObject({
@@ -1214,12 +1208,12 @@ export class InputValidator<Schema extends SchemaDef> {
12141208
return schema;
12151209
}
12161210

1217-
@cacheWithState()
1211+
@cache()
12181212
private makeCreateManySchema(model: string) {
12191213
return this.mergePluginArgsSchema(this.makeCreateManyDataSchema(model, []), 'createMany').optional();
12201214
}
12211215

1222-
@cacheWithState()
1216+
@cache()
12231217
private makeCreateManyAndReturnSchema(model: string) {
12241218
const base = this.makeCreateManyDataSchema(model, []);
12251219
let result: ZodObject = base.extend({
@@ -1230,7 +1224,7 @@ export class InputValidator<Schema extends SchemaDef> {
12301224
return this.refineForSelectOmitMutuallyExclusive(result).optional();
12311225
}
12321226

1233-
@cacheWithState()
1227+
@cache()
12341228
private makeCreateDataSchema(
12351229
model: string,
12361230
canBeArray: boolean,
@@ -1507,7 +1501,7 @@ export class InputValidator<Schema extends SchemaDef> {
15071501

15081502
// #region Update
15091503

1510-
@cacheWithState()
1504+
@cache()
15111505
private makeUpdateSchema(model: string) {
15121506
const baseSchema = z.strictObject({
15131507
where: this.makeWhereSchema(model, true),
@@ -1522,7 +1516,7 @@ export class InputValidator<Schema extends SchemaDef> {
15221516
return schema;
15231517
}
15241518

1525-
@cacheWithState()
1519+
@cache()
15261520
private makeUpdateManySchema(model: string) {
15271521
return this.mergePluginArgsSchema(
15281522
z.strictObject({
@@ -1534,7 +1528,7 @@ export class InputValidator<Schema extends SchemaDef> {
15341528
);
15351529
}
15361530

1537-
@cacheWithState()
1531+
@cache()
15381532
private makeUpdateManyAndReturnSchema(model: string) {
15391533
// plugin extended args schema is merged in `makeUpdateManySchema`
15401534
const baseSchema: ZodObject = this.makeUpdateManySchema(model);
@@ -1546,7 +1540,7 @@ export class InputValidator<Schema extends SchemaDef> {
15461540
return schema;
15471541
}
15481542

1549-
@cacheWithState()
1543+
@cache()
15501544
private makeUpsertSchema(model: string) {
15511545
const baseSchema = z.strictObject({
15521546
where: this.makeWhereSchema(model, true),
@@ -1562,7 +1556,7 @@ export class InputValidator<Schema extends SchemaDef> {
15621556
return schema;
15631557
}
15641558

1565-
@cacheWithState()
1559+
@cache()
15661560
private makeUpdateDataSchema(model: string, withoutFields: string[] = [], withoutRelationFields = false) {
15671561
// Normalize array argument for consistent cache keys
15681562
withoutFields = [...withoutFields].sort();
@@ -1676,7 +1670,7 @@ export class InputValidator<Schema extends SchemaDef> {
16761670

16771671
// #region Delete
16781672

1679-
@cacheWithState()
1673+
@cache()
16801674
private makeDeleteSchema(model: GetModels<Schema>) {
16811675
const baseSchema = z.strictObject({
16821676
where: this.makeWhereSchema(model, true),
@@ -1690,7 +1684,7 @@ export class InputValidator<Schema extends SchemaDef> {
16901684
return schema;
16911685
}
16921686

1693-
@cacheWithState()
1687+
@cache()
16941688
private makeDeleteManySchema(model: GetModels<Schema>) {
16951689
return this.mergePluginArgsSchema(
16961690
z.strictObject({
@@ -1705,7 +1699,7 @@ export class InputValidator<Schema extends SchemaDef> {
17051699

17061700
// #region Count
17071701

1708-
@cacheWithState()
1702+
@cache()
17091703
makeCountSchema(model: GetModels<Schema>) {
17101704
return this.mergePluginArgsSchema(
17111705
z.strictObject({
@@ -1741,7 +1735,7 @@ export class InputValidator<Schema extends SchemaDef> {
17411735

17421736
// #region Aggregate
17431737

1744-
@cacheWithState()
1738+
@cache()
17451739
makeAggregateSchema(model: GetModels<Schema>) {
17461740
return this.mergePluginArgsSchema(
17471741
z.strictObject({
@@ -1793,7 +1787,7 @@ export class InputValidator<Schema extends SchemaDef> {
17931787
);
17941788
}
17951789

1796-
@cacheWithState()
1790+
@cache()
17971791
private makeGroupBySchema(model: GetModels<Schema>) {
17981792
const modelDef = requireModel(this.schema, model);
17991793
const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);

0 commit comments

Comments
 (0)