UNPKG

15 kBTypeScriptView Raw
1/// <reference types="@fxjs/sql-query" />
2/// <reference path="_common.d.ts" />
3/// <reference path="Validators.d.ts" />
4/// <reference path="assoc.d.ts" />
5/// <reference path="patch.d.ts" />
6/// <reference path="hook.d.ts" />
7/// <reference path="instance.d.ts" />
8/// <reference path="settings.d.ts" />
9/// <reference path="query.d.ts" />
10
11declare namespace FxOrmModel {
12 type ModelInstanceConstructorOptions = (string | number | FxOrmInstance.InstanceDataPayload)[]
13
14 interface ModelInstanceConstructor {
15 (): FxOrmInstance.Instance;
16 new(): FxOrmInstance.Instance;
17 (...data: ModelInstanceConstructorOptions): FxOrmInstance.Instance;
18 new(...data: ModelInstanceConstructorOptions): FxOrmInstance.Instance;
19 }
20
21 type OrderListOrLimitOffer = number | string | string[]
22 interface Model extends ModelInstanceConstructor, ModelHooks, FxOrmSynchronous.SynchronizedModel {
23 name: string;
24 properties: FxOrmProperty.NormalizedPropertyHash;
25 settings: FxOrmSettings.SettingInstance;
26
27 table: string;
28 id: string[];
29 /* @nonenum */
30 uid: string;
31
32 caches: Class_LruCache;
33
34 keys: string[];
35
36 allProperties: FxOrmProperty.NormalizedPropertyHash
37
38 /* property operation :start */
39 addProperty: {
40 (
41 propIn: FxOrmProperty.NormalizedProperty, /* ModelPropertyDefinition */
42 options?: {
43 name?: string
44 klass?: FxOrmProperty.KlassType
45 } | false
46 ): FxOrmProperty.NormalizedProperty
47 }
48 /* property operation :end */
49
50 sync(callback?: FxOrmNS.GenericCallback<FxOrmSqlDDLSync.SyncResult>): Model;
51 drop(callback?: FxOrmNS.VoidCallback): Model;
52
53 /**
54 * methods used to add associations
55 */
56 /* association about api :start */
57 hasOne: {
58 (assoc_name: string, ext_model?: Model, assoc_options?: FxOrmAssociation.AssociationDefinitionOptions_HasOne): FxOrmModel.Model
59 }
60 hasMany: {
61 (assoc_name: string, ext_model: Model, assoc_options?: FxOrmAssociation.AssociationDefinitionOptions_HasMany): FxOrmModel.Model
62 (assoc_name: string, ext_model: Model, assoc_props: ModelPropertyDefinitionHash, assoc_options?: FxOrmAssociation.AssociationDefinitionOptions_HasMany): FxOrmModel.Model
63 }
64 extendsTo: {
65 (name: string, properties: ModelPropertyDefinitionHash, assoc_options?: FxOrmAssociation.AssociationDefinitionOptions_ExtendsTo): Model
66 };
67
68 associations: {
69 [k: string]: {
70 type: 'hasOne'
71 association: FxOrmAssociation.InstanceAssociationItem_HasOne
72 } | {
73 type: 'hasMany'
74 association: FxOrmAssociation.InstanceAssociationItem_HasMany
75 } | {
76 type: 'extendsTo'
77 association: FxOrmAssociation.InstanceAssociationItem_ExtendTos
78 }
79 }
80 findBy: {
81 <T = any>(
82 association_name: ModelFindByDescriptorItem['association_name'],
83 conditions?: ModelFindByDescriptorItem['conditions'],
84 options?: ModelFindByDescriptorItem['options'],
85 cb?: FxOrmNS.ExecutionCallback<T>
86 ): FxOrmQuery.IChainFind
87 <T = any>(
88 list: ModelFindByDescriptorItem[],
89 self_conditions: FxOrmModel.ModelQueryConditions__Find,
90 cb?: FxOrmNS.ExecutionCallback<T>
91 ): FxOrmQuery.IChainFind
92 }
93 /* association about api :end */
94
95 /* data operation api :start */
96 create: {
97 (data: FxOrmInstance.InstanceDataPayload, callback?: ModelMethodCallback__CreateItem): Model;
98 (data: FxOrmInstance.InstanceDataPayload, options?: ModelOptions__Create, callback?: ModelMethodCallback__CreateItem): Model;
99 }
100 clear: {
101 (...args: any[]): Model;
102 }
103 get: {
104 (...ids: any[]): Model; // this model is from its return
105 }
106
107 chain: {
108 (conditions?: FxOrmModel.ModelQueryConditions__Find, ...args: (FxOrmModel.ModelOptions__Find | OrderListOrLimitOffer)[]): FxOrmQuery.IChainFind;
109 }
110
111 find: {
112 (conditions?: ModelQueryConditions__Find): FxOrmQuery.IChainFind
113 (callback: ModelMethodCallback__Find): Model
114 (conditions: ModelQueryConditions__Find, callback: ModelMethodCallback__Find): Model
115
116 (conditions: ModelQueryConditions__Find, options: ModelOptions__Find): FxOrmQuery.IChainFind
117 (conditions: ModelQueryConditions__Find, options: ModelOptions__Find, callback: ModelMethodCallback__Find): Model
118
119 (conditions: ModelQueryConditions__Find, limit_order?: OrderListOrLimitOffer, limit_order2?: OrderListOrLimitOffer): FxOrmQuery.IChainFind
120
121 (conditions: ModelQueryConditions__Find, limit_order: OrderListOrLimitOffer, callback: ModelMethodCallback__Find): Model
122 (conditions: ModelQueryConditions__Find, limit_order: OrderListOrLimitOffer, limit_order2: OrderListOrLimitOffer, callback: ModelMethodCallback__Find): Model
123 }
124
125 all: Model['find']
126 where: Model['find']
127
128 /**
129 * not like other methods, you must provide callback to those methods
130 * - `one`
131 * - `count`
132 * - `exists`
133 *
134 * that's maybe due to their purpose: always return Model rather than IChainFind
135 */
136 one: {
137 (callback: ModelMethodCallback__Get): Model;
138 (conditions: ModelQueryConditions__Find, callback: ModelMethodCallback__Get): Model;
139 (conditions: ModelQueryConditions__Find, options: ModelOptions__Find, callback: ModelMethodCallback__Get): Model;
140 (conditions: ModelQueryConditions__Find, order: string[], callback: ModelMethodCallback__Get): Model;
141 (conditions: ModelQueryConditions__Find, limit: number, callback: ModelMethodCallback__Get): Model;
142 }
143
144 count: {
145 (callback: ModelMethodCallback__Count): Model;
146 (conditions: ModelQueryConditions__Find, callback: ModelMethodCallback__Count): Model;
147 }
148
149 exists: {
150 (...conditions: (FibOrmNS.IdType | FxSqlQuerySubQuery.SubQueryConditions | FxOrmNS.ExecutionCallback<boolean>)[]): FxOrmQuery.IChainFind
151 }
152
153 aggregate: {
154 (conditions: ModelQueryConditions__Find): FxOrmQuery.IAggregated;
155 (properties: string[]): FxOrmQuery.IAggregated;
156 (conditions: ModelQueryConditions__Find, properties: string[]): FxOrmQuery.IAggregated;
157 }
158 /* data operation api :end */
159
160 prependValidation: {
161 (key: string, validation: FibjsEnforce.IValidator): void
162 }
163
164 [property: string]: any;
165 }
166
167 type FindByListStyleFunctionArgs<T = any> = [
168 FxOrmModel.ModelFindByDescriptorItem[],
169 FxOrmModel.ModelQueryConditions__Find,
170 FxOrmModel.ModelOptions__Find,
171 FxOrmNS.ExecutionCallback<T>
172 ]
173
174 type FindByItemStyleFunctionArgs<T = any> = [
175 FxOrmModel.ModelFindByDescriptorItem['association_name'],
176 FxOrmModel.ModelFindByDescriptorItem['conditions'],
177 FxOrmModel.ModelFindByDescriptorItem['options'],
178 FxOrmNS.ExecutionCallback<T>
179 ]
180
181 type ModelConstructor = new (opts: ModelConstructorOptions) => Model
182 // interface ModelConstructor {
183 // (opts: ModelConstructorOptions): void
184 // prototype: Model
185 // }
186
187 interface ModelFindByDescriptorItem {
188 // association name
189 association_name: string,
190 // findby conditions
191 conditions?: ModelQueryConditions__Find,
192 // findby options
193 options?: FxOrmAssociation.ModelAssociationMethod__FindByOptions,
194
195 // extra where conditions fields for hasmany-assoc
196 join_where?: FxOrmModel.ModelQueryConditions__Find
197 // extra select fields for hasmany-assoc
198 extra_select?: string[]
199 }
200
201 interface ModelConstructorOptions {
202 name: string
203 db: FxOrmNS.ORM
204 settings: FxOrmSettings.SettingInstance
205 driver_name: string
206 driver: FxOrmDMLDriver.DMLDriver
207 table: string
208 properties: FxOrmProperty.NormalizedPropertyHash
209 __for_extension: boolean
210 indexes: string[]
211
212 identityCache: boolean
213 instanceCacheSize: number
214
215 keys: string[]
216 autoSave: boolean
217 autoFetch: boolean
218 autoFetchLimit: number
219 cascadeRemove: boolean
220 hooks: Hooks
221 methods: {[method_name: string]: Function}
222 validations: FxOrmValidators.IValidatorHash
223 ievents: FxOrmInstance.InstanceConstructorOptions['events']
224 }
225
226 interface ModelDefineOptions {
227 /**
228 * pririoty: table > collection
229 */
230 table?: ModelConstructorOptions['table']
231 collection?: ModelConstructorOptions['table']
232
233 /**
234 * @dirty would be deprecated
235 */
236 __for_extension?: ModelConstructorOptions['__for_extension']
237 indexes?: ModelConstructorOptions['indexes']
238 // keys composition, it's array-like
239 id?: ModelConstructorOptions['keys']
240 autoSave?: ModelConstructorOptions['autoSave']
241 autoFetch?: ModelConstructorOptions['autoFetch']
242 autoFetchLimit?: ModelConstructorOptions['autoFetchLimit']
243 hooks?: ModelConstructorOptions['hooks']
244 validations?: ModelConstructorOptions['validations']
245 methods?: { [name: string]: Function };
246 identityCache?: ModelConstructorOptions['identityCache']
247 cascadeRemove?: ModelConstructorOptions['cascadeRemove']
248 ievents?: ModelConstructorOptions['ievents']
249 useSelfSettings?: boolean
250
251 [extensibleProperty: string]: any;
252 }
253 type ModelOptions = ModelDefineOptions
254
255 interface Hooks {
256 beforeValidation?: FxOrmNS.Arraible<FxOrmHook.HookActionCallback>;
257 beforeCreate?: FxOrmNS.Arraible<FxOrmHook.HookActionCallback>;
258 afterCreate?: FxOrmNS.Arraible<FxOrmHook.HookResultCallback>;
259 beforeSave?: FxOrmNS.Arraible<FxOrmHook.HookActionCallback>;
260 afterSave?: FxOrmNS.Arraible<FxOrmHook.HookResultCallback>;
261 afterLoad?: FxOrmNS.Arraible<FxOrmHook.HookActionCallback>;
262 afterAutoFetch?: FxOrmNS.Arraible<FxOrmHook.HookActionCallback>;
263 beforeRemove?: FxOrmNS.Arraible<FxOrmHook.HookActionCallback>;
264 afterRemove?: FxOrmNS.Arraible<FxOrmHook.HookResultCallback>;
265 }
266 type keyofHooks = keyof Hooks
267
268 interface ModelHookPatchOptions extends FxOrmHook.HookPatchOptions {
269 }
270
271 interface ModelHooks {
272 beforeValidation?: {
273 (func: FxOrmHook.HookActionCallback, opts?: ModelHookPatchOptions): any
274 };
275 beforeCreate?: {
276 (func: FxOrmHook.HookActionCallback, opts?: ModelHookPatchOptions): any
277 };
278 afterCreate?: {
279 (func: FxOrmHook.HookActionCallback, opts?: ModelHookPatchOptions): any
280 };
281 beforeSave?: {
282 (func: FxOrmHook.HookActionCallback, opts?: ModelHookPatchOptions): any
283 };
284 afterSave?: {
285 (func: FxOrmHook.HookResultCallback, opts?: ModelHookPatchOptions): any
286 };
287 afterLoad?: {
288 (func: FxOrmHook.HookResultCallback, opts?: ModelHookPatchOptions): any
289 };
290 afterAutoFetch?: {
291 (func: FxOrmHook.HookActionCallback, opts?: ModelHookPatchOptions): any
292 };
293 beforeRemove?: {
294 (func: FxOrmHook.HookActionCallback, opts?: ModelHookPatchOptions): any
295 };
296 afterRemove?: {
297 (func: FxOrmHook.HookResultCallback, opts?: ModelHookPatchOptions): any
298 };
299 }
300
301 interface ModelPropertyDefinition extends FxOrmSqlDDLSync__Column.Property {
302 key?: boolean
303 klass?: FxOrmProperty.KlassType
304 alwaysValidate?: boolean
305 enumerable?: boolean
306 // whether lazyload property, if it is, it can be loaded only by its accessor
307 lazyload?: boolean
308 }
309
310 // @deprecated
311 type OrigDetailedModelProperty = FxOrmProperty.NormalizedProperty
312 type OrigDetailedModelPropertyHash = FxOrmProperty.NormalizedPropertyHash
313
314 type PrimitiveConstructor = String | Boolean | Number | Date | Object | Class_Buffer
315 type EnumTypeValues = any[]
316 type PropTypeStrPropertyDefinition = string
317
318 type ComplexModelPropertyDefinition =
319 ModelPropertyDefinition
320 | (PrimitiveConstructor & {
321 name: string
322 })
323 | EnumTypeValues
324 | PropTypeStrPropertyDefinition
325
326 type ModelPropertyDefinitionHash = {
327 [key: string]: ComplexModelPropertyDefinition
328 }
329
330 interface DetailedPropertyDefinitionHash {
331 [key: string]: ModelPropertyDefinition
332 }
333
334 interface ModelOptions__Find {
335 chainfind_linktable?: string;
336
337 only?: string[];
338 limit?: number;
339 order?: FxOrmQuery.OrderRawInput | FxOrmQuery.ChainFindOptions['order']
340 // order?: FxOrmQuery.OrderRawInput
341 offset?: number;
342 identityCache?: boolean
343
344 autoFetch?: boolean
345 cascadeRemove?: boolean
346 autoSave?: boolean
347 autoFetchLimit?: number
348 __merge?: FxOrmQuery.ChainFindOptions['merge']
349 exists?: FxOrmQuery.ChainWhereExistsInfo[]
350
351 // useless, just for compat
352 extra?: FxOrmAssociation.InstanceAssociationItem_HasMany['props']
353
354 // access dynamic findby options
355 [k: string]: any
356 }
357
358 interface ModelOptions__Findby extends ModelOptions__Find {
359
360 }
361
362 interface ModelOptions__Get extends ModelOptions__Find {}
363
364 interface ModelQueryConditions__Find extends FxSqlQuerySubQuery.SubQueryConditions {
365 [property: string]: any
366 }
367
368 type ModelQueryConditionsItem = FxSqlQuerySql.SqlFragmentStr | ModelQueryConditions__Find
369
370 type ModelMethodOptions_Find = FxOrmNS.IdType | ModelQueryConditions__Find
371
372 type ModelMethodCallback__Boolean = FxOrmNS.GenericCallback<Boolean>
373 type ModelMethodCallback__Find = FxOrmNS.GenericCallback<FxOrmInstance.Instance[]>
374 type ModelMethodCallback__Get = FxOrmNS.GenericCallback<FxOrmInstance.Instance>
375 type ModelMethodCallback__CreateItem = FxOrmNS.GenericCallback<FxOrmInstance.Instance>
376 type ModelMethodCallback__UpdateItem = FxOrmNS.GenericCallback<FxOrmInstance.Instance>
377 type ModelMethodCallback__BatchCreate = FxOrmNS.GenericCallback<FxOrmInstance.Instance[]>
378 type ModelMethodCallback__BatchUpdate = FxOrmNS.GenericCallback<FxOrmInstance.Instance[]>
379
380 type ModelMethodCallback__Count = FxOrmNS.GenericCallback<number>
381
382 interface ModelOptions__Create {
383 parallel?: boolean
384 }
385}