This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathoptions.ts
More file actions
156 lines (135 loc) · 4.53 KB
/
options.ts
File metadata and controls
156 lines (135 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type { Dialect, Expression, ExpressionBuilder, KyselyConfig } from 'kysely';
import type { GetModel, GetModelFields, GetModels, ProcedureDef, ScalarFields, SchemaDef } from '../schema';
import type { PrependParameter } from '../utils/type-utils';
import type { ClientContract, CRUD_EXT } from './contract';
import type { GetProcedureNames, ProcedureHandlerFunc } from './crud-types';
import type { BaseCrudDialect } from './crud/dialects/base-dialect';
import type { AnyPlugin } from './plugin';
import type { ToKyselySchema } from './query-builder';
export type ZModelFunctionContext<Schema extends SchemaDef> = {
/**
* ZenStack client instance
*/
client: ClientContract<Schema>;
/**
* Database dialect
*/
dialect: BaseCrudDialect<Schema>;
/**
* The containing model name
*/
model: GetModels<Schema>;
/**
* The alias name that can be used to refer to the containing model
*/
modelAlias: string;
/**
* The CRUD operation being performed
*/
operation: CRUD_EXT;
};
export type ZModelFunction<Schema extends SchemaDef> = (
eb: ExpressionBuilder<ToKyselySchema<Schema>, keyof ToKyselySchema<Schema>>,
args: Expression<any>[],
context: ZModelFunctionContext<Schema>,
) => Expression<unknown>;
/**
* ZenStack client options.
*/
export type ClientOptions<Schema extends SchemaDef> = {
/**
* Kysely dialect.
*/
dialect: Dialect;
/**
* Custom function definitions.
*
* @private
*/
functions?: Record<string, ZModelFunction<Schema>>;
/**
* Plugins.
*/
plugins?: AnyPlugin[];
/**
* Logging configuration.
*/
log?: KyselyConfig['log'];
/**
* Whether to automatically fix timezone for `DateTime` fields returned by node-pg. Defaults
* to `true`.
*
* Node-pg has a terrible quirk that it interprets the date value as local timezone (as a
* `Date` object) although for `DateTime` field the data in DB is stored in UTC.
* @see https://github.com/brianc/node-postgres/issues/429
*/
fixPostgresTimezone?: boolean;
/**
* Whether to enable input validations expressed with attributes like `@email`, `@regex`,
* `@@validate`, etc. Defaults to `true`.
*/
validateInput?: boolean;
/**
* Options for omitting fields in ORM query results.
*/
omit?: OmitConfig<Schema>;
/**
* Whether to allow overriding omit settings at query time. Defaults to `true`. When set to
* `false`, an `omit` clause that sets field to `false` (not omitting) will trigger a validation
* error.
*/
allowQueryTimeOmitOverride?: boolean;
} & (HasComputedFields<Schema> extends true
? {
/**
* Computed field definitions.
*/
computedFields: ComputedFieldsOptions<Schema>;
}
: {}) &
(HasProcedures<Schema> extends true
? {
/**
* Custom procedure definitions.
*/
procedures: ProceduresOptions<Schema>;
}
: {});
/**
* Config for omitting fields in ORM query results.
*/
export type OmitConfig<Schema extends SchemaDef> = {
[Model in GetModels<Schema>]?: {
[Field in GetModelFields<Schema, Model> as Field extends ScalarFields<Schema, Model> ? Field : never]?: boolean;
};
};
export type ComputedFieldsOptions<Schema extends SchemaDef> = {
[Model in GetModels<Schema> as 'computedFields' extends keyof GetModel<Schema, Model> ? Model : never]: {
[Field in keyof Schema['models'][Model]['computedFields']]: PrependParameter<
ExpressionBuilder<ToKyselySchema<Schema>, Model>,
Schema['models'][Model]['computedFields'][Field]
>;
};
};
export type HasComputedFields<Schema extends SchemaDef> =
string extends GetModels<Schema> ? false : keyof ComputedFieldsOptions<Schema> extends never ? false : true;
export type ProceduresOptions<Schema extends SchemaDef> = Schema extends {
procedures: Record<string, ProcedureDef>;
}
? {
[Key in GetProcedureNames<Schema>]: ProcedureHandlerFunc<Schema, Key>;
}
: {};
export type HasProcedures<Schema extends SchemaDef> = Schema extends {
procedures: Record<string, ProcedureDef>;
}
? true
: false;
/**
* Subset of client options relevant to query operations.
*/
export type QueryOptions<Schema extends SchemaDef> = Pick<ClientOptions<Schema>, 'omit'>;
/**
* Extract QueryOptions from ClientOptions
*/
export type ToQueryOptions<T extends ClientOptions<any>> = Pick<T, 'omit'>;