1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {EventEmitter} from 'events';
|
7 | import {AnyObject, Options} from './common';
|
8 | import {DataSource} from './datasource';
|
9 | import {Listener, OperationHookContext} from './observer-mixin';
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export type PropertyType =
|
15 | | string
|
16 | | Function
|
17 | | {[property: string]: PropertyType};
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export interface PropertyDefinition extends AnyObject {
|
23 | type?: PropertyType;
|
24 | id?: boolean | number;
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export interface Schema {
|
31 | name: string;
|
32 | properties: ModelProperties;
|
33 | settings?: ModelSettings;
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | export interface IdDefinition {
|
40 | name: string;
|
41 | id: number;
|
42 | property: AnyObject;
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | export interface IndexDefinition extends AnyObject {}
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | export interface ColumnMetadata extends AnyObject {
|
54 | name: string;
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | export interface ModelProperties {
|
66 | [name: string]: PropertyDefinition
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | export interface ModelSettings extends AnyObject {
|
78 | strict?: boolean;
|
79 | forceId?: boolean;
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | export declare class ModelDefinition extends EventEmitter implements Schema {
|
86 | name: string;
|
87 | properties: ModelProperties;
|
88 | rawProperties: AnyObject;
|
89 | settings?: ModelSettings;
|
90 | relations?: AnyObject[];
|
91 |
|
92 | constructor(
|
93 | modelBuilder: ModelBuilder | null | undefined,
|
94 | name: string,
|
95 | properties?: ModelProperties,
|
96 | settings?: ModelSettings,
|
97 | );
|
98 | constructor(modelBuilder: ModelBuilder | null | undefined, schema: Schema);
|
99 |
|
100 | tableName(connectorType: string): string;
|
101 | columnName(connectorType: string, propertyName: string): string;
|
102 | columnNames(connectorType: string): string[];
|
103 | columnMetadata(connectorType: string, propertyName: string): ColumnMetadata;
|
104 |
|
105 | ids(): IdDefinition[];
|
106 | idName(): string;
|
107 | idNames(): string[];
|
108 |
|
109 | defineProperty(
|
110 | propertyName: string,
|
111 | propertyDefinition: PropertyDefinition,
|
112 | ): void;
|
113 | indexes(): {[name: string]: IndexDefinition};
|
114 | build(forceRebuild?: boolean): AnyObject;
|
115 | toJSON(forceRebuild?: boolean): AnyObject;
|
116 | }
|
117 |
|
118 | /**
|
119 | * Base class for LoopBack 3.x models
|
120 | */
|
121 | export declare class ModelBase {
|
122 | static dataSource?: DataSource;
|
123 | static modelName: string;
|
124 | static definition: ModelDefinition;
|
125 | static readonly base: typeof ModelBase;
|
126 |
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | static extend<ChildModel extends typeof ModelBase = typeof ModelBase>(
|
145 | modelName: string,
|
146 | properties?: ModelProperties,
|
147 | settings?: ModelSettings,
|
148 | ): ChildModel;
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 | static attachTo(ds: DataSource): void;
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 | static getPropertyType(propName: string): string | null;
|
162 |
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 | static isHiddenProperty(propertyName: string): boolean;
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 | static isProtectedProperty(propertyName: string): boolean;
|
176 |
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | static getUpdateOnlyProperties(): string[];
|
184 |
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | constructor(...args: any[]);
|
195 | // constructor(data: AnyObject, options?: Options);
|
196 |
|
197 | /**
|
198 | * Convert the model instance to a plain json object
|
199 | */
|
200 | toJSON(): AnyObject;
|
201 |
|
202 | /**
|
203 | * Populate properties from a JSON object
|
204 | * @param obj The source object
|
205 | */
|
206 | fromObject(obj: AnyObject): void;
|
207 |
|
208 | /**
|
209 | * Convert model instance to a plain JSON object.
|
210 | * Returns a canonical object representation (no getters and setters).
|
211 | *
|
212 | * @param options Options for the conversion
|
213 | * @property {boolean} onlySchema Restrict properties to dataSource only.
|
214 | * Default is false. If true, the function returns only properties defined
|
215 | * in the schema; Otherwise it returns all enumerable properties.
|
216 | * @property {boolean} removeHidden Boolean flag as part of the transformation.
|
217 | * If true, then hidden properties should not be brought out.
|
218 | * @property {boolean} removeProtected Boolean flag as part of the transformation.
|
219 | * If true, then protected properties should not be brought out.
|
220 | * @returns {object} returns Plain JSON object
|
221 | */
|
222 | toObject(options?: Options): AnyObject;
|
223 |
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 | defineProperty(
|
230 | propertyName: string,
|
231 | definition: Partial<PropertyDefinition>,
|
232 | ): void;
|
233 |
|
234 | getDataSource(): DataSource;
|
235 |
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | static mixin(
|
244 | anotherClass: string | ModelBaseClass | object,
|
245 | options?: Options,
|
246 | ): ModelBaseClass;
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | |
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | static observe<T extends typeof ModelBase>(
|
289 | this: T,
|
290 | operation: string,
|
291 | listener: Listener<OperationHookContext<T>>,
|
292 | ): void;
|
293 |
|
294 | |
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | static removeObserver<T extends typeof ModelBase>(
|
311 | this: T,
|
312 | operation: string,
|
313 | listener: Listener<OperationHookContext<T>>,
|
314 | ): Listener<OperationHookContext<T>> | undefined;
|
315 |
|
316 | |
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | static clearObservers(operation: string): void;
|
331 | }
|
332 |
|
333 | export type ModelBaseClass = typeof ModelBase;
|
334 |
|
335 | export declare class ModelBuilder extends EventEmitter {
|
336 | static defaultInstance: ModelBuilder;
|
337 |
|
338 | models: {[name: string]: ModelBaseClass};
|
339 | definitions: {[name: string]: ModelDefinition};
|
340 | settings: ModelSettings;
|
341 |
|
342 | defaultModelBaseClass: typeof ModelBase;
|
343 |
|
344 | getModel(name: string, forceCreate?: boolean): ModelBaseClass;
|
345 |
|
346 | getModelDefinition(name: string): ModelDefinition | undefined;
|
347 |
|
348 | define(
|
349 | className: string,
|
350 | properties?: ModelProperties,
|
351 | settings?: ModelSettings,
|
352 | parent?: ModelBaseClass,
|
353 | ): ModelBaseClass;
|
354 |
|
355 | defineProperty(
|
356 | modelName: string,
|
357 | propertyName: string,
|
358 | propertyDefinition: AnyObject,
|
359 | ): void;
|
360 |
|
361 | defineValueType(type: string, aliases?: string[]): void;
|
362 |
|
363 | extendModel(modelName: string, properties: AnyObject): void;
|
364 |
|
365 | getSchemaName(name?: string): string;
|
366 |
|
367 | resolveType(type: any): any;
|
368 |
|
369 | buildModels(
|
370 | schemas: AnyObject,
|
371 | createModel?: Function,
|
372 | ): {[name: string]: ModelBaseClass};
|
373 |
|
374 | buildModelFromInstance(
|
375 | name: string,
|
376 | json: AnyObject,
|
377 | options: Options,
|
378 | ): ModelBaseClass;
|
379 | }
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 | export type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]>; };
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 | export type ModelData<T extends ModelBase = ModelBase> = T | DeepPartial<T>;
|