-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmodel.ts
327 lines (292 loc) · 7.92 KB
/
model.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {AnyObject, DataObject, Options} from './common-types';
import {RelationMetadata} from './relations';
import {TypeResolver} from './type-resolver';
import {Type} from './types';
/**
* This module defines the key classes representing building blocks for Domain
* Driven Design.
* See https://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks
*/
// tslint:disable:no-any
export type PropertyType =
| string
| Function
| Object
| Type<any>
| TypeResolver<Model>;
/**
* Property definition for a model
*/
export interface PropertyDefinition {
type: PropertyType; // For example, 'string', String, or {}
id?: boolean;
json?: PropertyForm;
store?: PropertyForm;
itemType?: PropertyType; // type of array
[attribute: string]: any; // Other attributes
}
/**
* See https://github.com/strongloop/loopback-datasource-juggler/issues/432
*/
export interface PropertyForm {
in?: boolean; // Can the property be used for input
out?: boolean; // Can the property be used for output
name?: string; // Custom name for this form
}
/**
* A key-value map describing model relations.
* A relation name is used as the key, a relation definition is the value.
*/
export type RelationDefinitionMap = {
[relationName: string]: RelationMetadata;
};
/**
* DSL for building a model definition.
*/
export interface ModelDefinitionSyntax {
name: string;
properties?: {[name: string]: PropertyDefinition | PropertyType};
settings?: {[name: string]: any};
relations?: RelationDefinitionMap;
[attribute: string]: any;
}
/**
* Definition for a model
*/
export class ModelDefinition {
readonly name: string;
properties: {[name: string]: PropertyDefinition};
settings: {[name: string]: any};
relations: RelationDefinitionMap;
// indexes: Map<string, any>;
[attribute: string]: any; // Other attributes
constructor(nameOrDef: string | ModelDefinitionSyntax) {
if (typeof nameOrDef === 'string') {
nameOrDef = {name: nameOrDef};
}
const {name, properties, settings, relations} = nameOrDef;
this.name = name;
this.properties = {};
if (properties) {
for (const p in properties) {
this.addProperty(p, properties[p]);
}
}
this.settings = settings || new Map();
this.relations = relations || {};
}
/**
* Add a property
* @param name Property definition or name (string)
* @param definitionOrType Definition or property type
*/
addProperty(
name: string,
definitionOrType: PropertyDefinition | PropertyType,
): this {
const definition = (definitionOrType as PropertyDefinition).type
? (definitionOrType as PropertyDefinition)
: {type: definitionOrType};
this.properties[name] = definition;
return this;
}
/**
* Add a setting
* @param name Setting name
* @param value Setting value
*/
addSetting(name: string, value: any): this {
this.settings[name] = value;
return this;
}
/**
* Define a new relation.
* @param definition The definition of the new relation.
*/
addRelation(definition: RelationMetadata): this {
this.relations[definition.name] = definition;
return this;
}
/**
* Get an array of names of ID properties, which are specified in
* the model settings or properties with `id` attribute. For example,
* ```
* {
* settings: {
* id: ['id']
* }
* properties: {
* id: {
* type: 'string',
* id: true
* }
* }
* }
* ```
*/
idProperties(): string[] {
if (typeof this.settings.id === 'string') {
return [this.settings.id];
} else if (Array.isArray(this.settings.id)) {
return this.settings.id;
}
const idProps = Object.keys(this.properties).filter(
prop => this.properties[prop].id,
);
return idProps;
}
}
function asJSON(value: any): any {
if (value == null) return value;
if (typeof value.toJSON === 'function') {
return value.toJSON();
}
// Handle arrays
if (Array.isArray(value)) {
return value.map(item => asJSON(item));
}
return value;
}
function asObject(value: any, options?: Options): any {
if (value == null) return value;
if (typeof value.toObject === 'function') {
return value.toObject(options);
}
if (typeof value.toJSON === 'function') {
return value.toJSON();
}
if (Array.isArray(value)) {
return value.map(item => asObject(item, options));
}
return value;
}
/**
* Base class for models
*/
export abstract class Model {
static get modelName(): string {
return (this.definition && this.definition.name) || this.name;
}
static definition: ModelDefinition;
/**
* Serialize into a plain JSON object
*/
toJSON(): Object {
const def = (<typeof Model>this.constructor).definition;
if (def == null || def.settings.strict === false) {
return this.toObject({ignoreUnknownProperties: false});
}
const json: AnyObject = {};
for (const p in def.properties) {
if (p in this) {
json[p] = asJSON((this as AnyObject)[p]);
}
}
return json;
}
/**
* Convert to a plain object as DTO
*/
toObject(options?: Options): Object {
let obj: AnyObject;
if (options && options.ignoreUnknownProperties === false) {
obj = {};
for (const p in this) {
let val = (this as AnyObject)[p];
obj[p] = asObject(val, options);
}
} else {
obj = this.toJSON();
}
return obj;
}
constructor(data?: DataObject<Model>) {
Object.assign(this, data);
}
}
export interface Persistable {
// isNew: boolean;
}
/**
* Base class for value objects - An object that contains attributes but has no
* conceptual identity. They should be treated as immutable.
*/
export abstract class ValueObject extends Model implements Persistable {}
/**
* Base class for entities which have unique ids
*/
export abstract class Entity extends Model implements Persistable {
/**
* Get the identity value for a given entity instance or entity data object.
*
* @param entityOrData The data object for which to determine the identity
* value.
*/
static getIdOf(entityOrData: AnyObject): any {
if (typeof entityOrData.getId === 'function') {
return entityOrData.getId();
}
const idName = this.definition.idName();
return entityOrData[idName];
}
/**
* Get the identity value. If the identity is a composite key, returns
* an object.
*/
getId(): any {
const definition = (this.constructor as typeof Entity).definition;
const idProps = definition.idProperties();
if (idProps.length === 1) {
return (this as AnyObject)[idProps[0]];
}
if (!idProps.length) {
throw new Error(
`Invalid Entity ${this.constructor.name}:` +
'missing primary key (id) property',
);
}
return this.getIdObject();
}
/**
* Get the identity as an object, such as `{id: 1}` or
* `{schoolId: 1, studentId: 2}`
*/
getIdObject(): Object {
const definition = (this.constructor as typeof Entity).definition;
const idProps = definition.idProperties();
const idObj = {} as any;
for (const idProp of idProps) {
idObj[idProp] = (this as AnyObject)[idProp];
}
return idObj;
}
/**
* Build the where object for the given id
* @param id The id value
*/
static buildWhereForId(id: any) {
const where = {} as any;
const idProps = this.definition.idProperties();
if (idProps.length === 1) {
where[idProps[0]] = id;
} else {
for (const idProp of idProps) {
where[idProp] = id[idProp];
}
}
return where;
}
}
/**
* Domain events
*/
export class Event {
source: any;
type: string;
}
export type EntityData = DataObject<Entity>;
export type EntityResolver<T extends Entity> = TypeResolver<T, typeof Entity>;