1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const g = require('strong-globalize')();
|
13 | const inflection = require('inflection');
|
14 | const EventEmitter = require('events').EventEmitter;
|
15 | const util = require('util');
|
16 | const assert = require('assert');
|
17 | const deprecated = require('depd')('loopback-datasource-juggler');
|
18 | const DefaultModelBaseClass = require('./model.js');
|
19 | const List = require('./list.js');
|
20 | const ModelDefinition = require('./model-definition.js');
|
21 | const MixinProvider = require('./mixins');
|
22 | const {
|
23 | deepMerge,
|
24 | deepMergeProperty,
|
25 | rankArrayElements,
|
26 | isClass,
|
27 | applyParentProperty,
|
28 | } = require('./utils');
|
29 |
|
30 |
|
31 | require('./types')(ModelBuilder);
|
32 |
|
33 | const introspect = require('./introspection')(ModelBuilder);
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | exports.ModelBuilder = exports.Schema = ModelBuilder;
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const slice = Array.prototype.slice;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function ModelBuilder() {
|
53 |
|
54 | this.models = {};
|
55 | this.definitions = {};
|
56 | this.settings = {};
|
57 | this.mixins = new MixinProvider(this);
|
58 | this.defaultModelBaseClass = DefaultModelBaseClass;
|
59 | }
|
60 |
|
61 |
|
62 | util.inherits(ModelBuilder, EventEmitter);
|
63 |
|
64 |
|
65 | ModelBuilder.defaultInstance = new ModelBuilder();
|
66 |
|
67 | function isModelClass(cls) {
|
68 | if (!cls) {
|
69 | return false;
|
70 | }
|
71 | return cls.prototype instanceof DefaultModelBaseClass;
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | ModelBuilder.prototype.getModel = function(name, forceCreate) {
|
82 | let model = this.models[name];
|
83 | if (!model && forceCreate) {
|
84 | model = this.define(name, {}, {unresolved: true});
|
85 | }
|
86 | return model;
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | ModelBuilder.prototype.getModelDefinition = function(name) {
|
95 | return this.definitions[name];
|
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 | ModelBuilder.prototype.define = function defineClass(className, properties, settings, parent) {
|
128 | const modelBuilder = this;
|
129 | const args = slice.call(arguments);
|
130 | const pluralName = (settings && settings.plural) ||
|
131 | inflection.pluralize(className);
|
132 |
|
133 | const httpOptions = (settings && settings.http) || {};
|
134 | let pathName = httpOptions.path || pluralName;
|
135 |
|
136 | if (!className) {
|
137 | throw new Error(g.f('Class name required'));
|
138 | }
|
139 | if (args.length === 1) {
|
140 | properties = {};
|
141 | args.push(properties);
|
142 | }
|
143 | if (args.length === 2) {
|
144 | settings = {};
|
145 | args.push(settings);
|
146 | }
|
147 |
|
148 | properties = properties || {};
|
149 | settings = settings || {};
|
150 |
|
151 |
|
152 | if (settings.strict === undefined || settings.strict === null) {
|
153 | settings.strict = false;
|
154 | }
|
155 |
|
156 |
|
157 | let ModelBaseClass = parent || this.defaultModelBaseClass;
|
158 | const baseClass = settings.base || settings['super'];
|
159 | if (baseClass) {
|
160 |
|
161 | settings.base = baseClass;
|
162 | delete settings['super'];
|
163 |
|
164 | if (isModelClass(baseClass)) {
|
165 | ModelBaseClass = baseClass;
|
166 | } else {
|
167 | ModelBaseClass = this.models[baseClass];
|
168 | assert(ModelBaseClass, 'Base model is not found: ' + baseClass);
|
169 | }
|
170 | }
|
171 |
|
172 |
|
173 | assert(ModelBaseClass.getMergePolicy, `Base class ${ModelBaseClass.modelName}
|
174 | does not provide method getMergePolicy(). Most likely it is not inheriting
|
175 | from datasource-juggler's built-in default ModelBaseClass, which is an
|
176 | incorrect usage of the framework.`);
|
177 |
|
178 |
|
179 | ModelBaseClass.__rank = ModelBaseClass.__rank || 1;
|
180 |
|
181 |
|
182 |
|
183 | if ((parent && !settings.base) || (!parent && settings.base)) {
|
184 | return ModelBaseClass.extend(className, properties, settings);
|
185 | }
|
186 |
|
187 |
|
188 | let ModelClass = this.models[className];
|
189 |
|
190 |
|
191 |
|
192 | if (!ModelClass || !ModelClass.settings.unresolved) {
|
193 | ModelClass = createModelClassCtor(className, ModelBaseClass);
|
194 |
|
195 |
|
196 | const events = new EventEmitter();
|
197 |
|
198 |
|
199 | events.setMaxListeners(32);
|
200 | for (const f in EventEmitter.prototype) {
|
201 | if (typeof EventEmitter.prototype[f] === 'function') {
|
202 | ModelClass[f] = EventEmitter.prototype[f].bind(events);
|
203 | }
|
204 | }
|
205 | hiddenProperty(ModelClass, 'modelName', className);
|
206 | }
|
207 |
|
208 |
|
209 | ModelClass.__rank = ModelBaseClass.__rank + 1;
|
210 |
|
211 | util.inherits(ModelClass, ModelBaseClass);
|
212 |
|
213 |
|
214 | this.models[className] = ModelClass;
|
215 |
|
216 |
|
217 | if (settings.unresolved) {
|
218 | ModelClass.settings = {unresolved: true};
|
219 | return ModelClass;
|
220 | }
|
221 |
|
222 |
|
223 | hiddenProperty(ModelClass, 'modelBuilder', modelBuilder);
|
224 | hiddenProperty(ModelClass, 'dataSource', null);
|
225 | hiddenProperty(ModelClass, 'pluralModelName', pluralName);
|
226 | hiddenProperty(ModelClass, 'relations', {});
|
227 | if (pathName[0] !== '/') {
|
228 |
|
229 | pathName = '/' + pathName;
|
230 | }
|
231 | hiddenProperty(ModelClass, 'http', {path: pathName});
|
232 | hiddenProperty(ModelClass, 'base', ModelBaseClass);
|
233 | hiddenProperty(ModelClass, '_observers', {});
|
234 | hiddenProperty(ModelClass, '_warned', {});
|
235 |
|
236 |
|
237 | for (const i in ModelBaseClass) {
|
238 |
|
239 | if (i !== '_mixins' && !(i in ModelClass)) {
|
240 | ModelClass[i] = ModelBaseClass[i];
|
241 | }
|
242 | }
|
243 |
|
244 |
|
245 | if (settings.models) {
|
246 | Object.keys(settings.models).forEach(function(m) {
|
247 | const model = settings.models[m];
|
248 | ModelClass[m] = typeof model === 'string' ? modelBuilder.getModel(model, true) : model;
|
249 | });
|
250 | }
|
251 |
|
252 | ModelClass.getter = {};
|
253 | ModelClass.setter = {};
|
254 |
|
255 | for (const p in properties) {
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 | const excludePropertyList = settings['excludeBaseProperties'];
|
265 |
|
266 | if (properties[p] === null || properties[p] === false ||
|
267 | (excludePropertyList != null && excludePropertyList.indexOf(p) != -1)) {
|
268 |
|
269 | delete properties[p];
|
270 | }
|
271 |
|
272 |
|
273 | if (/\./.test(p)) {
|
274 | throw new Error(g.f('Property names containing dot(s) are not supported. ' +
|
275 | 'Model: %s, property: %s', className, p));
|
276 | }
|
277 |
|
278 |
|
279 | if (p === 'constructor') {
|
280 | deprecated(g.f('Property name should not be "{{constructor}}" in Model: %s', className));
|
281 | }
|
282 | }
|
283 |
|
284 | const modelDefinition = new ModelDefinition(this, className, properties, settings);
|
285 |
|
286 | this.definitions[className] = modelDefinition;
|
287 |
|
288 |
|
289 | ModelClass.definition = modelDefinition;
|
290 |
|
291 | ModelClass.settings = modelDefinition.settings;
|
292 |
|
293 | let idInjection = settings.idInjection;
|
294 | if (idInjection !== false) {
|
295 |
|
296 | idInjection = true;
|
297 | }
|
298 |
|
299 | let idNames = modelDefinition.idNames();
|
300 | if (idNames.length > 0) {
|
301 |
|
302 | idInjection = false;
|
303 | }
|
304 |
|
305 |
|
306 | if (idInjection) {
|
307 |
|
308 | ModelClass.definition.defineProperty('id', {type: Number, id: 1, generated: true});
|
309 | }
|
310 |
|
311 | idNames = modelDefinition.idNames();
|
312 |
|
313 | if (idNames.length === 1) {
|
314 | const idProp = idNames[0];
|
315 | if (idProp !== 'id') {
|
316 | Object.defineProperty(ModelClass.prototype, 'id', {
|
317 | get: function() {
|
318 | const idProp = ModelClass.definition.idNames()[0];
|
319 | return this.__data[idProp];
|
320 | },
|
321 | configurable: true,
|
322 | enumerable: false,
|
323 | });
|
324 | }
|
325 | } else {
|
326 |
|
327 | Object.defineProperty(ModelClass.prototype, 'id', {
|
328 | get: function() {
|
329 | const compositeId = {};
|
330 | const idNames = ModelClass.definition.idNames();
|
331 | for (let i = 0, p; i < idNames.length; i++) {
|
332 | p = idNames[i];
|
333 | compositeId[p] = this.__data[p];
|
334 | }
|
335 | return compositeId;
|
336 | },
|
337 | configurable: true,
|
338 | enumerable: false,
|
339 | });
|
340 | }
|
341 |
|
342 |
|
343 |
|
344 | let forceId = ModelClass.settings.forceId;
|
345 | if (idNames.length > 0) {
|
346 | const idName = modelDefinition.idName();
|
347 | const idProp = ModelClass.definition.rawProperties[idName];
|
348 | if (idProp.generated && forceId !== false) {
|
349 | forceId = 'auto';
|
350 | } else if (!idProp.generated && forceId === 'auto') {
|
351 |
|
352 |
|
353 |
|
354 |
|
355 | forceId = false;
|
356 | }
|
357 |
|
358 | if (forceId) {
|
359 | ModelClass.validatesAbsenceOf(idName, {if: 'isNewRecord'});
|
360 | }
|
361 |
|
362 | ModelClass.definition.properties[idName].updateOnly = !!forceId;
|
363 | ModelClass.definition.rawProperties[idName].updateOnly = !!forceId;
|
364 |
|
365 | ModelClass.settings.forceId = forceId;
|
366 | }
|
367 |
|
368 |
|
369 | ModelClass.forEachProperty = function(cb) {
|
370 | const props = ModelClass.definition.properties;
|
371 | const keys = Object.keys(props);
|
372 | for (let i = 0, n = keys.length; i < n; i++) {
|
373 | cb(keys[i], props[keys[i]]);
|
374 | }
|
375 | };
|
376 |
|
377 |
|
378 | ModelClass.attachTo = function(dataSource) {
|
379 | dataSource.attach(this);
|
380 | };
|
381 |
|
382 | |
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 | ModelClass.extend = function(className, subClassProperties, subClassSettings) {
|
404 | const baseClassProperties = ModelClass.definition.properties;
|
405 | const baseClassSettings = ModelClass.definition.settings;
|
406 |
|
407 | subClassProperties = subClassProperties || {};
|
408 | subClassSettings = subClassSettings || {};
|
409 |
|
410 |
|
411 | let idFound = false;
|
412 | for (const k in subClassProperties) {
|
413 | if (subClassProperties[k] && subClassProperties[k].id) {
|
414 | idFound = true;
|
415 | break;
|
416 | }
|
417 | }
|
418 |
|
419 |
|
420 | const keys = Object.keys(baseClassProperties);
|
421 | for (let i = 0, n = keys.length; i < n; i++) {
|
422 | const key = keys[i];
|
423 |
|
424 | if (idFound && baseClassProperties[key].id) {
|
425 |
|
426 | continue;
|
427 | }
|
428 | if (subClassProperties[key] === undefined) {
|
429 | const baseProp = baseClassProperties[key];
|
430 | let basePropCopy = baseProp;
|
431 | if (baseProp && typeof baseProp === 'object') {
|
432 |
|
433 | basePropCopy = deepMerge(baseProp);
|
434 | }
|
435 | subClassProperties[key] = basePropCopy;
|
436 | }
|
437 | }
|
438 |
|
439 |
|
440 | const originalSubclassSettings = subClassSettings;
|
441 | const mergePolicy = ModelClass.getMergePolicy(subClassSettings);
|
442 | subClassSettings = mergeSettings(baseClassSettings, subClassSettings, mergePolicy);
|
443 |
|
444 |
|
445 |
|
446 |
|
447 | if (!originalSubclassSettings.base) {
|
448 | subClassSettings.base = ModelClass;
|
449 | }
|
450 |
|
451 |
|
452 | const subClass = modelBuilder.define(className, subClassProperties, subClassSettings, ModelClass);
|
453 |
|
454 |
|
455 | if (typeof subClass.setup === 'function') {
|
456 | subClass.setup.call(subClass);
|
457 | }
|
458 |
|
459 | return subClass;
|
460 | };
|
461 |
|
462 | |
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | function mergeSettings(baseClassSettings, subClassSettings, mergePolicy) {
|
499 |
|
500 | const mergedSettings = deepMerge(baseClassSettings);
|
501 |
|
502 | Object.keys(baseClassSettings).forEach(function(key) {
|
503 |
|
504 | if (mergePolicy[key] && mergePolicy[key].rank) {
|
505 | baseClassSettings[key] = rankArrayElements(baseClassSettings[key], ModelBaseClass.__rank);
|
506 | }
|
507 | });
|
508 |
|
509 | Object.keys(subClassSettings).forEach(function(key) {
|
510 |
|
511 |
|
512 | if (mergePolicy[key] == null) {
|
513 | mergePolicy[key] = mergePolicy.__default || {};
|
514 | }
|
515 |
|
516 |
|
517 | if (subClassSettings[key] === mergePolicy.__delete) {
|
518 | delete mergedSettings[key];
|
519 | return;
|
520 | }
|
521 |
|
522 | if (mergePolicy[key].rank) {
|
523 | subClassSettings[key] = rankArrayElements(subClassSettings[key], ModelBaseClass.__rank + 1);
|
524 | }
|
525 |
|
526 | if (mergePolicy[key].replace) {
|
527 | mergedSettings[key] = subClassSettings[key];
|
528 | return;
|
529 | }
|
530 |
|
531 | if (mergePolicy[key].patch) {
|
532 |
|
533 | mergedSettings[key] = mergedSettings[key] || {};
|
534 | Object.keys(subClassSettings[key]).forEach(function(innerKey) {
|
535 | mergedSettings[key][innerKey] = subClassSettings[key][innerKey];
|
536 | });
|
537 | return;
|
538 | }
|
539 |
|
540 |
|
541 |
|
542 | mergedSettings[key] = deepMergeProperty(baseClassSettings[key], subClassSettings[key]);
|
543 | });
|
544 |
|
545 | return mergedSettings;
|
546 | }
|
547 |
|
548 | |
549 |
|
550 |
|
551 |
|
552 | ModelClass.registerProperty = function(propertyName) {
|
553 | const properties = modelDefinition.build();
|
554 | const prop = properties[propertyName];
|
555 | const DataType = prop.type;
|
556 | if (!DataType) {
|
557 | throw new Error(g.f('Invalid type for property %s', propertyName));
|
558 | }
|
559 |
|
560 | if (prop.required) {
|
561 | const requiredOptions = typeof prop.required === 'object' ? prop.required : undefined;
|
562 | ModelClass.validatesPresenceOf(propertyName, requiredOptions);
|
563 | }
|
564 | if (DataType === Date) ModelClass.validatesDateOf(propertyName);
|
565 |
|
566 | Object.defineProperty(ModelClass.prototype, propertyName, {
|
567 | get: function() {
|
568 | if (ModelClass.getter[propertyName]) {
|
569 | return ModelClass.getter[propertyName].call(this);
|
570 | } else {
|
571 | return this.__data && this.__data[propertyName];
|
572 | }
|
573 | },
|
574 | set: function(value) {
|
575 | let DataType = ModelClass.definition.properties[propertyName].type;
|
576 | if (Array.isArray(DataType) || DataType === Array) {
|
577 | DataType = List;
|
578 | } else if (DataType === Date) {
|
579 | DataType = DateType;
|
580 | } else if (DataType === Boolean) {
|
581 | DataType = BooleanType;
|
582 | } else if (typeof DataType === 'string') {
|
583 | DataType = modelBuilder.resolveType(DataType);
|
584 | }
|
585 |
|
586 | const persistUndefinedAsNull = ModelClass.definition.settings.persistUndefinedAsNull;
|
587 | if (value === undefined && persistUndefinedAsNull) {
|
588 | value = null;
|
589 | }
|
590 |
|
591 | if (ModelClass.setter[propertyName]) {
|
592 | ModelClass.setter[propertyName].call(this, value);
|
593 | } else {
|
594 | this.__data = this.__data || {};
|
595 | if (value === null || value === undefined) {
|
596 | this.__data[propertyName] = value;
|
597 | } else {
|
598 | if (DataType === List) {
|
599 | this.__data[propertyName] = isClass(DataType) ?
|
600 | new DataType(value, properties[propertyName].type, this) :
|
601 | DataType(value, properties[propertyName].type, this);
|
602 | } else {
|
603 |
|
604 |
|
605 | this.__data[propertyName] = (value instanceof DataType) ?
|
606 | value :
|
607 | isClass(DataType) ? new DataType(value) : DataType(value);
|
608 | if (value && this.__data[propertyName] instanceof DefaultModelBaseClass) {
|
609 |
|
610 | applyParentProperty(this.__data[propertyName], this);
|
611 | }
|
612 | }
|
613 | }
|
614 | }
|
615 | },
|
616 | configurable: true,
|
617 | enumerable: true,
|
618 | });
|
619 |
|
620 |
|
621 |
|
622 | Object.defineProperty(ModelClass.prototype, '$' + propertyName, {
|
623 | get: function() {
|
624 | return this.__data && this.__data[propertyName];
|
625 | },
|
626 | set: function(value) {
|
627 | if (!this.__data) {
|
628 | this.__data = {};
|
629 | }
|
630 | this.__data[propertyName] = value;
|
631 | },
|
632 | configurable: true,
|
633 | enumerable: false,
|
634 | });
|
635 | };
|
636 |
|
637 | const props = ModelClass.definition.properties;
|
638 | let keys = Object.keys(props);
|
639 | let size = keys.length;
|
640 | for (let i = 0; i < size; i++) {
|
641 | const propertyName = keys[i];
|
642 | ModelClass.registerProperty(propertyName);
|
643 | }
|
644 |
|
645 | const mixinSettings = settings.mixins || {};
|
646 | keys = Object.keys(mixinSettings);
|
647 | size = keys.length;
|
648 | for (let i = 0; i < size; i++) {
|
649 | const name = keys[i];
|
650 | let mixin = mixinSettings[name];
|
651 | if (mixin === true) {
|
652 | mixin = {};
|
653 | }
|
654 | if (Array.isArray(mixin)) {
|
655 | mixin.forEach(function(m) {
|
656 | if (m === true) m = {};
|
657 | if (typeof m === 'object') {
|
658 | modelBuilder.mixins.applyMixin(ModelClass, name, m);
|
659 | }
|
660 | });
|
661 | } else if (typeof mixin === 'object') {
|
662 | modelBuilder.mixins.applyMixin(ModelClass, name, mixin);
|
663 | }
|
664 | }
|
665 |
|
666 | ModelClass.emit('defined', ModelClass);
|
667 |
|
668 | return ModelClass;
|
669 | };
|
670 |
|
671 | function createModelClassCtor(name, ModelBaseClass) {
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 | name = name.replace(/[-.:]/g, '_');
|
678 |
|
679 | try {
|
680 |
|
681 |
|
682 |
|
683 |
|
684 | const factory = new Function('ModelBaseClass', `
|
685 | // every class can receive hash of data as optional param
|
686 | return function ${name}(data, options) {
|
687 | if (!(this instanceof ${name})) {
|
688 | return new ${name}(data, options);
|
689 | }
|
690 | if (${name}.settings.unresolved) {
|
691 | throw new Error(g.f('Model %s is not defined.', ${JSON.stringify(name)}));
|
692 | }
|
693 | ModelBaseClass.apply(this, arguments);
|
694 | };`);
|
695 |
|
696 | return factory(ModelBaseClass);
|
697 | } catch (err) {
|
698 |
|
699 |
|
700 |
|
701 | if (err.name === 'SyntaxError') {
|
702 | return createModelClassCtor('ModelConstructor', ModelBaseClass);
|
703 | } else {
|
704 | throw err;
|
705 | }
|
706 | }
|
707 | }
|
708 |
|
709 |
|
710 | function DateType(arg) {
|
711 | if (arg === null) return null;
|
712 | const d = new Date(arg);
|
713 | return d;
|
714 | }
|
715 |
|
716 |
|
717 | function BooleanType(arg) {
|
718 | if (typeof arg === 'string') {
|
719 | switch (arg) {
|
720 | case 'true':
|
721 | case '1':
|
722 | return true;
|
723 | case 'false':
|
724 | case '0':
|
725 | return false;
|
726 | }
|
727 | }
|
728 | if (arg == null) {
|
729 | return null;
|
730 | }
|
731 | return Boolean(arg);
|
732 | }
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 | ModelBuilder.prototype.defineProperty = function(model, propertyName, propertyDefinition) {
|
742 | this.definitions[model].defineProperty(propertyName, propertyDefinition);
|
743 | this.models[model].registerProperty(propertyName);
|
744 | };
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 | ModelBuilder.prototype.defineValueType = function(type, aliases) {
|
752 | ModelBuilder.registerType(type, aliases);
|
753 | };
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 | ModelBuilder.prototype.extendModel = function(model, props) {
|
786 | const t = this;
|
787 | const keys = Object.keys(props);
|
788 | for (let i = 0; i < keys.length; i++) {
|
789 | const definition = props[keys[i]];
|
790 | t.defineProperty(model, keys[i], definition);
|
791 | }
|
792 | };
|
793 |
|
794 | ModelBuilder.prototype.copyModel = function copyModel(Master) {
|
795 | const modelBuilder = this;
|
796 | const className = Master.modelName;
|
797 | const md = Master.modelBuilder.definitions[className];
|
798 | const Slave = function SlaveModel() {
|
799 | Master.apply(this, [].slice.call(arguments));
|
800 | };
|
801 |
|
802 | util.inherits(Slave, Master);
|
803 |
|
804 | Slave.__proto__ = Master;
|
805 |
|
806 | hiddenProperty(Slave, 'modelBuilder', modelBuilder);
|
807 | hiddenProperty(Slave, 'modelName', className);
|
808 | hiddenProperty(Slave, 'relations', Master.relations);
|
809 |
|
810 | if (!(className in modelBuilder.models)) {
|
811 |
|
812 | modelBuilder.models[className] = Slave;
|
813 | modelBuilder.definitions[className] = {
|
814 | properties: md.properties,
|
815 | settings: md.settings,
|
816 | };
|
817 | }
|
818 |
|
819 | return Slave;
|
820 | };
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 | ModelBuilder.prototype.deleteModelByName = function(modelName) {
|
828 | delete this.models[modelName];
|
829 | delete this.definitions[modelName];
|
830 | };
|
831 |
|
832 |
|
833 |
|
834 |
|
835 | function hiddenProperty(where, property, value) {
|
836 | Object.defineProperty(where, property, {
|
837 | writable: true,
|
838 | enumerable: false,
|
839 | configurable: true,
|
840 | value: value,
|
841 | });
|
842 | }
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 | ModelBuilder.prototype.getSchemaName = function(name) {
|
851 | if (name) {
|
852 | return name;
|
853 | }
|
854 | if (typeof this._nameCount !== 'number') {
|
855 | this._nameCount = 0;
|
856 | } else {
|
857 | this._nameCount++;
|
858 | }
|
859 | return 'AnonymousModel_' + this._nameCount;
|
860 | };
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 | ModelBuilder.prototype.resolveType = function(prop, isSubProperty) {
|
868 | if (!prop) {
|
869 | return prop;
|
870 | }
|
871 | if (Array.isArray(prop) && prop.length > 0) {
|
872 |
|
873 | const itemType = this.resolveType(prop[0]);
|
874 | if (typeof itemType === 'function') {
|
875 | return [itemType];
|
876 | } else {
|
877 | return itemType;
|
878 | }
|
879 | }
|
880 | if (typeof prop === 'string') {
|
881 | const schemaType = ModelBuilder.schemaTypes[prop.toLowerCase()] || this.models[prop];
|
882 | if (schemaType) {
|
883 | return schemaType;
|
884 | } else {
|
885 |
|
886 | prop = this.define(prop, {}, {unresolved: true});
|
887 | return prop;
|
888 | }
|
889 | } else if (prop.constructor.name === 'Object') {
|
890 |
|
891 | if (!isSubProperty && prop.type) {
|
892 | return this.resolveType(prop.type, true);
|
893 | } else {
|
894 | return this.define(this.getSchemaName(null),
|
895 | prop, {
|
896 | anonymous: true,
|
897 | idInjection: false,
|
898 | strict: this.settings.strictEmbeddedModels || false,
|
899 | });
|
900 | }
|
901 | } else if ('function' === typeof prop) {
|
902 | return prop;
|
903 | }
|
904 | return prop;
|
905 | };
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 | ModelBuilder.prototype.buildModels = function(schemas, createModel) {
|
921 | const models = {};
|
922 |
|
923 |
|
924 | if (!Array.isArray(schemas)) {
|
925 | if (schemas.properties && schemas.name) {
|
926 |
|
927 | schemas = [schemas];
|
928 | } else {
|
929 |
|
930 | schemas = [
|
931 | {
|
932 | name: this.getSchemaName(),
|
933 | properties: schemas,
|
934 | options: {anonymous: true},
|
935 | },
|
936 | ];
|
937 | }
|
938 | }
|
939 |
|
940 | let relations = [];
|
941 | for (let s = 0, n = schemas.length; s < n; s++) {
|
942 | const name = this.getSchemaName(schemas[s].name);
|
943 | schemas[s].name = name;
|
944 | const model = typeof createModel === 'function' ?
|
945 | createModel(schemas[s].name, schemas[s].properties, schemas[s].options) :
|
946 | this.define(schemas[s].name, schemas[s].properties, schemas[s].options);
|
947 | models[name] = model;
|
948 | relations = relations.concat(model.definition.relations);
|
949 | }
|
950 |
|
951 |
|
952 | for (let i = 0; i < relations.length; i++) {
|
953 | const relation = relations[i];
|
954 | const sourceModel = models[relation.source];
|
955 | const targetModel = models[relation.target];
|
956 | if (sourceModel && targetModel) {
|
957 | if (typeof sourceModel[relation.type] === 'function') {
|
958 | sourceModel[relation.type](targetModel, {as: relation.as});
|
959 | }
|
960 | }
|
961 | }
|
962 | return models;
|
963 | };
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 | ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) {
|
973 |
|
974 | const schema = introspect(json);
|
975 |
|
976 |
|
977 | return this.define(name, schema, options);
|
978 | };
|