@zenstackhq/orm 3.4.6 → 3.5.0-beta.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.d.cts CHANGED
@@ -586,7 +586,8 @@ type ClientOptions<Schema extends SchemaDef> = QueryOptions<Schema> & {
586
586
  */
587
587
  plugins?: AnyPlugin[];
588
588
  /**
589
- * Logging configuration.
589
+ * Logging configuration. Extends Kysely's log config with a `'warning'` level
590
+ * for ZenStack-specific diagnostics (e.g., slow query warnings).
590
591
  */
591
592
  log?: KyselyConfig['log'];
592
593
  /**
@@ -599,8 +600,11 @@ type ClientOptions<Schema extends SchemaDef> = QueryOptions<Schema> & {
599
600
  */
600
601
  fixPostgresTimezone?: boolean;
601
602
  /**
602
- * Whether to enable input validations expressed with attributes like `@email`, `@regex`,
603
- * `@@validate`, etc. Defaults to `true`.
603
+ * Whether to enable query args validation. Defaults to `true`.
604
+ *
605
+ * **USE WITH CAUTION**, as setting it to `false` will allow malformed input to pass through, causing
606
+ * incorrect SQL generation or runtime errors. If you use validation attributes like `@email`, `@regex`,
607
+ * etc., in ZModel, they will be ignored too.
604
608
  */
605
609
  validateInput?: boolean;
606
610
  /**
@@ -612,9 +616,23 @@ type ClientOptions<Schema extends SchemaDef> = QueryOptions<Schema> & {
612
616
  */
613
617
  useCompactAliasNames?: boolean;
614
618
  /**
615
- * Whether to skip validation for computed fields.
619
+ * Whether to skip validation for whether all computed fields are properly defined.
616
620
  */
617
621
  skipValidationForComputedFields?: boolean;
622
+ /**
623
+ * Diagnostics related options.
624
+ */
625
+ diagnostics?: {
626
+ /**
627
+ * Threshold in milliseconds for determining slow queries. If not specified, no query will be considered slow.
628
+ */
629
+ slowQueryThresholdMs?: number;
630
+ /**
631
+ * Maximum number of slow query records to keep in memory. Defaults to `100`. When the number is exceeded, the
632
+ * entry with the lowest duration will be removed. Set to `Infinity` to keep unlimited records.
633
+ */
634
+ slowQueryMaxRecords?: number;
635
+ };
618
636
  } & (HasComputedFields<Schema> extends true ? {
619
637
  /**
620
638
  * Computed field definitions.
@@ -820,10 +838,17 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
820
838
  protected abstract buildOrderByField(query: SelectQueryBuilder<any, any, any>, field: Expression<unknown>, sort: SortOrder, nulls: 'first' | 'last'): SelectQueryBuilder<any, any, any>;
821
839
  }
822
840
 
841
+ type InputValidatorOptions = {
842
+ /**
843
+ * Whether validation is enabled. Defaults to `true`.
844
+ */
845
+ enabled?: boolean;
846
+ };
823
847
  declare class InputValidator<Schema extends SchemaDef> {
824
848
  private readonly client;
825
849
  readonly zodFactory: ZodSchemaFactory<Schema>;
826
- constructor(client: ClientContract<Schema>);
850
+ private readonly enabled;
851
+ constructor(client: ClientContract<Schema>, options?: InputValidatorOptions);
827
852
  validateFindArgs(model: GetModels<Schema>, args: unknown, operation: 'findFirst' | 'findUnique' | 'findMany'): FindArgs<Schema, GetModels<Schema>, any, true> | undefined;
828
853
  validateExistsArgs(model: GetModels<Schema>, args: unknown): ExistsArgs<Schema, GetModels<Schema>, any> | undefined;
829
854
  validateCreateArgs(model: GetModels<Schema>, args: unknown): CreateArgs<Schema, GetModels<Schema>, any>;
@@ -1999,6 +2024,50 @@ type ProviderSupportsDistinct<Schema extends SchemaDef> = Schema['provider']['ty
1999
2024
  */
2000
2025
  type ExtractExtQueryArgs<ExtQueryArgs, Operation extends CoreCrudOperations> = (Operation extends keyof ExtQueryArgs ? ExtQueryArgs[Operation] : {}) & ('$create' extends keyof ExtQueryArgs ? Operation extends CoreCreateOperations ? ExtQueryArgs['$create'] : {} : {}) & ('$read' extends keyof ExtQueryArgs ? (Operation extends CoreReadOperations ? ExtQueryArgs['$read'] : {}) : {}) & ('$update' extends keyof ExtQueryArgs ? Operation extends CoreUpdateOperations ? ExtQueryArgs['$update'] : {} : {}) & ('$delete' extends keyof ExtQueryArgs ? Operation extends CoreDeleteOperations ? ExtQueryArgs['$delete'] : {} : {}) & ('$all' extends keyof ExtQueryArgs ? ExtQueryArgs['$all'] : {});
2001
2026
 
2027
+ /**
2028
+ * Zod schema cache statistics.
2029
+ */
2030
+ interface ZodCacheStats {
2031
+ /**
2032
+ * Number of cached Zod schemas.
2033
+ */
2034
+ size: number;
2035
+ /**
2036
+ * Keys of the cached Zod schemas.
2037
+ */
2038
+ keys: string[];
2039
+ }
2040
+ /**
2041
+ * Information about a query, used for diagnostics.
2042
+ */
2043
+ interface QueryInfo {
2044
+ /**
2045
+ * Time when the query started.
2046
+ */
2047
+ startedAt: Date;
2048
+ /**
2049
+ * Duration of the query in milliseconds.
2050
+ */
2051
+ durationMs: number;
2052
+ /**
2053
+ * SQL statement of the query.
2054
+ */
2055
+ sql: string;
2056
+ }
2057
+ /**
2058
+ * ZenStackClient diagnostics.
2059
+ */
2060
+ interface Diagnostics {
2061
+ /**
2062
+ * Statistics about the Zod schemas (used for query args validation) cache.
2063
+ */
2064
+ zodCache: ZodCacheStats;
2065
+ /**
2066
+ * Slow queries.
2067
+ */
2068
+ slowQueries: QueryInfo[];
2069
+ }
2070
+
2002
2071
  /**
2003
2072
  * A promise that only executes when it's awaited or .then() is called.
2004
2073
  */
@@ -2084,8 +2153,7 @@ type ClientContract<Schema extends SchemaDef, Options extends ClientOptions<Sche
2084
2153
  */
2085
2154
  $setOptions<NewOptions extends ClientOptions<Schema>>(options: NewOptions): ClientContract<Schema, NewOptions, ExtQueryArgs, ExtClientMembers>;
2086
2155
  /**
2087
- * Returns a new client enabling/disabling input validations expressed with attributes like
2088
- * `@email`, `@regex`, `@@validate`, etc.
2156
+ * Returns a new client enabling/disabling query args validation.
2089
2157
  *
2090
2158
  * @deprecated Use {@link $setOptions} instead.
2091
2159
  */
@@ -2139,6 +2207,10 @@ type ClientContract<Schema extends SchemaDef, Options extends ClientOptions<Sche
2139
2207
  * @private
2140
2208
  */
2141
2209
  $pushSchema(): Promise<void>;
2210
+ /**
2211
+ * Returns diagnostics information such as cache and slow query statistics.
2212
+ */
2213
+ $diagnostics(): Promise<Diagnostics>;
2142
2214
  } & {
2143
2215
  [Key in GetSlicedModels<Schema, Options> as Uncapitalize<Key>]: ModelOperations<Schema, Key, Options, ExtQueryArgs>;
2144
2216
  } & ProcedureOperations<Schema, Options> & ExtClientMembers;
@@ -2739,15 +2811,18 @@ declare class ZodSchemaFactory<Schema extends SchemaDef, Options extends ClientO
2739
2811
  private readonly allFilterKinds;
2740
2812
  private readonly schema;
2741
2813
  private readonly options;
2814
+ private readonly extraValidationsEnabled;
2742
2815
  constructor(client: ClientContract<Schema, Options, ExtQueryArgs, any>);
2743
2816
  constructor(schema: Schema, options?: Options);
2744
2817
  private get plugins();
2745
- private get extraValidationsEnabled();
2746
2818
  private shouldIncludeRelations;
2747
2819
  private nextOptions;
2748
2820
  private getCache;
2749
2821
  private setCache;
2750
- private printCacheStats;
2822
+ get cacheStats(): {
2823
+ size: number;
2824
+ keys: string[];
2825
+ };
2751
2826
  makeFindUniqueSchema<Model extends GetModels<Schema>>(model: Model, options?: CreateSchemaOptions): ZodType<FindUniqueArgs<Schema, Model, Options, ExtQueryArgs>>;
2752
2827
  makeFindFirstSchema<Model extends GetModels<Schema>>(model: Model, options?: CreateSchemaOptions): ZodType<FindFirstArgs<Schema, Model, Options, ExtQueryArgs> | undefined>;
2753
2828
  makeFindManySchema<Model extends GetModels<Schema>>(model: Model, options?: CreateSchemaOptions): ZodType<FindManyArgs<Schema, Model, Options, ExtQueryArgs> | undefined>;
package/dist/index.d.ts CHANGED
@@ -586,7 +586,8 @@ type ClientOptions<Schema extends SchemaDef> = QueryOptions<Schema> & {
586
586
  */
587
587
  plugins?: AnyPlugin[];
588
588
  /**
589
- * Logging configuration.
589
+ * Logging configuration. Extends Kysely's log config with a `'warning'` level
590
+ * for ZenStack-specific diagnostics (e.g., slow query warnings).
590
591
  */
591
592
  log?: KyselyConfig['log'];
592
593
  /**
@@ -599,8 +600,11 @@ type ClientOptions<Schema extends SchemaDef> = QueryOptions<Schema> & {
599
600
  */
600
601
  fixPostgresTimezone?: boolean;
601
602
  /**
602
- * Whether to enable input validations expressed with attributes like `@email`, `@regex`,
603
- * `@@validate`, etc. Defaults to `true`.
603
+ * Whether to enable query args validation. Defaults to `true`.
604
+ *
605
+ * **USE WITH CAUTION**, as setting it to `false` will allow malformed input to pass through, causing
606
+ * incorrect SQL generation or runtime errors. If you use validation attributes like `@email`, `@regex`,
607
+ * etc., in ZModel, they will be ignored too.
604
608
  */
605
609
  validateInput?: boolean;
606
610
  /**
@@ -612,9 +616,23 @@ type ClientOptions<Schema extends SchemaDef> = QueryOptions<Schema> & {
612
616
  */
613
617
  useCompactAliasNames?: boolean;
614
618
  /**
615
- * Whether to skip validation for computed fields.
619
+ * Whether to skip validation for whether all computed fields are properly defined.
616
620
  */
617
621
  skipValidationForComputedFields?: boolean;
622
+ /**
623
+ * Diagnostics related options.
624
+ */
625
+ diagnostics?: {
626
+ /**
627
+ * Threshold in milliseconds for determining slow queries. If not specified, no query will be considered slow.
628
+ */
629
+ slowQueryThresholdMs?: number;
630
+ /**
631
+ * Maximum number of slow query records to keep in memory. Defaults to `100`. When the number is exceeded, the
632
+ * entry with the lowest duration will be removed. Set to `Infinity` to keep unlimited records.
633
+ */
634
+ slowQueryMaxRecords?: number;
635
+ };
618
636
  } & (HasComputedFields<Schema> extends true ? {
619
637
  /**
620
638
  * Computed field definitions.
@@ -820,10 +838,17 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
820
838
  protected abstract buildOrderByField(query: SelectQueryBuilder<any, any, any>, field: Expression<unknown>, sort: SortOrder, nulls: 'first' | 'last'): SelectQueryBuilder<any, any, any>;
821
839
  }
822
840
 
841
+ type InputValidatorOptions = {
842
+ /**
843
+ * Whether validation is enabled. Defaults to `true`.
844
+ */
845
+ enabled?: boolean;
846
+ };
823
847
  declare class InputValidator<Schema extends SchemaDef> {
824
848
  private readonly client;
825
849
  readonly zodFactory: ZodSchemaFactory<Schema>;
826
- constructor(client: ClientContract<Schema>);
850
+ private readonly enabled;
851
+ constructor(client: ClientContract<Schema>, options?: InputValidatorOptions);
827
852
  validateFindArgs(model: GetModels<Schema>, args: unknown, operation: 'findFirst' | 'findUnique' | 'findMany'): FindArgs<Schema, GetModels<Schema>, any, true> | undefined;
828
853
  validateExistsArgs(model: GetModels<Schema>, args: unknown): ExistsArgs<Schema, GetModels<Schema>, any> | undefined;
829
854
  validateCreateArgs(model: GetModels<Schema>, args: unknown): CreateArgs<Schema, GetModels<Schema>, any>;
@@ -1999,6 +2024,50 @@ type ProviderSupportsDistinct<Schema extends SchemaDef> = Schema['provider']['ty
1999
2024
  */
2000
2025
  type ExtractExtQueryArgs<ExtQueryArgs, Operation extends CoreCrudOperations> = (Operation extends keyof ExtQueryArgs ? ExtQueryArgs[Operation] : {}) & ('$create' extends keyof ExtQueryArgs ? Operation extends CoreCreateOperations ? ExtQueryArgs['$create'] : {} : {}) & ('$read' extends keyof ExtQueryArgs ? (Operation extends CoreReadOperations ? ExtQueryArgs['$read'] : {}) : {}) & ('$update' extends keyof ExtQueryArgs ? Operation extends CoreUpdateOperations ? ExtQueryArgs['$update'] : {} : {}) & ('$delete' extends keyof ExtQueryArgs ? Operation extends CoreDeleteOperations ? ExtQueryArgs['$delete'] : {} : {}) & ('$all' extends keyof ExtQueryArgs ? ExtQueryArgs['$all'] : {});
2001
2026
 
2027
+ /**
2028
+ * Zod schema cache statistics.
2029
+ */
2030
+ interface ZodCacheStats {
2031
+ /**
2032
+ * Number of cached Zod schemas.
2033
+ */
2034
+ size: number;
2035
+ /**
2036
+ * Keys of the cached Zod schemas.
2037
+ */
2038
+ keys: string[];
2039
+ }
2040
+ /**
2041
+ * Information about a query, used for diagnostics.
2042
+ */
2043
+ interface QueryInfo {
2044
+ /**
2045
+ * Time when the query started.
2046
+ */
2047
+ startedAt: Date;
2048
+ /**
2049
+ * Duration of the query in milliseconds.
2050
+ */
2051
+ durationMs: number;
2052
+ /**
2053
+ * SQL statement of the query.
2054
+ */
2055
+ sql: string;
2056
+ }
2057
+ /**
2058
+ * ZenStackClient diagnostics.
2059
+ */
2060
+ interface Diagnostics {
2061
+ /**
2062
+ * Statistics about the Zod schemas (used for query args validation) cache.
2063
+ */
2064
+ zodCache: ZodCacheStats;
2065
+ /**
2066
+ * Slow queries.
2067
+ */
2068
+ slowQueries: QueryInfo[];
2069
+ }
2070
+
2002
2071
  /**
2003
2072
  * A promise that only executes when it's awaited or .then() is called.
2004
2073
  */
@@ -2084,8 +2153,7 @@ type ClientContract<Schema extends SchemaDef, Options extends ClientOptions<Sche
2084
2153
  */
2085
2154
  $setOptions<NewOptions extends ClientOptions<Schema>>(options: NewOptions): ClientContract<Schema, NewOptions, ExtQueryArgs, ExtClientMembers>;
2086
2155
  /**
2087
- * Returns a new client enabling/disabling input validations expressed with attributes like
2088
- * `@email`, `@regex`, `@@validate`, etc.
2156
+ * Returns a new client enabling/disabling query args validation.
2089
2157
  *
2090
2158
  * @deprecated Use {@link $setOptions} instead.
2091
2159
  */
@@ -2139,6 +2207,10 @@ type ClientContract<Schema extends SchemaDef, Options extends ClientOptions<Sche
2139
2207
  * @private
2140
2208
  */
2141
2209
  $pushSchema(): Promise<void>;
2210
+ /**
2211
+ * Returns diagnostics information such as cache and slow query statistics.
2212
+ */
2213
+ $diagnostics(): Promise<Diagnostics>;
2142
2214
  } & {
2143
2215
  [Key in GetSlicedModels<Schema, Options> as Uncapitalize<Key>]: ModelOperations<Schema, Key, Options, ExtQueryArgs>;
2144
2216
  } & ProcedureOperations<Schema, Options> & ExtClientMembers;
@@ -2739,15 +2811,18 @@ declare class ZodSchemaFactory<Schema extends SchemaDef, Options extends ClientO
2739
2811
  private readonly allFilterKinds;
2740
2812
  private readonly schema;
2741
2813
  private readonly options;
2814
+ private readonly extraValidationsEnabled;
2742
2815
  constructor(client: ClientContract<Schema, Options, ExtQueryArgs, any>);
2743
2816
  constructor(schema: Schema, options?: Options);
2744
2817
  private get plugins();
2745
- private get extraValidationsEnabled();
2746
2818
  private shouldIncludeRelations;
2747
2819
  private nextOptions;
2748
2820
  private getCache;
2749
2821
  private setCache;
2750
- private printCacheStats;
2822
+ get cacheStats(): {
2823
+ size: number;
2824
+ keys: string[];
2825
+ };
2751
2826
  makeFindUniqueSchema<Model extends GetModels<Schema>>(model: Model, options?: CreateSchemaOptions): ZodType<FindUniqueArgs<Schema, Model, Options, ExtQueryArgs>>;
2752
2827
  makeFindFirstSchema<Model extends GetModels<Schema>>(model: Model, options?: CreateSchemaOptions): ZodType<FindFirstArgs<Schema, Model, Options, ExtQueryArgs> | undefined>;
2753
2828
  makeFindManySchema<Model extends GetModels<Schema>>(model: Model, options?: CreateSchemaOptions): ZodType<FindManyArgs<Schema, Model, Options, ExtQueryArgs> | undefined>;