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

Commit 96629ed

Browse files
authored
fix(type-check): try improving type checking performance (#475)
* fix(type-check): try improving type checking performance * update * update * update * revert to ZenStackPromise * update promise
1 parent 583e77e commit 96629ed

File tree

31 files changed

+408
-454
lines changed

31 files changed

+408
-454
lines changed

packages/common-helpers/src/zip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Zips two arrays into an array of tuples.
33
*/
4-
export function zip<T, U>(arr1: T[], arr2: U[]): Array<[T, U]> {
4+
export function zip<T, U>(arr1: readonly T[], arr2: readonly U[]): Array<[T, U]> {
55
const length = Math.min(arr1.length, arr2.length);
66
const result: Array<[T, U]> = [];
77
for (let i = 0; i < length; i++) {

packages/orm/src/client/crud-types.ts

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ import type {
1212
GetEnum,
1313
GetEnums,
1414
GetModel,
15-
GetModelDiscriminator,
1615
GetModelField,
1716
GetModelFields,
1817
GetModelFieldType,
1918
GetModels,
20-
GetSubModels,
2119
GetTypeDefField,
2220
GetTypeDefFields,
2321
GetTypeDefFieldType,
2422
GetTypeDefs,
25-
IsDelegateModel,
2623
ModelFieldIsOptional,
2724
NonRelationFields,
2825
RelationFields,
@@ -62,20 +59,26 @@ export type DefaultModelResult<
6259
Optional = false,
6360
Array = false,
6461
> = WrapType<
65-
IsDelegateModel<Schema, Model> extends true
66-
? // delegate model's selection result is a union of all sub-models
67-
DelegateUnionResult<Schema, Model, Options, GetSubModels<Schema, Model>, Omit>
68-
: {
69-
[Key in NonRelationFields<Schema, Model> as ShouldOmitField<
70-
Schema,
71-
Model,
72-
Options,
73-
Key,
74-
Omit
75-
> extends true
76-
? never
77-
: Key]: MapModelFieldType<Schema, Model, Key>;
78-
},
62+
{
63+
[Key in NonRelationFields<Schema, Model> as ShouldOmitField<Schema, Model, Options, Key, Omit> extends true
64+
? never
65+
: Key]: MapModelFieldType<Schema, Model, Key>;
66+
},
67+
// TODO: revisit how to efficiently implement discriminated sub model types
68+
// IsDelegateModel<Schema, Model> extends true
69+
// ? // delegate model's selection result is a union of all sub-models
70+
// DelegateUnionResult<Schema, Model, Options, GetSubModels<Schema, Model>, Omit>
71+
// : {
72+
// [Key in NonRelationFields<Schema, Model> as ShouldOmitField<
73+
// Schema,
74+
// Model,
75+
// Options,
76+
// Key,
77+
// Omit
78+
// > extends true
79+
// ? never
80+
// : Key]: MapModelFieldType<Schema, Model, Key>;
81+
// },
7982
Optional,
8083
Array
8184
>;
@@ -120,15 +123,15 @@ type SchemaLevelOmit<
120123
Field extends GetModelFields<Schema, Model>,
121124
> = GetModelField<Schema, Model, Field>['omit'] extends true ? true : false;
122125

123-
type DelegateUnionResult<
124-
Schema extends SchemaDef,
125-
Model extends GetModels<Schema>,
126-
Options extends ClientOptions<Schema>,
127-
SubModel extends GetModels<Schema>,
128-
Omit = undefined,
129-
> = SubModel extends string // typescript union distribution
130-
? DefaultModelResult<Schema, SubModel, Options, Omit> & { [K in GetModelDiscriminator<Schema, Model>]: SubModel } // fixate discriminated field
131-
: never;
126+
// type DelegateUnionResult<
127+
// Schema extends SchemaDef,
128+
// Model extends GetModels<Schema>,
129+
// Options extends ClientOptions<Schema>,
130+
// SubModel extends GetModels<Schema>,
131+
// Omit = undefined,
132+
// > = SubModel extends string // typescript union distribution
133+
// ? DefaultModelResult<Schema, SubModel, Options, Omit> & { [K in GetModelDiscriminator<Schema, Model>]: SubModel } // fixate discriminated field
134+
// : never;
132135

133136
type ModelSelectResult<
134137
Schema extends SchemaDef,
@@ -1029,7 +1032,7 @@ type OppositeRelationFields<
10291032
Model extends GetModels<Schema>,
10301033
Field extends GetModelFields<Schema, Model>,
10311034
Opposite = OppositeRelation<Schema, Model, Field>,
1032-
> = Opposite extends RelationInfo ? (Opposite['fields'] extends string[] ? Opposite['fields'] : []) : [];
1035+
> = Opposite extends RelationInfo ? (Opposite['fields'] extends readonly string[] ? Opposite['fields'] : []) : [];
10331036

10341037
type OppositeRelationAndFK<
10351038
Schema extends SchemaDef,
@@ -1083,21 +1086,16 @@ export type FindArgs<
10831086
Model extends GetModels<Schema>,
10841087
Collection extends boolean,
10851088
AllowFilter extends boolean = true,
1086-
> =
1087-
ProviderSupportsDistinct<Schema> extends true
1088-
? (Collection extends true
1089-
? SortAndTakeArgs<Schema, Model> & {
1090-
/**
1091-
* Distinct fields
1092-
*/
1093-
distinct?: OrArray<NonRelationFields<Schema, Model>>;
1094-
}
1095-
: {}) &
1096-
(AllowFilter extends true ? FilterArgs<Schema, Model> : {}) &
1097-
SelectIncludeOmit<Schema, Model, Collection>
1098-
: (Collection extends true ? SortAndTakeArgs<Schema, Model> : {}) &
1099-
(AllowFilter extends true ? FilterArgs<Schema, Model> : {}) &
1100-
SelectIncludeOmit<Schema, Model, Collection>;
1089+
> = (Collection extends true
1090+
? SortAndTakeArgs<Schema, Model> & {
1091+
/**
1092+
* Distinct fields
1093+
*/
1094+
distinct?: OrArray<NonRelationFields<Schema, Model>>;
1095+
}
1096+
: {}) &
1097+
(AllowFilter extends true ? FilterArgs<Schema, Model> : {}) &
1098+
SelectIncludeOmit<Schema, Model, Collection>;
11011099

11021100
export type FindManyArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = FindArgs<Schema, Model, true>;
11031101

@@ -2002,7 +2000,7 @@ type NestedDeleteManyInput<
20022000

20032001
type NonOwnedRelationFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = keyof {
20042002
[Key in RelationFields<Schema, Model> as GetModelField<Schema, Model, Key>['relation'] extends {
2005-
references: unknown[];
2003+
references: readonly unknown[];
20062004
}
20072005
? never
20082006
: Key]: true;
@@ -2014,8 +2012,8 @@ type HasToManyRelations<Schema extends SchemaDef, Model extends GetModels<Schema
20142012
? false
20152013
: true;
20162014

2017-
type ProviderSupportsDistinct<Schema extends SchemaDef> = Schema['provider']['type'] extends 'postgresql'
2018-
? true
2019-
: false;
2015+
// type ProviderSupportsDistinct<Schema extends SchemaDef> = Schema['provider']['type'] extends 'postgresql'
2016+
// ? true
2017+
// : false;
20202018

20212019
// #endregion

packages/orm/src/client/crud/operations/base.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
250250
data: any,
251251
fromRelation?: FromRelationContext,
252252
creatingForDelegate = false,
253-
returnFields?: string[],
253+
returnFields?: readonly string[],
254254
): Promise<unknown> {
255255
const modelDef = this.requireModel(model);
256256

@@ -662,7 +662,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
662662
input: { data: any; skipDuplicates?: boolean },
663663
returnData: ReturnData,
664664
fromRelation?: FromRelationContext,
665-
fieldsToReturn?: string[],
665+
fieldsToReturn?: readonly string[],
666666
): Promise<Result> {
667667
if (!input.data || (Array.isArray(input.data) && input.data.length === 0)) {
668668
// nothing todo
@@ -901,7 +901,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
901901
fromRelation?: FromRelationContext,
902902
allowRelationUpdate = true,
903903
throwIfNotFound = true,
904-
fieldsToReturn?: string[],
904+
fieldsToReturn?: readonly string[],
905905
): Promise<unknown> {
906906
if (!data || typeof data !== 'object') {
907907
throw createInvalidInputError('data must be an object');
@@ -1207,7 +1207,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
12071207
limit: number | undefined,
12081208
returnData: ReturnData,
12091209
filterModel?: string,
1210-
fieldsToReturn?: string[],
1210+
fieldsToReturn?: readonly string[],
12111211
): Promise<Result> {
12121212
if (typeof data !== 'object') {
12131213
throw createInvalidInputError('data must be an object');
@@ -1923,7 +1923,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
19231923
where: any,
19241924
limit?: number,
19251925
filterModel?: string,
1926-
fieldsToReturn?: string[],
1926+
fieldsToReturn?: readonly string[],
19271927
): Promise<QueryResult<unknown>> {
19281928
filterModel ??= model;
19291929

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export class InputValidator<Schema extends SchemaDef> {
297297
return result;
298298
}
299299

300-
private makeScalarSchema(type: string, attributes?: AttributeApplication[]) {
300+
private makeScalarSchema(type: string, attributes?: readonly AttributeApplication[]) {
301301
if (this.schema.typeDefs && type in this.schema.typeDefs) {
302302
return this.makeTypeDefSchema(type);
303303
} else if (this.schema.enums && type in this.schema.enums) {

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ function getArgValue<T extends string | number | boolean>(expr: Expression | und
2222
return expr.value as T;
2323
}
2424

25-
export function addStringValidation(schema: z.ZodString, attributes: AttributeApplication[] | undefined): z.ZodSchema {
25+
export function addStringValidation(
26+
schema: z.ZodString,
27+
attributes: readonly AttributeApplication[] | undefined,
28+
): z.ZodSchema {
2629
if (!attributes || attributes.length === 0) {
2730
return schema;
2831
}
@@ -86,7 +89,10 @@ export function addStringValidation(schema: z.ZodString, attributes: AttributeAp
8689
return result;
8790
}
8891

89-
export function addNumberValidation(schema: z.ZodNumber, attributes: AttributeApplication[] | undefined): z.ZodSchema {
92+
export function addNumberValidation(
93+
schema: z.ZodNumber,
94+
attributes: readonly AttributeApplication[] | undefined,
95+
): z.ZodSchema {
9096
if (!attributes || attributes.length === 0) {
9197
return schema;
9298
}
@@ -114,7 +120,10 @@ export function addNumberValidation(schema: z.ZodNumber, attributes: AttributeAp
114120
return result;
115121
}
116122

117-
export function addBigIntValidation(schema: z.ZodBigInt, attributes: AttributeApplication[] | undefined): z.ZodSchema {
123+
export function addBigIntValidation(
124+
schema: z.ZodBigInt,
125+
attributes: readonly AttributeApplication[] | undefined,
126+
): z.ZodSchema {
118127
if (!attributes || attributes.length === 0) {
119128
return schema;
120129
}
@@ -145,7 +154,7 @@ export function addBigIntValidation(schema: z.ZodBigInt, attributes: AttributeAp
145154

146155
export function addDecimalValidation(
147156
schema: z.ZodType<Decimal> | z.ZodString,
148-
attributes: AttributeApplication[] | undefined,
157+
attributes: readonly AttributeApplication[] | undefined,
149158
addExtraValidation: boolean,
150159
): z.ZodSchema {
151160
let result: z.ZodSchema = schema;
@@ -224,7 +233,7 @@ export function addDecimalValidation(
224233

225234
export function addListValidation(
226235
schema: z.ZodArray<any>,
227-
attributes: AttributeApplication[] | undefined,
236+
attributes: readonly AttributeApplication[] | undefined,
228237
): z.ZodSchema {
229238
if (!attributes || attributes.length === 0) {
230239
return schema;
@@ -248,7 +257,10 @@ export function addListValidation(
248257
return result;
249258
}
250259

251-
export function addCustomValidation(schema: z.ZodSchema, attributes: AttributeApplication[] | undefined): z.ZodSchema {
260+
export function addCustomValidation(
261+
schema: z.ZodSchema,
262+
attributes: readonly AttributeApplication[] | undefined,
263+
): z.ZodSchema {
252264
const attrs = attributes?.filter((a) => a.name === '@@validate');
253265
if (!attrs || attrs.length === 0) {
254266
return schema;

packages/orm/src/client/helpers/schema-db-pusher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ export class SchemaDbPusher<Schema extends SchemaDef> {
115115
const baseModelDef = requireModel(this.schema, modelDef.baseModel);
116116
table = table.addForeignKeyConstraint(
117117
`fk_${modelDef.baseModel}_delegate`,
118-
baseModelDef.idFields,
118+
baseModelDef.idFields as string[],
119119
modelDef.baseModel,
120-
baseModelDef.idFields,
120+
baseModelDef.idFields as string[],
121121
(cb) => cb.onDelete('cascade').onUpdate('cascade'),
122122
);
123123
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* Extract fields from an object.
33
*/
4-
export function extractFields(obj: any, fields: string[]) {
4+
export function extractFields(obj: any, fields: readonly string[]) {
55
return Object.fromEntries(Object.entries(obj).filter(([key]) => fields.includes(key)));
66
}
77

88
/**
99
* Create an object with fields as keys and true values.
1010
*/
11-
export function fieldsToSelectObject(fields: string[]): Record<string, boolean> {
11+
export function fieldsToSelectObject(fields: readonly string[]): Record<string, boolean> {
1212
return Object.fromEntries(fields.map((f) => [f, true]));
1313
}

0 commit comments

Comments
 (0)