1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var isPlainObject = _interopDefault(require('lodash.isplainobject'));
|
8 | var isFunction = _interopDefault(require('lodash.isfunction'));
|
9 | var mapValues = _interopDefault(require('lodash.mapvalues'));
|
10 | var uniq = _interopDefault(require('lodash.uniq'));
|
11 | var flatten = _interopDefault(require('lodash.flatten'));
|
12 | require('@miragejs/pretender-node-polyfill/before');
|
13 | var Pretender = _interopDefault(require('pretender'));
|
14 | require('@miragejs/pretender-node-polyfill/after');
|
15 | var inflected = require('inflected');
|
16 | var lowerFirst = _interopDefault(require('lodash.lowerfirst'));
|
17 | var isEqual = _interopDefault(require('lodash.isequal'));
|
18 | var map = _interopDefault(require('lodash.map'));
|
19 | var cloneDeep = _interopDefault(require('lodash.clonedeep'));
|
20 | var invokeMap = _interopDefault(require('lodash.invokemap'));
|
21 | var compact = _interopDefault(require('lodash.compact'));
|
22 | var has = _interopDefault(require('lodash.has'));
|
23 | var values = _interopDefault(require('lodash.values'));
|
24 | var isEmpty = _interopDefault(require('lodash.isempty'));
|
25 | var get = _interopDefault(require('lodash.get'));
|
26 | var uniqBy = _interopDefault(require('lodash.uniqby'));
|
27 | var forIn = _interopDefault(require('lodash.forin'));
|
28 | var pick = _interopDefault(require('lodash.pick'));
|
29 | var assign = _interopDefault(require('lodash.assign'));
|
30 | var find = _interopDefault(require('lodash.find'));
|
31 | var isInteger = _interopDefault(require('lodash.isinteger'));
|
32 |
|
33 | // jscs:disable disallowVar, requireArrayDestructuring
|
34 | /**
|
35 | @hide
|
36 | */
|
37 |
|
38 | function referenceSort (edges) {
|
39 | let nodes = uniq(flatten(edges));
|
40 | let cursor = nodes.length;
|
41 | let sorted = new Array(cursor);
|
42 | let visited = {};
|
43 | let i = cursor;
|
44 |
|
45 | let visit = function (node, i, predecessors) {
|
46 | if (predecessors.indexOf(node) >= 0) {
|
47 | throw new Error(`Cyclic dependency in properties ${JSON.stringify(predecessors)}`);
|
48 | }
|
49 |
|
50 | if (visited[i]) {
|
51 | return;
|
52 | } else {
|
53 | visited[i] = true;
|
54 | }
|
55 |
|
56 | let outgoing = edges.filter(function (edge) {
|
57 | return edge && edge[0] === node;
|
58 | });
|
59 | i = outgoing.length;
|
60 |
|
61 | if (i) {
|
62 | let preds = predecessors.concat(node);
|
63 |
|
64 | do {
|
65 | let pair = outgoing[--i];
|
66 | let child = pair[1];
|
67 |
|
68 | if (child) {
|
69 | visit(child, nodes.indexOf(child), preds);
|
70 | }
|
71 | } while (i);
|
72 | }
|
73 |
|
74 | sorted[--cursor] = node;
|
75 | };
|
76 |
|
77 | while (i--) {
|
78 | if (!visited[i]) {
|
79 | visit(nodes[i], i, []);
|
80 | }
|
81 | }
|
82 |
|
83 | return sorted.reverse();
|
84 | }
|
85 |
|
86 | let Factory = function () {
|
87 | this.build = function (sequence) {
|
88 | let object = {};
|
89 | let topLevelAttrs = Object.assign({}, this.attrs);
|
90 | delete topLevelAttrs.afterCreate;
|
91 | Object.keys(topLevelAttrs).forEach(attr => {
|
92 | if (Factory.isTrait.call(this, attr)) {
|
93 | delete topLevelAttrs[attr];
|
94 | }
|
95 | });
|
96 | let keys = sortAttrs(topLevelAttrs, sequence);
|
97 | keys.forEach(function (key) {
|
98 | let buildAttrs, buildSingleValue;
|
99 |
|
100 | buildAttrs = function (attrs) {
|
101 | return mapValues(attrs, buildSingleValue);
|
102 | };
|
103 |
|
104 | buildSingleValue = value => {
|
105 | if (Array.isArray(value)) {
|
106 | return value.map(buildSingleValue);
|
107 | } else if (isPlainObject(value)) {
|
108 | return buildAttrs(value);
|
109 | } else if (isFunction(value)) {
|
110 | return value.call(topLevelAttrs, sequence);
|
111 | } else {
|
112 | return value;
|
113 | }
|
114 | };
|
115 |
|
116 | let value = topLevelAttrs[key];
|
117 |
|
118 | if (isFunction(value)) {
|
119 | object[key] = value.call(object, sequence);
|
120 | } else {
|
121 | object[key] = buildSingleValue(value);
|
122 | }
|
123 | });
|
124 | return object;
|
125 | };
|
126 | };
|
127 |
|
128 | Factory.extend = function (attrs) {
|
129 |
|
130 | let newAttrs = Object.assign({}, this.attrs, attrs);
|
131 |
|
132 | let Subclass = function () {
|
133 | this.attrs = newAttrs;
|
134 | Factory.call(this);
|
135 | };
|
136 |
|
137 |
|
138 | Subclass.extend = Factory.extend;
|
139 | Subclass.extractAfterCreateCallbacks = Factory.extractAfterCreateCallbacks;
|
140 | Subclass.isTrait = Factory.isTrait;
|
141 |
|
142 | Subclass.attrs = newAttrs;
|
143 | return Subclass;
|
144 | };
|
145 |
|
146 | Factory.extractAfterCreateCallbacks = function ({
|
147 | traits
|
148 | } = {}) {
|
149 | let afterCreateCallbacks = [];
|
150 | let attrs = this.attrs || {};
|
151 | let traitCandidates;
|
152 |
|
153 | if (attrs.afterCreate) {
|
154 | afterCreateCallbacks.push(attrs.afterCreate);
|
155 | }
|
156 |
|
157 | if (Array.isArray(traits)) {
|
158 | traitCandidates = traits;
|
159 | } else {
|
160 | traitCandidates = Object.keys(attrs);
|
161 | }
|
162 |
|
163 | traitCandidates.filter(attr => {
|
164 | return this.isTrait(attr) && attrs[attr].extension.afterCreate;
|
165 | }).forEach(attr => {
|
166 | afterCreateCallbacks.push(attrs[attr].extension.afterCreate);
|
167 | });
|
168 | return afterCreateCallbacks;
|
169 | };
|
170 |
|
171 | Factory.isTrait = function (attrName) {
|
172 | let {
|
173 | attrs
|
174 | } = this;
|
175 | return isPlainObject(attrs[attrName]) && attrs[attrName].__isTrait__ === true;
|
176 | };
|
177 |
|
178 | function sortAttrs(attrs, sequence) {
|
179 | let Temp = function () {};
|
180 |
|
181 | let obj = new Temp();
|
182 | let refs = [];
|
183 | let property;
|
184 | Object.keys(attrs).forEach(function (key) {
|
185 | let value;
|
186 | Object.defineProperty(obj.constructor.prototype, key, {
|
187 | get() {
|
188 | refs.push([property, key]);
|
189 | return value;
|
190 | },
|
191 |
|
192 | set(newValue) {
|
193 | value = newValue;
|
194 | },
|
195 |
|
196 | enumerable: false,
|
197 | configurable: true
|
198 | });
|
199 | });
|
200 | Object.keys(attrs).forEach(function (key) {
|
201 | let value = attrs[key];
|
202 |
|
203 | if (typeof value !== "function") {
|
204 | obj[key] = value;
|
205 | }
|
206 | });
|
207 | Object.keys(attrs).forEach(function (key) {
|
208 | let value = attrs[key];
|
209 | property = key;
|
210 |
|
211 | if (typeof value === "function") {
|
212 | obj[key] = value.call(obj, sequence);
|
213 | }
|
214 |
|
215 | refs.push([key]);
|
216 | });
|
217 | return referenceSort(refs);
|
218 | }
|
219 |
|
220 | function isNumber(n) {
|
221 | return (+n).toString() === n.toString();
|
222 | }
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | class IdentityManager {
|
243 | constructor() {
|
244 | this._nextId = 1;
|
245 | this._ids = {};
|
246 | }
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | get() {
|
255 | return this._nextId;
|
256 | }
|
257 | |
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | set(uniqueIdentifier) {
|
267 | if (this._ids[uniqueIdentifier]) {
|
268 | throw new Error(`Attempting to use the ID ${uniqueIdentifier}, but it's already been used`);
|
269 | }
|
270 |
|
271 | if (isNumber(uniqueIdentifier) && +uniqueIdentifier >= this._nextId) {
|
272 | this._nextId = +uniqueIdentifier + 1;
|
273 | }
|
274 |
|
275 | this._ids[uniqueIdentifier] = true;
|
276 | }
|
277 | |
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | inc() {
|
285 | let nextValue = this.get() + 1;
|
286 | this._nextId = nextValue;
|
287 | return nextValue;
|
288 | }
|
289 | |
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | fetch() {
|
298 | let id = this.get();
|
299 | this._ids[id] = true;
|
300 | this.inc();
|
301 | return id.toString();
|
302 | }
|
303 | |
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | reset() {
|
311 | this._nextId = 1;
|
312 | this._ids = {};
|
313 | }
|
314 |
|
315 | }
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | let association = function (...traitsAndOverrides) {
|
321 | let __isAssociation__ = true;
|
322 | return {
|
323 | __isAssociation__,
|
324 | traitsAndOverrides
|
325 | };
|
326 | };
|
327 |
|
328 | let trait = function (extension) {
|
329 | let __isTrait__ = true;
|
330 | return {
|
331 | extension,
|
332 | __isTrait__
|
333 | };
|
334 | };
|
335 |
|
336 | const warn = console.warn;
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 | class Response {
|
353 | constructor(code, headers = {}, data) {
|
354 | this.code = code;
|
355 | this.headers = headers;
|
356 |
|
357 | if (code === 204) {
|
358 | if (data !== undefined && data !== "") {
|
359 | warn(`Mirage: One of your route handlers is returning a custom
|
360 | 204 Response that has data, but this is a violation of the HTTP spec
|
361 | and could lead to unexpected behavior. 204 responses should have no
|
362 | content (an empty string) as their body.`);
|
363 | } else {
|
364 | this.data = "";
|
365 | }
|
366 |
|
367 | } else if ((data === undefined || data === "") && !Object.prototype.hasOwnProperty.call(this.headers, "Content-Type")) {
|
368 | this.data = {};
|
369 | } else {
|
370 | this.data = data;
|
371 | }
|
372 |
|
373 |
|
374 | if (code !== 204 && !Object.prototype.hasOwnProperty.call(this.headers, "Content-Type")) {
|
375 | this.headers["Content-Type"] = "application/json";
|
376 | }
|
377 | }
|
378 |
|
379 | toRackResponse() {
|
380 | return [this.code, this.headers, this.data];
|
381 | }
|
382 |
|
383 | }
|
384 |
|
385 | const camelizeCache = {};
|
386 | const dasherizeCache = {};
|
387 | const underscoreCache = {};
|
388 | const capitalizeCache = {};
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | function camelize(word) {
|
395 | if (typeof camelizeCache[word] !== "string") {
|
396 | let camelizedWord = inflected.camelize(underscore(word), false);
|
397 | |
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 | const camelized = camelizedWord.split("/").map(lowerFirst).join("/");
|
407 | camelizeCache[word] = camelized;
|
408 | }
|
409 |
|
410 | return camelizeCache[word];
|
411 | }
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 | function dasherize(word) {
|
418 | if (typeof dasherizeCache[word] !== "string") {
|
419 | const dasherized = inflected.dasherize(underscore(word));
|
420 |
|
421 | dasherizeCache[word] = dasherized;
|
422 | }
|
423 |
|
424 | return dasherizeCache[word];
|
425 | }
|
426 | function underscore(word) {
|
427 | if (typeof underscoreCache[word] !== "string") {
|
428 | const underscored = inflected.underscore(word);
|
429 |
|
430 | underscoreCache[word] = underscored;
|
431 | }
|
432 |
|
433 | return underscoreCache[word];
|
434 | }
|
435 | function capitalize(word) {
|
436 | if (typeof capitalizeCache[word] !== "string") {
|
437 | const capitalized = inflected.capitalize(word);
|
438 |
|
439 | capitalizeCache[word] = capitalized;
|
440 | }
|
441 |
|
442 | return capitalizeCache[word];
|
443 | }
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 | function isAssociation (object) {
|
450 | return isPlainObject(object) && object.__isAssociation__ === true;
|
451 | }
|
452 |
|
453 |
|
454 | let errorProps = ["description", "fileName", "lineNumber", "message", "name", "number", "stack"];
|
455 |
|
456 |
|
457 |
|
458 |
|
459 | function assert(bool, text) {
|
460 | if (typeof bool === "string" && !text) {
|
461 |
|
462 | throw new MirageError(bool);
|
463 | }
|
464 |
|
465 | if (!bool) {
|
466 |
|
467 | throw new MirageError(text.replace(/^ +/gm, "") || "Assertion failed");
|
468 | }
|
469 | }
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | function MirageError(message, stack) {
|
477 | let tmp = Error(message);
|
478 |
|
479 | if (stack) {
|
480 | tmp.stack = stack;
|
481 | }
|
482 |
|
483 | for (let idx = 0; idx < errorProps.length; idx++) {
|
484 | let prop = errorProps[idx];
|
485 |
|
486 | if (["description", "message", "stack"].indexOf(prop) > -1) {
|
487 | this[prop] = `Mirage: ${tmp[prop]}`;
|
488 | } else {
|
489 | this[prop] = tmp[prop];
|
490 | }
|
491 | }
|
492 | }
|
493 | MirageError.prototype = Object.create(Error.prototype);
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 | class Association {
|
504 | constructor(modelName, opts) {
|
505 | if (typeof modelName === "object") {
|
506 |
|
507 | this.modelName = undefined;
|
508 | this.opts = modelName;
|
509 | } else {
|
510 |
|
511 |
|
512 | this.modelName = modelName ? dasherize(modelName) : "";
|
513 | this.opts = opts || {};
|
514 | }
|
515 |
|
516 |
|
517 | this.key = "";
|
518 |
|
519 | this.ownerModelName = "";
|
520 | }
|
521 | |
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 | setSchema(schema) {
|
530 | this.schema = schema;
|
531 | }
|
532 | |
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 | isReflexive() {
|
541 | let isExplicitReflexive = !!(this.modelName === this.ownerModelName && this.opts.inverse);
|
542 | let isImplicitReflexive = !!(this.opts.inverse === undefined && this.ownerModelName === this.modelName);
|
543 | return isExplicitReflexive || isImplicitReflexive;
|
544 | }
|
545 |
|
546 | get isPolymorphic() {
|
547 | return this.opts.polymorphic;
|
548 | }
|
549 | |
550 |
|
551 |
|
552 |
|
553 |
|
554 | get identifier() {
|
555 | throw new Error("Subclasses of Association must implement a getter for identifier");
|
556 | }
|
557 |
|
558 | }
|
559 |
|
560 | const identifierCache = {};
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 | class BelongsTo extends Association {
|
572 | get identifier() {
|
573 | if (typeof identifierCache[this.key] !== "string") {
|
574 | const identifier = `${camelize(this.key)}Id`;
|
575 | identifierCache[this.key] = identifier;
|
576 | }
|
577 |
|
578 | return identifierCache[this.key];
|
579 | }
|
580 | |
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 | getForeignKeyArray() {
|
589 | return [camelize(this.ownerModelName), this.getForeignKey()];
|
590 | }
|
591 | |
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 | getForeignKey() {
|
599 |
|
600 | if (typeof identifierCache[this.key] !== "string") {
|
601 | const foreignKey = `${camelize(this.key)}Id`;
|
602 | identifierCache[this.key] = foreignKey;
|
603 | }
|
604 |
|
605 | return identifierCache[this.key];
|
606 | }
|
607 | |
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 | addMethodsToModelClass(ModelClass, key) {
|
620 | let modelPrototype = ModelClass.prototype;
|
621 | let association = this;
|
622 | let foreignKey = this.getForeignKey();
|
623 | let associationHash = {
|
624 | [key]: this
|
625 | };
|
626 | modelPrototype.belongsToAssociations = Object.assign(modelPrototype.belongsToAssociations, associationHash);
|
627 |
|
628 | Object.keys(modelPrototype.belongsToAssociations).forEach(key => {
|
629 | const value = modelPrototype.belongsToAssociations[key];
|
630 | modelPrototype.belongsToAssociationFks[value.getForeignKey()] = value;
|
631 | });
|
632 |
|
633 | this.schema.addDependentAssociation(this, this.modelName);
|
634 |
|
635 |
|
636 | modelPrototype.associationKeys.add(key);
|
637 | modelPrototype.associationIdKeys.add(foreignKey);
|
638 | Object.defineProperty(modelPrototype, foreignKey, {
|
639 | |
640 |
|
641 |
|
642 |
|
643 | get() {
|
644 | this._tempAssociations = this._tempAssociations || {};
|
645 | let tempParent = this._tempAssociations[key];
|
646 | let id;
|
647 |
|
648 | if (tempParent === null) {
|
649 | id = null;
|
650 | } else {
|
651 | if (association.isPolymorphic) {
|
652 | if (tempParent) {
|
653 | id = {
|
654 | id: tempParent.id,
|
655 | type: tempParent.modelName
|
656 | };
|
657 | } else {
|
658 | id = this.attrs[foreignKey];
|
659 | }
|
660 | } else {
|
661 | if (tempParent) {
|
662 | id = tempParent.id;
|
663 | } else {
|
664 | id = this.attrs[foreignKey];
|
665 | }
|
666 | }
|
667 | }
|
668 |
|
669 | return id;
|
670 | },
|
671 |
|
672 | |
673 |
|
674 |
|
675 |
|
676 | set(id) {
|
677 | let tempParent;
|
678 |
|
679 | if (id === null) {
|
680 | tempParent = null;
|
681 | } else if (id !== undefined) {
|
682 | if (association.isPolymorphic) {
|
683 | assert(typeof id === "object", `You're setting an ID on the polymorphic association '${association.key}' but you didn't pass in an object. Polymorphic IDs need to be in the form { type, id }.`);
|
684 | tempParent = association.schema[association.schema.toCollectionName(id.type)].find(id.id);
|
685 | } else {
|
686 | tempParent = association.schema[association.schema.toCollectionName(association.modelName)].find(id);
|
687 | assert(tempParent, `Couldn't find ${association.modelName} with id = ${id}`);
|
688 | }
|
689 | }
|
690 |
|
691 | this[key] = tempParent;
|
692 | }
|
693 |
|
694 | });
|
695 | Object.defineProperty(modelPrototype, key, {
|
696 | |
697 |
|
698 |
|
699 |
|
700 | get() {
|
701 | this._tempAssociations = this._tempAssociations || {};
|
702 | let tempParent = this._tempAssociations[key];
|
703 | let foreignKeyId = this[foreignKey];
|
704 | let model = null;
|
705 |
|
706 | if (tempParent) {
|
707 | model = tempParent;
|
708 | } else if (foreignKeyId !== null) {
|
709 | if (association.isPolymorphic) {
|
710 | model = association.schema[association.schema.toCollectionName(foreignKeyId.type)].find(foreignKeyId.id);
|
711 | } else {
|
712 | model = association.schema[association.schema.toCollectionName(association.modelName)].find(foreignKeyId);
|
713 | }
|
714 | }
|
715 |
|
716 | return model;
|
717 | },
|
718 |
|
719 | |
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 | set(model) {
|
732 | this._tempAssociations = this._tempAssociations || {};
|
733 | this._tempAssociations[key] = model;
|
734 |
|
735 | if (model && model.hasInverseFor(association)) {
|
736 | let inverse = model.inverseFor(association);
|
737 | model.associate(this, inverse);
|
738 | }
|
739 | }
|
740 |
|
741 | });
|
742 | |
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 | modelPrototype[`new${capitalize(key)}`] = function (...args) {
|
749 | let modelName, attrs;
|
750 |
|
751 | if (association.isPolymorphic) {
|
752 | modelName = args[0];
|
753 | attrs = args[1];
|
754 | } else {
|
755 | modelName = association.modelName;
|
756 | attrs = args[0];
|
757 | }
|
758 |
|
759 | let parent = association.schema[association.schema.toCollectionName(modelName)].new(attrs);
|
760 | this[key] = parent;
|
761 | return parent;
|
762 | };
|
763 | |
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 | modelPrototype[`create${capitalize(key)}`] = function (...args) {
|
771 | let modelName, attrs;
|
772 |
|
773 | if (association.isPolymorphic) {
|
774 | modelName = args[0];
|
775 | attrs = args[1];
|
776 | } else {
|
777 | modelName = association.modelName;
|
778 | attrs = args[0];
|
779 | }
|
780 |
|
781 | let parent = association.schema[association.schema.toCollectionName(modelName)].create(attrs);
|
782 | this[key] = parent;
|
783 | this.save();
|
784 | return parent.reload();
|
785 | };
|
786 | }
|
787 | |
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 | disassociateAllDependentsFromTarget(model) {
|
795 | let owner = this.ownerModelName;
|
796 | let fk;
|
797 |
|
798 | if (this.isPolymorphic) {
|
799 | fk = {
|
800 | type: model.modelName,
|
801 | id: model.id
|
802 | };
|
803 | } else {
|
804 | fk = model.id;
|
805 | }
|
806 |
|
807 | let dependents = this.schema[this.schema.toCollectionName(owner)].where(potentialOwner => {
|
808 | let id = potentialOwner[this.getForeignKey()];
|
809 |
|
810 | if (!id) {
|
811 | return false;
|
812 | }
|
813 |
|
814 | if (typeof id === "object") {
|
815 | return id.type === fk.type && id.id === fk.id;
|
816 | } else {
|
817 | return id === fk;
|
818 | }
|
819 | });
|
820 | dependents.models.forEach(dependent => {
|
821 | dependent.disassociate(model, this);
|
822 | dependent.save();
|
823 | });
|
824 | }
|
825 |
|
826 | }
|
827 |
|
828 | function duplicate(data) {
|
829 | if (Array.isArray(data)) {
|
830 | return data.map(duplicate);
|
831 | } else {
|
832 | return Object.assign({}, data);
|
833 | }
|
834 | }
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 | class DbCollection {
|
858 | constructor(name, initialData, IdentityManager) {
|
859 | this.name = name;
|
860 | this._records = [];
|
861 | this.identityManager = new IdentityManager();
|
862 |
|
863 | if (initialData) {
|
864 | this.insert(initialData);
|
865 | }
|
866 | }
|
867 | |
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 | all() {
|
876 | return duplicate(this._records);
|
877 | }
|
878 | |
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 | insert(data) {
|
899 | if (!Array.isArray(data)) {
|
900 | return this._insertRecord(data);
|
901 | } else {
|
902 | return map(data, attrs => this._insertRecord(attrs));
|
903 | }
|
904 | }
|
905 | |
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 | find(ids) {
|
922 | if (Array.isArray(ids)) {
|
923 | let records = this._findRecords(ids).filter(Boolean).map(duplicate);
|
924 |
|
925 |
|
926 | return records;
|
927 | } else {
|
928 | let record = this._findRecord(ids);
|
929 |
|
930 | if (!record) {
|
931 | return null;
|
932 | }
|
933 |
|
934 |
|
935 | return duplicate(record);
|
936 | }
|
937 | }
|
938 | |
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 |
|
952 | findBy(query) {
|
953 | let record = this._findRecordBy(query);
|
954 |
|
955 | if (!record) {
|
956 | return null;
|
957 | }
|
958 |
|
959 |
|
960 | return duplicate(record);
|
961 | }
|
962 | |
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 | where(query) {
|
977 | return this._findRecordsWhere(query).map(duplicate);
|
978 | }
|
979 | |
980 |
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 | firstOrCreate(query, attributesForCreate = {}) {
|
1014 | let queryResult = this.where(query);
|
1015 | let [record] = queryResult;
|
1016 |
|
1017 | if (record) {
|
1018 | return record;
|
1019 | } else {
|
1020 | let mergedAttributes = Object.assign(attributesForCreate, query);
|
1021 | let createdRecord = this.insert(mergedAttributes);
|
1022 | return createdRecord;
|
1023 | }
|
1024 | }
|
1025 | |
1026 |
|
1027 |
|
1028 |
|
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 | update(target, attrs) {
|
1047 | let records;
|
1048 |
|
1049 | if (typeof attrs === "undefined") {
|
1050 | attrs = target;
|
1051 | let changedRecords = [];
|
1052 |
|
1053 | this._records.forEach(record => {
|
1054 | let oldRecord = Object.assign({}, record);
|
1055 |
|
1056 | this._updateRecord(record, attrs);
|
1057 |
|
1058 | if (!isEqual(oldRecord, record)) {
|
1059 | changedRecords.push(record);
|
1060 | }
|
1061 | });
|
1062 |
|
1063 | return changedRecords;
|
1064 | } else if (typeof target === "number" || typeof target === "string") {
|
1065 | let id = target;
|
1066 |
|
1067 | let record = this._findRecord(id);
|
1068 |
|
1069 | this._updateRecord(record, attrs);
|
1070 |
|
1071 | return record;
|
1072 | } else if (Array.isArray(target)) {
|
1073 | let ids = target;
|
1074 | records = this._findRecords(ids);
|
1075 | records.forEach(record => {
|
1076 | this._updateRecord(record, attrs);
|
1077 | });
|
1078 | return records;
|
1079 | } else if (typeof target === "object") {
|
1080 | let query = target;
|
1081 | records = this._findRecordsWhere(query);
|
1082 | records.forEach(record => {
|
1083 | this._updateRecord(record, attrs);
|
1084 | });
|
1085 | return records;
|
1086 | }
|
1087 | }
|
1088 | |
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 |
|
1103 |
|
1104 |
|
1105 |
|
1106 | remove(target) {
|
1107 | let records;
|
1108 |
|
1109 | if (typeof target === "undefined") {
|
1110 | this._records = [];
|
1111 | this.identityManager.reset();
|
1112 | } else if (typeof target === "number" || typeof target === "string") {
|
1113 | let record = this._findRecord(target);
|
1114 |
|
1115 | let index = this._records.indexOf(record);
|
1116 |
|
1117 | this._records.splice(index, 1);
|
1118 | } else if (Array.isArray(target)) {
|
1119 | records = this._findRecords(target);
|
1120 | records.forEach(record => {
|
1121 | let index = this._records.indexOf(record);
|
1122 |
|
1123 | this._records.splice(index, 1);
|
1124 | });
|
1125 | } else if (typeof target === "object") {
|
1126 | records = this._findRecordsWhere(target);
|
1127 | records.forEach(record => {
|
1128 | let index = this._records.indexOf(record);
|
1129 |
|
1130 | this._records.splice(index, 1);
|
1131 | });
|
1132 | }
|
1133 | }
|
1134 | |
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 | |
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 | _findRecord(id) {
|
1149 | id = id.toString();
|
1150 | return this._records.find(obj => obj.id === id);
|
1151 | }
|
1152 | |
1153 |
|
1154 |
|
1155 |
|
1156 |
|
1157 |
|
1158 |
|
1159 |
|
1160 | _findRecordBy(query) {
|
1161 | return this._findRecordsWhere(query)[0];
|
1162 | }
|
1163 | |
1164 |
|
1165 |
|
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 |
|
1171 | _findRecords(ids) {
|
1172 | return ids.map(this._findRecord, this);
|
1173 | }
|
1174 | |
1175 |
|
1176 |
|
1177 |
|
1178 |
|
1179 |
|
1180 |
|
1181 |
|
1182 | _findRecordsWhere(query) {
|
1183 | let records = this._records;
|
1184 |
|
1185 | function defaultQueryFunction(record) {
|
1186 | let keys = Object.keys(query);
|
1187 | return keys.every(function (key) {
|
1188 | return String(record[key]) === String(query[key]);
|
1189 | });
|
1190 | }
|
1191 |
|
1192 | let queryFunction = typeof query === "object" ? defaultQueryFunction : query;
|
1193 | return records.filter(queryFunction);
|
1194 | }
|
1195 | |
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 | _insertRecord(data) {
|
1204 | let attrs = duplicate(data);
|
1205 |
|
1206 | if (attrs && (attrs.id === undefined || attrs.id === null)) {
|
1207 | attrs.id = this.identityManager.fetch(attrs);
|
1208 | } else {
|
1209 | attrs.id = attrs.id.toString();
|
1210 | this.identityManager.set(attrs.id);
|
1211 | }
|
1212 |
|
1213 | this._records.push(attrs);
|
1214 |
|
1215 | return duplicate(attrs);
|
1216 | }
|
1217 | |
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 | _updateRecord(record, attrs) {
|
1227 | let targetId = attrs && Object.prototype.hasOwnProperty.call(attrs, "id") ? attrs.id.toString() : null;
|
1228 | let currentId = record.id;
|
1229 |
|
1230 | if (targetId && currentId !== targetId) {
|
1231 | throw new Error("Updating the ID of a record is not permitted");
|
1232 | }
|
1233 |
|
1234 | for (let attr in attrs) {
|
1235 | if (attr === "id") {
|
1236 | continue;
|
1237 | }
|
1238 |
|
1239 | record[attr] = attrs[attr];
|
1240 | }
|
1241 | }
|
1242 |
|
1243 | }
|
1244 |
|
1245 |
|
1246 |
|
1247 |
|
1248 |
|
1249 |
|
1250 |
|
1251 |
|
1252 |
|
1253 |
|
1254 |
|
1255 |
|
1256 |
|
1257 |
|
1258 |
|
1259 |
|
1260 |
|
1261 | class Db {
|
1262 | constructor(initialData, identityManagers) {
|
1263 | this._collections = [];
|
1264 | this.registerIdentityManagers(identityManagers);
|
1265 |
|
1266 | if (initialData) {
|
1267 | this.loadData(initialData);
|
1268 | }
|
1269 | }
|
1270 | |
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 |
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 | loadData(data) {
|
1289 | for (let key in data) {
|
1290 | this.createCollection(key, cloneDeep(data[key]));
|
1291 | }
|
1292 | }
|
1293 | |
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 | dump() {
|
1304 | return this._collections.reduce((data, collection) => {
|
1305 | data[collection.name] = collection.all();
|
1306 | return data;
|
1307 | }, {});
|
1308 | }
|
1309 | |
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 | createCollection(name, initialData) {
|
1319 | if (!this[name]) {
|
1320 | let IdentityManager = this.identityManagerFor(name);
|
1321 | let newCollection = new DbCollection(name, initialData, IdentityManager);
|
1322 |
|
1323 |
|
1324 | Object.defineProperty(this, name, {
|
1325 | get() {
|
1326 | let recordsCopy = newCollection.all();
|
1327 | ["insert", "find", "findBy", "where", "update", "remove", "firstOrCreate"].forEach(function (method) {
|
1328 | recordsCopy[method] = function () {
|
1329 | return newCollection[method](...arguments);
|
1330 | };
|
1331 | });
|
1332 | return recordsCopy;
|
1333 | }
|
1334 |
|
1335 | });
|
1336 |
|
1337 |
|
1338 |
|
1339 | Object.defineProperty(this, `_${name}`, {
|
1340 | get() {
|
1341 | let recordsCopy = [];
|
1342 | ["insert", "find", "findBy", "where", "update", "remove", "firstOrCreate"].forEach(function (method) {
|
1343 | recordsCopy[method] = function () {
|
1344 | return newCollection[method](...arguments);
|
1345 | };
|
1346 | });
|
1347 | return recordsCopy;
|
1348 | }
|
1349 |
|
1350 | });
|
1351 |
|
1352 | this._collections.push(newCollection);
|
1353 | } else if (initialData) {
|
1354 | this[name].insert(initialData);
|
1355 | }
|
1356 |
|
1357 | return this;
|
1358 | }
|
1359 | |
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 | createCollections(...collections) {
|
1368 | collections.forEach(c => this.createCollection(c));
|
1369 | }
|
1370 | |
1371 |
|
1372 |
|
1373 |
|
1374 |
|
1375 |
|
1376 |
|
1377 | emptyData() {
|
1378 | this._collections.forEach(c => c.remove());
|
1379 | }
|
1380 | |
1381 |
|
1382 |
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 | identityManagerFor(name) {
|
1389 | return this._identityManagers[this._container.inflector.singularize(name)] || this._identityManagers.application || IdentityManager;
|
1390 | }
|
1391 | |
1392 |
|
1393 |
|
1394 |
|
1395 |
|
1396 |
|
1397 |
|
1398 | registerIdentityManagers(identityManagers) {
|
1399 | this._identityManagers = identityManagers || {};
|
1400 | }
|
1401 |
|
1402 | }
|
1403 |
|
1404 |
|
1405 |
|
1406 |
|
1407 |
|
1408 |
|
1409 |
|
1410 |
|
1411 |
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 |
|
1419 |
|
1420 |
|
1421 | class Collection {
|
1422 | constructor(modelName, models = []) {
|
1423 | assert(modelName && typeof modelName === "string", "You must pass a `modelName` into a Collection");
|
1424 | |
1425 |
|
1426 |
|
1427 |
|
1428 |
|
1429 |
|
1430 |
|
1431 |
|
1432 |
|
1433 |
|
1434 |
|
1435 |
|
1436 | this.modelName = modelName;
|
1437 | |
1438 |
|
1439 |
|
1440 |
|
1441 |
|
1442 |
|
1443 |
|
1444 |
|
1445 |
|
1446 |
|
1447 |
|
1448 |
|
1449 |
|
1450 |
|
1451 |
|
1452 |
|
1453 |
|
1454 |
|
1455 |
|
1456 | this.models = models;
|
1457 | }
|
1458 | |
1459 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 | get length() {
|
1470 | return this.models.length;
|
1471 | }
|
1472 | |
1473 |
|
1474 |
|
1475 |
|
1476 |
|
1477 |
|
1478 |
|
1479 |
|
1480 |
|
1481 |
|
1482 |
|
1483 |
|
1484 |
|
1485 |
|
1486 | update(...args) {
|
1487 | invokeMap(this.models, "update", ...args);
|
1488 | return this;
|
1489 | }
|
1490 | |
1491 |
|
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 |
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 | save() {
|
1504 | invokeMap(this.models, "save");
|
1505 | return this;
|
1506 | }
|
1507 | |
1508 |
|
1509 |
|
1510 |
|
1511 |
|
1512 |
|
1513 |
|
1514 |
|
1515 |
|
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 | reload() {
|
1521 | invokeMap(this.models, "reload");
|
1522 | return this;
|
1523 | }
|
1524 | |
1525 |
|
1526 |
|
1527 |
|
1528 |
|
1529 |
|
1530 |
|
1531 |
|
1532 |
|
1533 |
|
1534 |
|
1535 |
|
1536 | destroy() {
|
1537 | invokeMap(this.models, "destroy");
|
1538 | return this;
|
1539 | }
|
1540 | |
1541 |
|
1542 |
|
1543 |
|
1544 |
|
1545 |
|
1546 |
|
1547 |
|
1548 |
|
1549 |
|
1550 |
|
1551 |
|
1552 |
|
1553 |
|
1554 | add(model) {
|
1555 | this.models.push(model);
|
1556 | return this;
|
1557 | }
|
1558 | |
1559 |
|
1560 |
|
1561 |
|
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 | remove(model) {
|
1575 | let match = this.models.find(m => m.toString() === model.toString());
|
1576 |
|
1577 | if (match) {
|
1578 | let i = this.models.indexOf(match);
|
1579 | this.models.splice(i, 1);
|
1580 | }
|
1581 |
|
1582 | return this;
|
1583 | }
|
1584 | |
1585 |
|
1586 |
|
1587 |
|
1588 |
|
1589 |
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 |
|
1603 |
|
1604 |
|
1605 |
|
1606 | includes(model) {
|
1607 | return this.models.some(m => m.toString() === model.toString());
|
1608 | }
|
1609 | |
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 |
|
1617 |
|
1618 |
|
1619 |
|
1620 |
|
1621 | filter(f) {
|
1622 | let filteredModels = this.models.filter(f);
|
1623 | return new Collection(this.modelName, filteredModels);
|
1624 | }
|
1625 | |
1626 |
|
1627 |
|
1628 |
|
1629 |
|
1630 |
|
1631 |
|
1632 |
|
1633 |
|
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 |
|
1639 | sort(f) {
|
1640 | let sortedModels = this.models.concat().sort(f);
|
1641 | return new Collection(this.modelName, sortedModels);
|
1642 | }
|
1643 | |
1644 |
|
1645 |
|
1646 |
|
1647 |
|
1648 |
|
1649 |
|
1650 |
|
1651 |
|
1652 |
|
1653 |
|
1654 |
|
1655 |
|
1656 | slice(...args) {
|
1657 | let slicedModels = this.models.slice(...args);
|
1658 | return new Collection(this.modelName, slicedModels);
|
1659 | }
|
1660 | |
1661 |
|
1662 |
|
1663 |
|
1664 |
|
1665 |
|
1666 |
|
1667 |
|
1668 |
|
1669 |
|
1670 |
|
1671 |
|
1672 |
|
1673 | mergeCollection(collection) {
|
1674 | this.models = this.models.concat(collection.models);
|
1675 | return this;
|
1676 | }
|
1677 | |
1678 |
|
1679 |
|
1680 |
|
1681 |
|
1682 |
|
1683 |
|
1684 |
|
1685 |
|
1686 |
|
1687 |
|
1688 | toString() {
|
1689 | return `collection:${this.modelName}(${this.models.map(m => m.id).join(",")})`;
|
1690 | }
|
1691 |
|
1692 | }
|
1693 |
|
1694 |
|
1695 |
|
1696 |
|
1697 |
|
1698 |
|
1699 |
|
1700 |
|
1701 |
|
1702 |
|
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 |
|
1708 | class PolymorphicCollection {
|
1709 | constructor(models = []) {
|
1710 | this.models = models;
|
1711 | }
|
1712 | |
1713 |
|
1714 |
|
1715 |
|
1716 |
|
1717 |
|
1718 |
|
1719 |
|
1720 |
|
1721 | get length() {
|
1722 | return this.models.length;
|
1723 | }
|
1724 | |
1725 |
|
1726 |
|
1727 |
|
1728 |
|
1729 |
|
1730 |
|
1731 |
|
1732 |
|
1733 |
|
1734 | update(...args) {
|
1735 | invokeMap(this.models, "update", ...args);
|
1736 | return this;
|
1737 | }
|
1738 | |
1739 |
|
1740 |
|
1741 |
|
1742 |
|
1743 |
|
1744 |
|
1745 |
|
1746 | destroy() {
|
1747 | invokeMap(this.models, "destroy");
|
1748 | return this;
|
1749 | }
|
1750 | |
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 | save() {
|
1759 | invokeMap(this.models, "save");
|
1760 | return this;
|
1761 | }
|
1762 | |
1763 |
|
1764 |
|
1765 |
|
1766 |
|
1767 |
|
1768 |
|
1769 |
|
1770 | reload() {
|
1771 | invokeMap(this.models, "reload");
|
1772 | return this;
|
1773 | }
|
1774 | |
1775 |
|
1776 |
|
1777 |
|
1778 |
|
1779 |
|
1780 |
|
1781 |
|
1782 |
|
1783 | add(model) {
|
1784 | this.models.push(model);
|
1785 | return this;
|
1786 | }
|
1787 | |
1788 |
|
1789 |
|
1790 |
|
1791 |
|
1792 |
|
1793 |
|
1794 |
|
1795 |
|
1796 | remove(model) {
|
1797 | let match = this.models.find(m => isEqual(m.attrs, model.attrs));
|
1798 |
|
1799 | if (match) {
|
1800 | let i = this.models.indexOf(match);
|
1801 | this.models.splice(i, 1);
|
1802 | }
|
1803 |
|
1804 | return this;
|
1805 | }
|
1806 | |
1807 |
|
1808 |
|
1809 |
|
1810 |
|
1811 |
|
1812 |
|
1813 |
|
1814 |
|
1815 | includes(model) {
|
1816 | return this.models.some(m => isEqual(m.attrs, model.attrs));
|
1817 | }
|
1818 | |
1819 |
|
1820 |
|
1821 |
|
1822 |
|
1823 |
|
1824 |
|
1825 |
|
1826 | filter(f) {
|
1827 | let filteredModels = this.models.filter(f);
|
1828 | return new PolymorphicCollection(filteredModels);
|
1829 | }
|
1830 | |
1831 |
|
1832 |
|
1833 |
|
1834 |
|
1835 |
|
1836 |
|
1837 |
|
1838 | sort(f) {
|
1839 | let sortedModels = this.models.concat().sort(f);
|
1840 | return new PolymorphicCollection(sortedModels);
|
1841 | }
|
1842 | |
1843 |
|
1844 |
|
1845 |
|
1846 |
|
1847 |
|
1848 |
|
1849 |
|
1850 |
|
1851 | slice(...args) {
|
1852 | let slicedModels = this.models.slice(...args);
|
1853 | return new PolymorphicCollection(slicedModels);
|
1854 | }
|
1855 | |
1856 |
|
1857 |
|
1858 |
|
1859 |
|
1860 |
|
1861 |
|
1862 |
|
1863 | mergeCollection(collection) {
|
1864 | this.models = this.models.concat(collection.models);
|
1865 | return this;
|
1866 | }
|
1867 | |
1868 |
|
1869 |
|
1870 |
|
1871 |
|
1872 |
|
1873 |
|
1874 |
|
1875 | toString() {
|
1876 | return `collection:${this.modelName}(${this.models.map(m => m.id).join(",")})`;
|
1877 | }
|
1878 |
|
1879 | }
|
1880 |
|
1881 | const identifierCache$1 = {};
|
1882 |
|
1883 |
|
1884 |
|
1885 |
|
1886 |
|
1887 |
|
1888 |
|
1889 |
|
1890 | class HasMany extends Association {
|
1891 | get identifier() {
|
1892 | if (typeof identifierCache$1[this.key] !== "string") {
|
1893 | const identifier = `${camelize(this._container.inflector.singularize(this.key))}Ids`;
|
1894 | identifierCache$1[this.key] = identifier;
|
1895 | }
|
1896 |
|
1897 | return identifierCache$1[this.key];
|
1898 | }
|
1899 | |
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 |
|
1905 |
|
1906 |
|
1907 | getForeignKeyArray() {
|
1908 | return [camelize(this.ownerModelName), this.getForeignKey()];
|
1909 | }
|
1910 | |
1911 |
|
1912 |
|
1913 |
|
1914 |
|
1915 |
|
1916 |
|
1917 | getForeignKey() {
|
1918 |
|
1919 | if (typeof identifierCache$1[this.key] !== "string") {
|
1920 | const foreignKey = `${this._container.inflector.singularize(camelize(this.key))}Ids`;
|
1921 | identifierCache$1[this.key] = foreignKey;
|
1922 | }
|
1923 |
|
1924 | return identifierCache$1[this.key];
|
1925 | }
|
1926 | |
1927 |
|
1928 |
|
1929 |
|
1930 |
|
1931 |
|
1932 |
|
1933 |
|
1934 |
|
1935 |
|
1936 |
|
1937 |
|
1938 | addMethodsToModelClass(ModelClass, key) {
|
1939 | let modelPrototype = ModelClass.prototype;
|
1940 | let association = this;
|
1941 | let foreignKey = this.getForeignKey();
|
1942 | let associationHash = {
|
1943 | [key]: this
|
1944 | };
|
1945 | modelPrototype.hasManyAssociations = Object.assign(modelPrototype.hasManyAssociations, associationHash);
|
1946 |
|
1947 | Object.keys(modelPrototype.hasManyAssociations).forEach(key => {
|
1948 | const value = modelPrototype.hasManyAssociations[key];
|
1949 | modelPrototype.hasManyAssociationFks[value.getForeignKey()] = value;
|
1950 | });
|
1951 |
|
1952 | this.schema.addDependentAssociation(this, this.modelName);
|
1953 |
|
1954 |
|
1955 | modelPrototype.associationKeys.add(key);
|
1956 | modelPrototype.associationIdKeys.add(foreignKey);
|
1957 | Object.defineProperty(modelPrototype, foreignKey, {
|
1958 | |
1959 |
|
1960 |
|
1961 |
|
1962 | get() {
|
1963 | this._tempAssociations = this._tempAssociations || {};
|
1964 | let tempChildren = this._tempAssociations[key];
|
1965 | let ids = [];
|
1966 |
|
1967 | if (tempChildren) {
|
1968 | if (association.isPolymorphic) {
|
1969 | ids = tempChildren.models.map(model => ({
|
1970 | type: model.modelName,
|
1971 | id: model.id
|
1972 | }));
|
1973 | } else {
|
1974 | ids = tempChildren.models.map(model => model.id);
|
1975 | }
|
1976 | } else {
|
1977 | ids = this.attrs[foreignKey] || [];
|
1978 | }
|
1979 |
|
1980 | return ids;
|
1981 | },
|
1982 |
|
1983 | |
1984 |
|
1985 |
|
1986 |
|
1987 | set(ids) {
|
1988 | let tempChildren;
|
1989 |
|
1990 | if (ids === null) {
|
1991 | tempChildren = [];
|
1992 | } else if (ids !== undefined) {
|
1993 | assert(Array.isArray(ids), `You must pass an array in when setting ${foreignKey} on ${this}`);
|
1994 |
|
1995 | if (association.isPolymorphic) {
|
1996 | assert(ids.every(el => {
|
1997 | return typeof el === "object" && typeof el.type !== undefined && typeof el.id !== undefined;
|
1998 | }), `You must pass in an array of polymorphic identifiers (objects of shape { type, id }) when setting ${foreignKey} on ${this}`);
|
1999 | let models = ids.map(({
|
2000 | type,
|
2001 | id
|
2002 | }) => {
|
2003 | return association.schema[association.schema.toCollectionName(type)].find(id);
|
2004 | });
|
2005 | tempChildren = new PolymorphicCollection(models);
|
2006 | } else {
|
2007 | tempChildren = association.schema[association.schema.toCollectionName(association.modelName)].find(ids);
|
2008 | }
|
2009 | }
|
2010 |
|
2011 | this[key] = tempChildren;
|
2012 | }
|
2013 |
|
2014 | });
|
2015 | Object.defineProperty(modelPrototype, key, {
|
2016 | |
2017 |
|
2018 |
|
2019 |
|
2020 | get() {
|
2021 | this._tempAssociations = this._tempAssociations || {};
|
2022 | let collection = null;
|
2023 |
|
2024 | if (this._tempAssociations[key]) {
|
2025 | collection = this._tempAssociations[key];
|
2026 | } else {
|
2027 | if (association.isPolymorphic) {
|
2028 | if (this[foreignKey]) {
|
2029 | let polymorphicIds = this[foreignKey];
|
2030 | let models = polymorphicIds.map(({
|
2031 | type,
|
2032 | id
|
2033 | }) => {
|
2034 | return association.schema[association.schema.toCollectionName(type)].find(id);
|
2035 | });
|
2036 | collection = new PolymorphicCollection(models);
|
2037 | } else {
|
2038 | collection = new PolymorphicCollection(association.modelName);
|
2039 | }
|
2040 | } else {
|
2041 | if (this[foreignKey]) {
|
2042 | collection = association.schema[association.schema.toCollectionName(association.modelName)].find(this[foreignKey]);
|
2043 | } else {
|
2044 | collection = new Collection(association.modelName);
|
2045 | }
|
2046 | }
|
2047 |
|
2048 | this._tempAssociations[key] = collection;
|
2049 | }
|
2050 |
|
2051 | return collection;
|
2052 | },
|
2053 |
|
2054 | |
2055 |
|
2056 |
|
2057 |
|
2058 | set(models) {
|
2059 | if (models instanceof Collection || models instanceof PolymorphicCollection) {
|
2060 | models = models.models;
|
2061 | }
|
2062 |
|
2063 | models = models ? compact(models) : [];
|
2064 | this._tempAssociations = this._tempAssociations || {};
|
2065 | let collection;
|
2066 |
|
2067 | if (association.isPolymorphic) {
|
2068 | collection = new PolymorphicCollection(models);
|
2069 | } else {
|
2070 | collection = new Collection(association.modelName, models);
|
2071 | }
|
2072 |
|
2073 | this._tempAssociations[key] = collection;
|
2074 | models.forEach(model => {
|
2075 | if (model.hasInverseFor(association)) {
|
2076 | let inverse = model.inverseFor(association);
|
2077 | model.associate(this, inverse);
|
2078 | }
|
2079 | });
|
2080 | }
|
2081 |
|
2082 | });
|
2083 | |
2084 |
|
2085 |
|
2086 |
|
2087 |
|
2088 | modelPrototype[`new${capitalize(camelize(this._container.inflector.singularize(association.key)))}`] = function (...args) {
|
2089 | let modelName, attrs;
|
2090 |
|
2091 | if (association.isPolymorphic) {
|
2092 | modelName = args[0];
|
2093 | attrs = args[1];
|
2094 | } else {
|
2095 | modelName = association.modelName;
|
2096 | attrs = args[0];
|
2097 | }
|
2098 |
|
2099 | let child = association.schema[association.schema.toCollectionName(modelName)].new(attrs);
|
2100 | let children = this[key].models;
|
2101 | children.push(child);
|
2102 | this[key] = children;
|
2103 | return child;
|
2104 | };
|
2105 | |
2106 |
|
2107 |
|
2108 |
|
2109 |
|
2110 |
|
2111 |
|
2112 |
|
2113 |
|
2114 | modelPrototype[`create${capitalize(camelize(this._container.inflector.singularize(association.key)))}`] = function (...args) {
|
2115 | let modelName, attrs;
|
2116 |
|
2117 | if (association.isPolymorphic) {
|
2118 | modelName = args[0];
|
2119 | attrs = args[1];
|
2120 | } else {
|
2121 | modelName = association.modelName;
|
2122 | attrs = args[0];
|
2123 | }
|
2124 |
|
2125 | let child = association.schema[association.schema.toCollectionName(modelName)].create(attrs);
|
2126 | let children = this[key].models;
|
2127 | children.push(child);
|
2128 | this[key] = children;
|
2129 | this.save();
|
2130 | return child.reload();
|
2131 | };
|
2132 | }
|
2133 | |
2134 |
|
2135 |
|
2136 |
|
2137 |
|
2138 |
|
2139 |
|
2140 | disassociateAllDependentsFromTarget(model) {
|
2141 | let owner = this.ownerModelName;
|
2142 | let fk;
|
2143 |
|
2144 | if (this.isPolymorphic) {
|
2145 | fk = {
|
2146 | type: model.modelName,
|
2147 | id: model.id
|
2148 | };
|
2149 | } else {
|
2150 | fk = model.id;
|
2151 | }
|
2152 |
|
2153 | let dependents = this.schema[this.schema.toCollectionName(owner)].where(potentialOwner => {
|
2154 | let currentIds = potentialOwner[this.getForeignKey()];
|
2155 |
|
2156 | return currentIds && currentIds.find(id => {
|
2157 | if (typeof id === "object") {
|
2158 | return id.type === fk.type && id.id === fk.id;
|
2159 | } else {
|
2160 | return id === fk;
|
2161 | }
|
2162 | });
|
2163 | });
|
2164 | dependents.models.forEach(dependent => {
|
2165 | dependent.disassociate(model, this);
|
2166 | dependent.save();
|
2167 | });
|
2168 | }
|
2169 |
|
2170 | }
|
2171 |
|
2172 | const pathModelClassCache = {};
|
2173 |
|
2174 |
|
2175 |
|
2176 |
|
2177 | class BaseRouteHandler {
|
2178 | getModelClassFromPath(fullPath) {
|
2179 | if (!fullPath) {
|
2180 | return;
|
2181 | }
|
2182 |
|
2183 | if (typeof pathModelClassCache[fullPath] !== "string") {
|
2184 | let path = fullPath.split("/");
|
2185 | let lastPath;
|
2186 |
|
2187 | for (let i = path.length - 1; i >= 0; i--) {
|
2188 | const segment = path[i];
|
2189 |
|
2190 | if (segment.length && segment[0] !== ":") {
|
2191 | lastPath = segment;
|
2192 | break;
|
2193 | }
|
2194 | }
|
2195 |
|
2196 | pathModelClassCache[fullPath] = dasherize(camelize(this._container.inflector.singularize(lastPath)));
|
2197 | }
|
2198 |
|
2199 | return pathModelClassCache[fullPath];
|
2200 | }
|
2201 |
|
2202 | _getIdForRequest(request, jsonApiDoc) {
|
2203 | let id;
|
2204 |
|
2205 | if (request && request.params && request.params.id) {
|
2206 | id = request.params.id;
|
2207 | } else if (jsonApiDoc && jsonApiDoc.data && jsonApiDoc.data.id) {
|
2208 | id = jsonApiDoc.data.id;
|
2209 | }
|
2210 |
|
2211 | return id;
|
2212 | }
|
2213 |
|
2214 | _getJsonApiDocForRequest(request, modelName) {
|
2215 | let body;
|
2216 |
|
2217 | if (request && request.requestBody) {
|
2218 | body = JSON.parse(request.requestBody);
|
2219 | }
|
2220 |
|
2221 | return this.serializerOrRegistry.normalize(body, modelName);
|
2222 | }
|
2223 |
|
2224 | _getAttrsForRequest(request, modelName) {
|
2225 | let json = this._getJsonApiDocForRequest(request, modelName);
|
2226 |
|
2227 | let id = this._getIdForRequest(request, json);
|
2228 |
|
2229 | let attrs = {};
|
2230 | assert(json.data && (json.data.attributes || json.data.type || json.data.relationships), `You're using a shorthand or #normalizedRequestAttrs, but your serializer's normalize function did not return a valid JSON:API document. Consult the docs for the normalize hook on the Serializer class.`);
|
2231 |
|
2232 | if (json.data.attributes) {
|
2233 | attrs = Object.keys(json.data.attributes).reduce((sum, key) => {
|
2234 | sum[camelize(key)] = json.data.attributes[key];
|
2235 | return sum;
|
2236 | }, {});
|
2237 | }
|
2238 |
|
2239 | if (json.data.relationships) {
|
2240 | Object.keys(json.data.relationships).forEach(relationshipName => {
|
2241 | let relationship = json.data.relationships[relationshipName];
|
2242 | let modelClass = this.schema.modelClassFor(modelName);
|
2243 | let association = modelClass.associationFor(camelize(relationshipName));
|
2244 | let valueForRelationship;
|
2245 | assert(association, `You're passing the relationship '${relationshipName}' to the '${modelName}' model via a ${request.method} to '${request.url}', but you did not define the '${relationshipName}' association on the '${modelName}' model.`);
|
2246 |
|
2247 | if (association.isPolymorphic) {
|
2248 | valueForRelationship = relationship.data;
|
2249 | } else if (association instanceof HasMany) {
|
2250 | valueForRelationship = relationship.data && relationship.data.map(rel => rel.id);
|
2251 | } else {
|
2252 | valueForRelationship = relationship.data && relationship.data.id;
|
2253 | }
|
2254 |
|
2255 | attrs[association.identifier] = valueForRelationship;
|
2256 | }, {});
|
2257 | }
|
2258 |
|
2259 | if (id) {
|
2260 | attrs.id = id;
|
2261 | }
|
2262 |
|
2263 | return attrs;
|
2264 | }
|
2265 |
|
2266 | _getAttrsForFormRequest({
|
2267 | requestBody
|
2268 | }) {
|
2269 | let attrs;
|
2270 | let urlEncodedParts = [];
|
2271 | assert(requestBody && typeof requestBody === "string", `You're using the helper method #normalizedFormData, but the request body is empty or not a valid url encoded string.`);
|
2272 | urlEncodedParts = requestBody.split("&");
|
2273 | attrs = urlEncodedParts.reduce((a, urlEncodedPart) => {
|
2274 | let [key, value] = urlEncodedPart.split("=");
|
2275 | a[key] = decodeURIComponent(value.replace(/\+/g, " "));
|
2276 | return a;
|
2277 | }, {});
|
2278 | return attrs;
|
2279 | }
|
2280 |
|
2281 | }
|
2282 |
|
2283 |
|
2284 |
|
2285 |
|
2286 |
|
2287 | class FunctionRouteHandler extends BaseRouteHandler {
|
2288 | constructor(schema, serializerOrRegistry, userFunction, path, server) {
|
2289 | super(server);
|
2290 | this.schema = schema;
|
2291 | this.serializerOrRegistry = serializerOrRegistry;
|
2292 | this.userFunction = userFunction;
|
2293 | this.path = path;
|
2294 | }
|
2295 |
|
2296 | handle(request) {
|
2297 | return this.userFunction(this.schema, request);
|
2298 | }
|
2299 |
|
2300 | setRequest(request) {
|
2301 | this.request = request;
|
2302 | }
|
2303 |
|
2304 | serialize(response, serializerType) {
|
2305 | let serializer;
|
2306 |
|
2307 | if (serializerType) {
|
2308 | serializer = this.serializerOrRegistry.serializerFor(serializerType, {
|
2309 | explicit: true
|
2310 | });
|
2311 | } else {
|
2312 | serializer = this.serializerOrRegistry;
|
2313 | }
|
2314 |
|
2315 | return serializer.serialize(response, this.request);
|
2316 | }
|
2317 |
|
2318 | normalizedRequestAttrs(modelName = null) {
|
2319 | let {
|
2320 | path,
|
2321 | request,
|
2322 | request: {
|
2323 | requestHeaders
|
2324 | }
|
2325 | } = this;
|
2326 | let attrs;
|
2327 | let lowerCaseHeaders = {};
|
2328 |
|
2329 | for (let header in requestHeaders) {
|
2330 | lowerCaseHeaders[header.toLowerCase()] = requestHeaders[header];
|
2331 | }
|
2332 |
|
2333 | if (/x-www-form-urlencoded/.test(lowerCaseHeaders["content-type"])) {
|
2334 | attrs = this._getAttrsForFormRequest(request);
|
2335 | } else {
|
2336 | if (modelName) {
|
2337 | assert(dasherize(modelName) === modelName, `You called normalizedRequestAttrs('${modelName}'), but normalizedRequestAttrs was intended to be used with the dasherized version of the model type. Please change this to normalizedRequestAttrs('${dasherize(modelName)}').`);
|
2338 | } else {
|
2339 | modelName = this.getModelClassFromPath(path);
|
2340 | }
|
2341 |
|
2342 | assert(this.schema.hasModelForModelName(modelName), `You're using a shorthand or the #normalizedRequestAttrs helper but the detected model of '${modelName}' does not exist. You might need to pass in the correct modelName as the first argument to #normalizedRequestAttrs.`);
|
2343 | attrs = this._getAttrsForRequest(request, modelName);
|
2344 | }
|
2345 |
|
2346 | return attrs;
|
2347 | }
|
2348 |
|
2349 | }
|
2350 |
|
2351 |
|
2352 |
|
2353 |
|
2354 | class ObjectRouteHandler {
|
2355 | constructor(schema, serializerOrRegistry, object) {
|
2356 | this.schema = schema;
|
2357 | this.serializerOrRegistry = serializerOrRegistry;
|
2358 | this.object = object;
|
2359 | }
|
2360 |
|
2361 | handle()
|
2362 |
|
2363 | {
|
2364 | return this.object;
|
2365 | }
|
2366 |
|
2367 | }
|
2368 |
|
2369 |
|
2370 |
|
2371 |
|
2372 |
|
2373 | class BaseShorthandRouteHandler extends BaseRouteHandler {
|
2374 | constructor(schema, serializerOrRegistry, shorthand, path, options = {}) {
|
2375 | super();
|
2376 | shorthand = shorthand || this.getModelClassFromPath(path);
|
2377 | this.schema = schema;
|
2378 | this.serializerOrRegistry = serializerOrRegistry;
|
2379 | this.shorthand = shorthand;
|
2380 | this.options = options;
|
2381 | let type = Array.isArray(shorthand) ? "array" : typeof shorthand;
|
2382 |
|
2383 | if (type === "string") {
|
2384 | let modelClass = this.schema[this.schema.toCollectionName(shorthand)];
|
2385 |
|
2386 | this.handle = request => {
|
2387 | return this.handleStringShorthand(request, modelClass);
|
2388 | };
|
2389 | } else if (type === "array") {
|
2390 | let modelClasses = shorthand.map(modelName => this.schema[this.schema.toCollectionName(modelName)]);
|
2391 |
|
2392 | this.handle = request => {
|
2393 | return this.handleArrayShorthand(request, modelClasses);
|
2394 | };
|
2395 | }
|
2396 | }
|
2397 |
|
2398 |
|
2399 |
|
2400 |
|
2401 |
|
2402 |
|
2403 |
|
2404 |
|
2405 | }
|
2406 |
|
2407 |
|
2408 |
|
2409 |
|
2410 |
|
2411 | class GetShorthandRouteHandler extends BaseShorthandRouteHandler {
|
2412 | |
2413 |
|
2414 |
|
2415 |
|
2416 |
|
2417 |
|
2418 | handleStringShorthand(request, modelClass) {
|
2419 | let modelName = this.shorthand;
|
2420 | let camelizedModelName = camelize(modelName);
|
2421 | assert(modelClass, `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist.`);
|
2422 |
|
2423 | let id = this._getIdForRequest(request);
|
2424 |
|
2425 | if (id) {
|
2426 | let model = modelClass.find(id);
|
2427 |
|
2428 | if (!model) {
|
2429 | return new Response(404);
|
2430 | } else {
|
2431 | return model;
|
2432 | }
|
2433 | } else if (this.options.coalesce) {
|
2434 | let ids = this.serializerOrRegistry.getCoalescedIds(request, camelizedModelName);
|
2435 |
|
2436 | if (ids) {
|
2437 | return modelClass.find(ids);
|
2438 | }
|
2439 | }
|
2440 |
|
2441 | return modelClass.all();
|
2442 | }
|
2443 | |
2444 |
|
2445 |
|
2446 |
|
2447 |
|
2448 |
|
2449 | handleArrayShorthand(request, modelClasses) {
|
2450 | let keys = this.shorthand;
|
2451 |
|
2452 | let id = this._getIdForRequest(request);
|
2453 | |
2454 |
|
2455 |
|
2456 |
|
2457 |
|
2458 |
|
2459 |
|
2460 |
|
2461 |
|
2462 | assert(!id || this._container.inflector.singularize(keys[0]) !== keys[0], `It looks like you're using the "Single record with
|
2463 | related records" version of the array shorthand, in addition to opting
|
2464 | in to the model layer. This shorthand was made when there was no
|
2465 | serializer layer. Now that you're using models, please ensure your
|
2466 | relationships are defined, and create a serializer for the parent
|
2467 | model, adding the relationships there.`);
|
2468 | return modelClasses.map(modelClass => modelClass.all());
|
2469 | }
|
2470 |
|
2471 | }
|
2472 |
|
2473 |
|
2474 |
|
2475 |
|
2476 |
|
2477 | class PostShorthandRouteHandler extends BaseShorthandRouteHandler {
|
2478 | |
2479 |
|
2480 |
|
2481 |
|
2482 |
|
2483 | handleStringShorthand(request, modelClass) {
|
2484 | let modelName = this.shorthand;
|
2485 | let camelizedModelName = camelize(modelName);
|
2486 | assert(modelClass, `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist.`);
|
2487 |
|
2488 | let attrs = this._getAttrsForRequest(request, modelClass.camelizedModelName);
|
2489 |
|
2490 | return modelClass.create(attrs);
|
2491 | }
|
2492 |
|
2493 | }
|
2494 |
|
2495 |
|
2496 |
|
2497 |
|
2498 |
|
2499 | class PutShorthandRouteHandler extends BaseShorthandRouteHandler {
|
2500 | |
2501 |
|
2502 |
|
2503 |
|
2504 | handleStringShorthand(request, modelClass) {
|
2505 | let modelName = this.shorthand;
|
2506 | let camelizedModelName = camelize(modelName);
|
2507 | assert(modelClass, `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist.`);
|
2508 |
|
2509 | let id = this._getIdForRequest(request);
|
2510 |
|
2511 | let model = modelClass.find(id);
|
2512 |
|
2513 | if (!model) {
|
2514 | return new Response(404);
|
2515 | }
|
2516 |
|
2517 | let attrs = this._getAttrsForRequest(request, modelClass.camelizedModelName);
|
2518 |
|
2519 | return model.update(attrs);
|
2520 | }
|
2521 |
|
2522 | }
|
2523 |
|
2524 |
|
2525 |
|
2526 |
|
2527 |
|
2528 | class DeleteShorthandRouteHandler extends BaseShorthandRouteHandler {
|
2529 | |
2530 |
|
2531 |
|
2532 |
|
2533 |
|
2534 | handleStringShorthand(request, modelClass) {
|
2535 | let modelName = this.shorthand;
|
2536 | let camelizedModelName = camelize(modelName);
|
2537 | assert(modelClass, `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist.`);
|
2538 |
|
2539 | let id = this._getIdForRequest(request);
|
2540 |
|
2541 | let model = modelClass.find(id);
|
2542 |
|
2543 | if (!model) {
|
2544 | return new Response(404);
|
2545 | }
|
2546 |
|
2547 | model.destroy();
|
2548 | }
|
2549 | |
2550 |
|
2551 |
|
2552 |
|
2553 |
|
2554 |
|
2555 |
|
2556 |
|
2557 | handleArrayShorthand(request, modelClasses) {
|
2558 | let id = this._getIdForRequest(request);
|
2559 |
|
2560 | let parent = modelClasses[0].find(id);
|
2561 | let childTypes = modelClasses.slice(1).map(modelClass => this._container.inflector.pluralize(modelClass.camelizedModelName));
|
2562 |
|
2563 | childTypes.forEach(type => parent[type].destroy());
|
2564 | parent.destroy();
|
2565 | }
|
2566 |
|
2567 | }
|
2568 |
|
2569 |
|
2570 |
|
2571 |
|
2572 |
|
2573 | class HeadShorthandRouteHandler extends BaseShorthandRouteHandler {
|
2574 | |
2575 |
|
2576 |
|
2577 |
|
2578 |
|
2579 |
|
2580 | handleStringShorthand(request, modelClass) {
|
2581 | let modelName = this.shorthand;
|
2582 | let camelizedModelName = camelize(modelName);
|
2583 | assert(modelClass, `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist.`);
|
2584 |
|
2585 | let id = this._getIdForRequest(request);
|
2586 |
|
2587 | if (id) {
|
2588 | let model = modelClass.find(id);
|
2589 |
|
2590 | if (!model) {
|
2591 | return new Response(404);
|
2592 | } else {
|
2593 | return new Response(204);
|
2594 | }
|
2595 | } else if (this.options.coalesce && request.queryParams && request.queryParams.ids) {
|
2596 | let model = modelClass.find(request.queryParams.ids);
|
2597 |
|
2598 | if (!model) {
|
2599 | return new Response(404);
|
2600 | } else {
|
2601 | return new Response(204);
|
2602 | }
|
2603 | } else {
|
2604 | return new Response(204);
|
2605 | }
|
2606 | }
|
2607 |
|
2608 | }
|
2609 |
|
2610 | const DEFAULT_CODES = {
|
2611 | get: 200,
|
2612 | put: 204,
|
2613 | post: 201,
|
2614 | delete: 204
|
2615 | };
|
2616 |
|
2617 | function createHandler({
|
2618 | verb,
|
2619 | schema,
|
2620 | serializerOrRegistry,
|
2621 | path,
|
2622 | rawHandler,
|
2623 | options
|
2624 | }) {
|
2625 | let handler;
|
2626 | let args = [schema, serializerOrRegistry, rawHandler, path, options];
|
2627 | let type = typeof rawHandler;
|
2628 |
|
2629 | if (type === "function") {
|
2630 | handler = new FunctionRouteHandler(...args);
|
2631 | } else if (type === "object" && rawHandler) {
|
2632 | handler = new ObjectRouteHandler(...args);
|
2633 | } else if (verb === "get") {
|
2634 | handler = new GetShorthandRouteHandler(...args);
|
2635 | } else if (verb === "post") {
|
2636 | handler = new PostShorthandRouteHandler(...args);
|
2637 | } else if (verb === "put" || verb === "patch") {
|
2638 | handler = new PutShorthandRouteHandler(...args);
|
2639 | } else if (verb === "delete") {
|
2640 | handler = new DeleteShorthandRouteHandler(...args);
|
2641 | } else if (verb === "head") {
|
2642 | handler = new HeadShorthandRouteHandler(...args);
|
2643 | }
|
2644 |
|
2645 | return handler;
|
2646 | }
|
2647 |
|
2648 |
|
2649 |
|
2650 |
|
2651 |
|
2652 | class RouteHandler {
|
2653 | constructor({
|
2654 | schema,
|
2655 | verb,
|
2656 | rawHandler,
|
2657 | customizedCode,
|
2658 | options,
|
2659 | path,
|
2660 | serializerOrRegistry
|
2661 | }) {
|
2662 | this.verb = verb;
|
2663 | this.customizedCode = customizedCode;
|
2664 | this.serializerOrRegistry = serializerOrRegistry;
|
2665 | this.handler = createHandler({
|
2666 | verb,
|
2667 | schema,
|
2668 | path,
|
2669 | serializerOrRegistry,
|
2670 | rawHandler,
|
2671 | options
|
2672 | });
|
2673 | }
|
2674 |
|
2675 | handle(request) {
|
2676 | return this._getMirageResponseForRequest(request).then(mirageResponse => this.serialize(mirageResponse, request)).then(serializedMirageResponse => {
|
2677 | return serializedMirageResponse.toRackResponse();
|
2678 | });
|
2679 | }
|
2680 |
|
2681 | _getMirageResponseForRequest(request) {
|
2682 | let result;
|
2683 |
|
2684 | try {
|
2685 | |
2686 |
|
2687 |
|
2688 |
|
2689 | if (this.handler instanceof FunctionRouteHandler) {
|
2690 | this.handler.setRequest(request);
|
2691 | }
|
2692 |
|
2693 | result = this.handler.handle(request);
|
2694 | } catch (e) {
|
2695 | if (e instanceof MirageError) {
|
2696 | result = new Response(500, {}, e);
|
2697 | } else {
|
2698 | let message = e.message || e;
|
2699 | result = new Response(500, {}, {
|
2700 | message,
|
2701 | stack: `Mirage: Your ${request.method} handler for the url ${request.url} threw an error:\n\n${e.stack || e}`
|
2702 | });
|
2703 | }
|
2704 | }
|
2705 |
|
2706 | return this._toMirageResponse(result);
|
2707 | }
|
2708 |
|
2709 | _toMirageResponse(result) {
|
2710 | let mirageResponse;
|
2711 | return new Promise((resolve, reject) => {
|
2712 | Promise.resolve(result).then(response => {
|
2713 | if (response instanceof Response) {
|
2714 | mirageResponse = result;
|
2715 | } else {
|
2716 | let code = this._getCodeForResponse(response);
|
2717 |
|
2718 | mirageResponse = new Response(code, {}, response);
|
2719 | }
|
2720 |
|
2721 | resolve(mirageResponse);
|
2722 | }).catch(reject);
|
2723 | });
|
2724 | }
|
2725 |
|
2726 | _getCodeForResponse(response) {
|
2727 | let code;
|
2728 |
|
2729 | if (this.customizedCode) {
|
2730 | code = this.customizedCode;
|
2731 | } else {
|
2732 | code = DEFAULT_CODES[this.verb];
|
2733 |
|
2734 | if (code === 204 && response !== undefined && response !== "") {
|
2735 | code = 200;
|
2736 | }
|
2737 | }
|
2738 |
|
2739 | return code;
|
2740 | }
|
2741 |
|
2742 | serialize(mirageResponse, request) {
|
2743 | mirageResponse.data = this.serializerOrRegistry.serialize(mirageResponse.data, request);
|
2744 | return mirageResponse;
|
2745 | }
|
2746 |
|
2747 | }
|
2748 |
|
2749 |
|
2750 |
|
2751 |
|
2752 |
|
2753 | function extend(protoProps, staticProps) {
|
2754 | class Child extends this {
|
2755 | constructor(...args) {
|
2756 | super(...args);
|
2757 |
|
2758 |
|
2759 | if (protoProps && has(protoProps, "constructor")) {
|
2760 | protoProps.constructor.call(this, ...args);
|
2761 | }
|
2762 | }
|
2763 |
|
2764 | }
|
2765 |
|
2766 |
|
2767 | Object.assign(Child, this, staticProps);
|
2768 |
|
2769 |
|
2770 | if (protoProps) {
|
2771 | Object.assign(Child.prototype, protoProps);
|
2772 | }
|
2773 |
|
2774 | return Child;
|
2775 | }
|
2776 |
|
2777 |
|
2778 |
|
2779 |
|
2780 |
|
2781 |
|
2782 |
|
2783 |
|
2784 |
|
2785 |
|
2786 |
|
2787 |
|
2788 |
|
2789 |
|
2790 |
|
2791 |
|
2792 |
|
2793 |
|
2794 |
|
2795 |
|
2796 |
|
2797 |
|
2798 |
|
2799 |
|
2800 |
|
2801 |
|
2802 |
|
2803 |
|
2804 |
|
2805 |
|
2806 |
|
2807 |
|
2808 |
|
2809 |
|
2810 |
|
2811 |
|
2812 |
|
2813 |
|
2814 |
|
2815 |
|
2816 |
|
2817 |
|
2818 |
|
2819 |
|
2820 |
|
2821 |
|
2822 | class Model {
|
2823 |
|
2824 |
|
2825 | |
2826 |
|
2827 |
|
2828 |
|
2829 |
|
2830 |
|
2831 | constructor(schema, modelName, attrs, fks) {
|
2832 | assert(schema, "A model requires a schema");
|
2833 | assert(modelName, "A model requires a modelName");
|
2834 | this._schema = schema;
|
2835 | this.modelName = modelName;
|
2836 | this.fks = fks || [];
|
2837 | |
2838 |
|
2839 |
|
2840 |
|
2841 |
|
2842 |
|
2843 |
|
2844 |
|
2845 |
|
2846 |
|
2847 |
|
2848 | this.attrs = {};
|
2849 | attrs = attrs || {};
|
2850 |
|
2851 | this.fks.forEach(fk => {
|
2852 | this.attrs[fk] = attrs[fk] !== undefined ? attrs[fk] : null;
|
2853 | });
|
2854 | Object.keys(attrs).forEach(name => {
|
2855 | const value = attrs[name];
|
2856 |
|
2857 | this._validateAttr(name, value);
|
2858 |
|
2859 | this._setupAttr(name, value);
|
2860 |
|
2861 | this._setupRelationship(name, value);
|
2862 | });
|
2863 | return this;
|
2864 | }
|
2865 | |
2866 |
|
2867 |
|
2868 |
|
2869 |
|
2870 |
|
2871 |
|
2872 |
|
2873 |
|
2874 |
|
2875 |
|
2876 |
|
2877 |
|
2878 |
|
2879 |
|
2880 |
|
2881 | save() {
|
2882 | let collection = this._schema.toInternalCollectionName(this.modelName);
|
2883 |
|
2884 | if (this.isNew()) {
|
2885 |
|
2886 | this.attrs = this._schema.db[collection].insert(this.attrs);
|
2887 |
|
2888 | this._definePlainAttribute("id");
|
2889 | } else {
|
2890 | this._schema.isSaving[this.toString()] = true;
|
2891 |
|
2892 | this._schema.db[collection].update(this.attrs.id, this.attrs);
|
2893 | }
|
2894 |
|
2895 | this._saveAssociations();
|
2896 |
|
2897 | this._schema.isSaving[this.toString()] = false;
|
2898 | return this;
|
2899 | }
|
2900 | |
2901 |
|
2902 |
|
2903 |
|
2904 |
|
2905 |
|
2906 |
|
2907 |
|
2908 |
|
2909 |
|
2910 |
|
2911 |
|
2912 |
|
2913 |
|
2914 |
|
2915 |
|
2916 |
|
2917 |
|
2918 | update(key, val) {
|
2919 | let attrs;
|
2920 |
|
2921 | if (key == null) {
|
2922 | return this;
|
2923 | }
|
2924 |
|
2925 | if (typeof key === "object") {
|
2926 | attrs = key;
|
2927 | } else {
|
2928 | (attrs = {})[key] = val;
|
2929 | }
|
2930 |
|
2931 | Object.keys(attrs).forEach(function (attr) {
|
2932 | if (!this.associationKeys.has(attr) && !this.associationIdKeys.has(attr)) {
|
2933 | this._definePlainAttribute(attr);
|
2934 | }
|
2935 |
|
2936 | this[attr] = attrs[attr];
|
2937 | }, this);
|
2938 | this.save();
|
2939 | return this;
|
2940 | }
|
2941 | |
2942 |
|
2943 |
|
2944 |
|
2945 |
|
2946 |
|
2947 |
|
2948 |
|
2949 |
|
2950 |
|
2951 |
|
2952 | destroy() {
|
2953 | if (this.isSaved()) {
|
2954 | this._disassociateFromDependents();
|
2955 |
|
2956 | let collection = this._schema.toInternalCollectionName(this.modelName);
|
2957 |
|
2958 | this._schema.db[collection].remove(this.attrs.id);
|
2959 | }
|
2960 | }
|
2961 | |
2962 |
|
2963 |
|
2964 |
|
2965 |
|
2966 |
|
2967 |
|
2968 |
|
2969 |
|
2970 |
|
2971 |
|
2972 |
|
2973 |
|
2974 |
|
2975 |
|
2976 |
|
2977 | isNew() {
|
2978 | let hasDbRecord = false;
|
2979 | let hasId = this.attrs.id !== undefined && this.attrs.id !== null;
|
2980 |
|
2981 | if (hasId) {
|
2982 | let collectionName = this._schema.toInternalCollectionName(this.modelName);
|
2983 |
|
2984 | let record = this._schema.db[collectionName].find(this.attrs.id);
|
2985 |
|
2986 | if (record) {
|
2987 | hasDbRecord = true;
|
2988 | }
|
2989 | }
|
2990 |
|
2991 | return !hasDbRecord;
|
2992 | }
|
2993 | |
2994 |
|
2995 |
|
2996 |
|
2997 |
|
2998 |
|
2999 |
|
3000 |
|
3001 | isSaved() {
|
3002 | return !this.isNew();
|
3003 | }
|
3004 | |
3005 |
|
3006 |
|
3007 |
|
3008 |
|
3009 |
|
3010 |
|
3011 |
|
3012 |
|
3013 |
|
3014 |
|
3015 |
|
3016 |
|
3017 |
|
3018 |
|
3019 |
|
3020 | reload() {
|
3021 | if (this.id) {
|
3022 | let collection = this._schema.toInternalCollectionName(this.modelName);
|
3023 |
|
3024 | let attrs = this._schema.db[collection].find(this.id);
|
3025 |
|
3026 | Object.keys(attrs).filter(function (attr) {
|
3027 | return attr !== "id";
|
3028 | }).forEach(function (attr) {
|
3029 | this.attrs[attr] = attrs[attr];
|
3030 | }, this);
|
3031 | }
|
3032 |
|
3033 |
|
3034 | this._tempAssociations = {};
|
3035 | return this;
|
3036 | }
|
3037 |
|
3038 | toJSON() {
|
3039 | return this.attrs;
|
3040 | }
|
3041 | |
3042 |
|
3043 |
|
3044 |
|
3045 |
|
3046 |
|
3047 |
|
3048 |
|
3049 |
|
3050 | associationFor(key) {
|
3051 | return this._schema.associationsFor(this.modelName)[key];
|
3052 | }
|
3053 | |
3054 |
|
3055 |
|
3056 |
|
3057 |
|
3058 |
|
3059 |
|
3060 |
|
3061 |
|
3062 |
|
3063 |
|
3064 |
|
3065 |
|
3066 |
|
3067 |
|
3068 |
|
3069 |
|
3070 |
|
3071 |
|
3072 |
|
3073 |
|
3074 |
|
3075 |
|
3076 |
|
3077 |
|
3078 |
|
3079 |
|
3080 |
|
3081 |
|
3082 |
|
3083 |
|
3084 |
|
3085 |
|
3086 |
|
3087 |
|
3088 |
|
3089 | inverseFor(association) {
|
3090 | return this._explicitInverseFor(association) || this._implicitInverseFor(association);
|
3091 | }
|
3092 | |
3093 |
|
3094 |
|
3095 |
|
3096 |
|
3097 |
|
3098 |
|
3099 | _explicitInverseFor(association) {
|
3100 | this._checkForMultipleExplicitInverses(association);
|
3101 |
|
3102 | let associations = this._schema.associationsFor(this.modelName);
|
3103 |
|
3104 | let inverse = association.opts.inverse;
|
3105 | let candidate = inverse ? associations[inverse] : null;
|
3106 | let matchingPolymorphic = candidate && candidate.isPolymorphic;
|
3107 | let matchingInverse = candidate && candidate.modelName === association.ownerModelName;
|
3108 | let candidateInverse = candidate && candidate.opts.inverse;
|
3109 |
|
3110 | if (candidateInverse && candidate.opts.inverse !== association.key) {
|
3111 | assert(false, `You specified an inverse of ${inverse} for ${association.key}, but it does not match ${candidate.modelName} ${candidate.key}'s inverse`);
|
3112 | }
|
3113 |
|
3114 | return matchingPolymorphic || matchingInverse ? candidate : null;
|
3115 | }
|
3116 | |
3117 |
|
3118 |
|
3119 |
|
3120 |
|
3121 |
|
3122 |
|
3123 |
|
3124 |
|
3125 | _checkForMultipleExplicitInverses(association) {
|
3126 | let associations = this._schema.associationsFor(this.modelName);
|
3127 |
|
3128 | let matchingExplicitInverses = Object.keys(associations).filter(key => {
|
3129 | let candidate = associations[key];
|
3130 | let modelMatches = association.ownerModelName === candidate.modelName;
|
3131 | let inverseKeyMatches = association.key === candidate.opts.inverse;
|
3132 | return modelMatches && inverseKeyMatches;
|
3133 | });
|
3134 | assert(matchingExplicitInverses.length <= 1, `The ${this.modelName} model has defined multiple explicit inverse associations for the ${association.ownerModelName}.${association.key} association.`);
|
3135 | }
|
3136 | |
3137 |
|
3138 |
|
3139 |
|
3140 |
|
3141 |
|
3142 |
|
3143 |
|
3144 | _implicitInverseFor(association) {
|
3145 | let associations = this._schema.associationsFor(this.modelName);
|
3146 |
|
3147 | let modelName = association.ownerModelName;
|
3148 | return values(associations).filter(candidate => candidate.modelName === modelName).reduce((inverse, candidate) => {
|
3149 | let candidateInverse = candidate.opts.inverse;
|
3150 | let candidateIsImplicitInverse = candidateInverse === undefined;
|
3151 | let candidateIsExplicitInverse = candidateInverse === association.key;
|
3152 | let candidateMatches = candidateIsImplicitInverse || candidateIsExplicitInverse;
|
3153 |
|
3154 | if (candidateMatches) {
|
3155 |
|
3156 | assert(!inverse, `The ${this.modelName} model has multiple possible inverse associations for the ${association.ownerModelName}.${association.key} association.`);
|
3157 | inverse = candidate;
|
3158 | }
|
3159 |
|
3160 | return inverse;
|
3161 | }, null);
|
3162 | }
|
3163 | |
3164 |
|
3165 |
|
3166 |
|
3167 |
|
3168 |
|
3169 |
|
3170 |
|
3171 |
|
3172 |
|
3173 |
|
3174 |
|
3175 | hasInverseFor(association) {
|
3176 | return !!this.inverseFor(association);
|
3177 | }
|
3178 | |
3179 |
|
3180 |
|
3181 |
|
3182 |
|
3183 |
|
3184 |
|
3185 |
|
3186 | alreadyAssociatedWith(model, association) {
|
3187 | let {
|
3188 | key
|
3189 | } = association;
|
3190 | let associatedModelOrCollection = this[key];
|
3191 |
|
3192 | if (associatedModelOrCollection && model) {
|
3193 | if (associatedModelOrCollection instanceof Model) {
|
3194 | if (associatedModelOrCollection.isSaved() && model.isSaved()) {
|
3195 | return associatedModelOrCollection.toString() === model.toString();
|
3196 | } else {
|
3197 | return associatedModelOrCollection === model;
|
3198 | }
|
3199 | } else {
|
3200 | return associatedModelOrCollection.includes(model);
|
3201 | }
|
3202 | }
|
3203 | }
|
3204 |
|
3205 | associate(model, association) {
|
3206 | if (this.alreadyAssociatedWith(model, association)) {
|
3207 | return;
|
3208 | }
|
3209 |
|
3210 | let {
|
3211 | key
|
3212 | } = association;
|
3213 |
|
3214 | if (association instanceof HasMany) {
|
3215 | if (!this[key].includes(model)) {
|
3216 | this[key].add(model);
|
3217 | }
|
3218 | } else {
|
3219 | this[key] = model;
|
3220 | }
|
3221 | }
|
3222 |
|
3223 | disassociate(model, association) {
|
3224 | let fk = association.getForeignKey();
|
3225 |
|
3226 | if (association instanceof HasMany) {
|
3227 | let i;
|
3228 |
|
3229 | if (association.isPolymorphic) {
|
3230 | let found = this[fk].find(({
|
3231 | type,
|
3232 | id
|
3233 | }) => type === model.modelName && id === model.id);
|
3234 | i = found && this[fk].indexOf(found);
|
3235 | } else {
|
3236 | i = this[fk].map(key => key.toString()).indexOf(model.id.toString());
|
3237 | }
|
3238 |
|
3239 | if (i > -1) {
|
3240 | this.attrs[fk].splice(i, 1);
|
3241 | }
|
3242 | } else {
|
3243 | this.attrs[fk] = null;
|
3244 | }
|
3245 | }
|
3246 | |
3247 |
|
3248 |
|
3249 |
|
3250 |
|
3251 | get isSaving() {
|
3252 | return this._schema.isSaving[this.toString()];
|
3253 | }
|
3254 |
|
3255 | |
3256 |
|
3257 |
|
3258 |
|
3259 |
|
3260 |
|
3261 |
|
3262 |
|
3263 |
|
3264 |
|
3265 |
|
3266 | _setupAttr(attr, value) {
|
3267 | const isAssociation = this.associationKeys.has(attr) || this.associationIdKeys.has(attr);
|
3268 |
|
3269 | if (!isAssociation) {
|
3270 | this.attrs[attr] = value;
|
3271 |
|
3272 | this._definePlainAttribute(attr);
|
3273 | }
|
3274 | }
|
3275 | |
3276 |
|
3277 |
|
3278 |
|
3279 |
|
3280 |
|
3281 |
|
3282 |
|
3283 |
|
3284 | _definePlainAttribute(attr) {
|
3285 |
|
3286 | let existingProperty = Object.getOwnPropertyDescriptor(this, attr);
|
3287 |
|
3288 | if (existingProperty && existingProperty.get) {
|
3289 | return;
|
3290 | }
|
3291 |
|
3292 |
|
3293 | if (!Object.prototype.hasOwnProperty.call(this.attrs, attr)) {
|
3294 | this.attrs[attr] = null;
|
3295 | }
|
3296 |
|
3297 |
|
3298 | Object.defineProperty(this, attr, {
|
3299 | get() {
|
3300 | return this.attrs[attr];
|
3301 | },
|
3302 |
|
3303 | set(val) {
|
3304 | this.attrs[attr] = val;
|
3305 | return this;
|
3306 | }
|
3307 |
|
3308 | });
|
3309 | }
|
3310 | |
3311 |
|
3312 |
|
3313 |
|
3314 |
|
3315 |
|
3316 |
|
3317 |
|
3318 |
|
3319 |
|
3320 |
|
3321 |
|
3322 |
|
3323 |
|
3324 | _setupRelationship(attr, value) {
|
3325 | const isFk = this.associationIdKeys.has(attr) || this.fks.includes(attr);
|
3326 | const isAssociation = this.associationKeys.has(attr);
|
3327 |
|
3328 | if (isFk) {
|
3329 | if (value !== undefined && value !== null) {
|
3330 | this._validateForeignKeyExistsInDatabase(attr, value);
|
3331 | }
|
3332 |
|
3333 | this.attrs[attr] = value;
|
3334 | }
|
3335 |
|
3336 | if (isAssociation) {
|
3337 | this[attr] = value;
|
3338 | }
|
3339 | }
|
3340 | |
3341 |
|
3342 |
|
3343 |
|
3344 |
|
3345 |
|
3346 |
|
3347 | _validateAttr(key, value) {
|
3348 |
|
3349 | {
|
3350 | if (this.associationKeys.has(key)) {
|
3351 | let association = this.associationFor(key);
|
3352 | let isNull = value === null;
|
3353 |
|
3354 | if (association instanceof HasMany) {
|
3355 | let isCollection = value instanceof Collection || value instanceof PolymorphicCollection;
|
3356 | let isArrayOfModels = Array.isArray(value) && value.every(item => item instanceof Model);
|
3357 | assert(isCollection || isArrayOfModels || isNull, `You're trying to create a ${this.modelName} model and you passed in "${value}" under the ${key} key, but that key is a HasMany relationship. You must pass in a Collection, PolymorphicCollection, array of Models, or null.`);
|
3358 | } else if (association instanceof BelongsTo) {
|
3359 | assert(value instanceof Model || isNull, `You're trying to create a ${this.modelName} model and you passed in "${value}" under the ${key} key, but that key is a BelongsTo relationship. You must pass in a Model or null.`);
|
3360 | }
|
3361 | }
|
3362 | }
|
3363 |
|
3364 | {
|
3365 | if (this.associationIdKeys.has(key)) {
|
3366 | if (key.endsWith("Ids")) {
|
3367 | let isArray = Array.isArray(value);
|
3368 | let isNull = value === null;
|
3369 | assert(isArray || isNull, `You're trying to create a ${this.modelName} model and you passed in "${value}" under the ${key} key, but that key is a foreign key for a HasMany relationship. You must pass in an array of ids or null.`);
|
3370 | }
|
3371 | }
|
3372 | }
|
3373 |
|
3374 | {
|
3375 | let isModelOrCollection = value instanceof Model || value instanceof Collection || value instanceof PolymorphicCollection;
|
3376 | let isArrayOfModels = Array.isArray(value) && value.length && value.every(item => item instanceof Model);
|
3377 |
|
3378 | if (isModelOrCollection || isArrayOfModels) {
|
3379 | let modelOrCollection = value;
|
3380 | assert(this.associationKeys.has(key), `You're trying to create a ${this.modelName} model and you passed in a ${modelOrCollection.toString()} under the ${key} key, but you haven't defined that key as an association on your model.`);
|
3381 | }
|
3382 | }
|
3383 | }
|
3384 | |
3385 |
|
3386 |
|
3387 |
|
3388 |
|
3389 |
|
3390 |
|
3391 |
|
3392 |
|
3393 |
|
3394 |
|
3395 | _validateForeignKeyExistsInDatabase(foreignKeyName, foreignKeys) {
|
3396 | if (Array.isArray(foreignKeys)) {
|
3397 | let association = this.hasManyAssociationFks[foreignKeyName];
|
3398 | let found;
|
3399 |
|
3400 | if (association.isPolymorphic) {
|
3401 | found = foreignKeys.map(({
|
3402 | type,
|
3403 | id
|
3404 | }) => {
|
3405 | return this._schema.db[this._schema.toInternalCollectionName(type)].find(id);
|
3406 | });
|
3407 | found = compact(found);
|
3408 | } else {
|
3409 | found = this._schema.db[this._schema.toInternalCollectionName(association.modelName)].find(foreignKeys);
|
3410 | }
|
3411 |
|
3412 | let foreignKeyLabel = association.isPolymorphic ? foreignKeys.map(fk => `${fk.type}:${fk.id}`).join(",") : foreignKeys;
|
3413 | assert(found.length === foreignKeys.length, `You're instantiating a ${this.modelName} that has a ${foreignKeyName} of ${foreignKeyLabel}, but some of those records don't exist in the database.`);
|
3414 | } else {
|
3415 | let association = this.belongsToAssociationFks[foreignKeyName];
|
3416 | let found;
|
3417 |
|
3418 | if (association.isPolymorphic) {
|
3419 | found = this._schema.db[this._schema.toInternalCollectionName(foreignKeys.type)].find(foreignKeys.id);
|
3420 | } else {
|
3421 | found = this._schema.db[this._schema.toInternalCollectionName(association.modelName)].find(foreignKeys);
|
3422 | }
|
3423 |
|
3424 | let foreignKeyLabel = association.isPolymorphic ? `${foreignKeys.type}:${foreignKeys.id}` : foreignKeys;
|
3425 | assert(found, `You're instantiating a ${this.modelName} that has a ${foreignKeyName} of ${foreignKeyLabel}, but that record doesn't exist in the database.`);
|
3426 | }
|
3427 | }
|
3428 | |
3429 |
|
3430 |
|
3431 |
|
3432 |
|
3433 |
|
3434 |
|
3435 |
|
3436 |
|
3437 | _saveAssociations() {
|
3438 | this._saveBelongsToAssociations();
|
3439 |
|
3440 | this._saveHasManyAssociations();
|
3441 | }
|
3442 |
|
3443 | _saveBelongsToAssociations() {
|
3444 | values(this.belongsToAssociations).forEach(association => {
|
3445 | this._disassociateFromOldInverses(association);
|
3446 |
|
3447 | this._saveNewAssociates(association);
|
3448 |
|
3449 | this._associateWithNewInverses(association);
|
3450 | });
|
3451 | }
|
3452 |
|
3453 | _saveHasManyAssociations() {
|
3454 | values(this.hasManyAssociations).forEach(association => {
|
3455 | this._disassociateFromOldInverses(association);
|
3456 |
|
3457 | this._saveNewAssociates(association);
|
3458 |
|
3459 | this._associateWithNewInverses(association);
|
3460 | });
|
3461 | }
|
3462 |
|
3463 | _disassociateFromOldInverses(association) {
|
3464 | if (association instanceof HasMany) {
|
3465 | this._disassociateFromHasManyInverses(association);
|
3466 | } else if (association instanceof BelongsTo) {
|
3467 | this._disassociateFromBelongsToInverse(association);
|
3468 | }
|
3469 | }
|
3470 |
|
3471 |
|
3472 | _disassociateFromHasManyInverses(association) {
|
3473 | let {
|
3474 | key
|
3475 | } = association;
|
3476 | let fk = association.getForeignKey();
|
3477 | let tempAssociation = this._tempAssociations && this._tempAssociations[key];
|
3478 | let associateIds = this.attrs[fk];
|
3479 |
|
3480 | if (tempAssociation && associateIds) {
|
3481 | let models;
|
3482 |
|
3483 | if (association.isPolymorphic) {
|
3484 | models = associateIds.map(({
|
3485 | type,
|
3486 | id
|
3487 | }) => {
|
3488 | return this._schema[this._schema.toCollectionName(type)].find(id);
|
3489 | });
|
3490 | } else {
|
3491 |
|
3492 | models = this._schema[this._schema.toCollectionName(association.modelName)].find(associateIds || []).models;
|
3493 | }
|
3494 |
|
3495 | models.filter(associate =>
|
3496 | !associate.isSaving &&
|
3497 | !tempAssociation.includes(associate) && associate.hasInverseFor(association)).forEach(associate => {
|
3498 | let inverse = associate.inverseFor(association);
|
3499 | associate.disassociate(this, inverse);
|
3500 | associate.save();
|
3501 | });
|
3502 | }
|
3503 | }
|
3504 | |
3505 |
|
3506 |
|
3507 |
|
3508 |
|
3509 |
|
3510 |
|
3511 |
|
3512 |
|
3513 |
|
3514 |
|
3515 |
|
3516 |
|
3517 |
|
3518 |
|
3519 | _disassociateFromBelongsToInverse(association) {
|
3520 | let {
|
3521 | key
|
3522 | } = association;
|
3523 | let fk = association.getForeignKey();
|
3524 | let tempAssociation = this._tempAssociations && this._tempAssociations[key];
|
3525 | let associateId = this.attrs[fk];
|
3526 |
|
3527 | if (tempAssociation !== undefined && associateId) {
|
3528 | let associate;
|
3529 |
|
3530 | if (association.isPolymorphic) {
|
3531 | associate = this._schema[this._schema.toCollectionName(associateId.type)].find(associateId.id);
|
3532 | } else {
|
3533 | associate = this._schema[this._schema.toCollectionName(association.modelName)].find(associateId);
|
3534 | }
|
3535 |
|
3536 | if (associate.hasInverseFor(association)) {
|
3537 | let inverse = associate.inverseFor(association);
|
3538 | associate.disassociate(this, inverse);
|
3539 |
|
3540 | associate._updateInDb(associate.attrs);
|
3541 | }
|
3542 | }
|
3543 | }
|
3544 |
|
3545 |
|
3546 | _disassociateFromDependents() {
|
3547 | this._schema.dependentAssociationsFor(this.modelName).forEach(association => {
|
3548 | association.disassociateAllDependentsFromTarget(this);
|
3549 | });
|
3550 | }
|
3551 |
|
3552 | _saveNewAssociates(association) {
|
3553 | let {
|
3554 | key
|
3555 | } = association;
|
3556 | let fk = association.getForeignKey();
|
3557 | let tempAssociate = this._tempAssociations && this._tempAssociations[key];
|
3558 |
|
3559 | if (tempAssociate !== undefined) {
|
3560 | this.__isSavingNewChildren = true;
|
3561 | delete this._tempAssociations[key];
|
3562 |
|
3563 | if (tempAssociate instanceof Collection) {
|
3564 | tempAssociate.models.filter(model => !model.isSaving).forEach(child => {
|
3565 | child.save();
|
3566 | });
|
3567 |
|
3568 | this._updateInDb({
|
3569 | [fk]: tempAssociate.models.map(child => child.id)
|
3570 | });
|
3571 | } else if (tempAssociate instanceof PolymorphicCollection) {
|
3572 | tempAssociate.models.filter(model => !model.isSaving).forEach(child => {
|
3573 | child.save();
|
3574 | });
|
3575 |
|
3576 | this._updateInDb({
|
3577 | [fk]: tempAssociate.models.map(child => {
|
3578 | return {
|
3579 | type: child.modelName,
|
3580 | id: child.id
|
3581 | };
|
3582 | })
|
3583 | });
|
3584 | } else {
|
3585 |
|
3586 | if (tempAssociate === null) {
|
3587 | this._updateInDb({
|
3588 | [fk]: null
|
3589 | });
|
3590 |
|
3591 | } else if (this.equals(tempAssociate)) {
|
3592 | this._updateInDb({
|
3593 | [fk]: this.id
|
3594 | });
|
3595 |
|
3596 | } else if (!tempAssociate.isSaving) {
|
3597 |
|
3598 | tempAssociate.save();
|
3599 |
|
3600 | this._syncTempAssociations(tempAssociate);
|
3601 |
|
3602 | let fkValue;
|
3603 |
|
3604 | if (association.isPolymorphic) {
|
3605 | fkValue = {
|
3606 | id: tempAssociate.id,
|
3607 | type: tempAssociate.modelName
|
3608 | };
|
3609 | } else {
|
3610 | fkValue = tempAssociate.id;
|
3611 | }
|
3612 |
|
3613 | this._updateInDb({
|
3614 | [fk]: fkValue
|
3615 | });
|
3616 | }
|
3617 | }
|
3618 |
|
3619 | this.__isSavingNewChildren = false;
|
3620 | }
|
3621 | }
|
3622 | |
3623 |
|
3624 |
|
3625 |
|
3626 |
|
3627 |
|
3628 |
|
3629 |
|
3630 |
|
3631 |
|
3632 |
|
3633 |
|
3634 |
|
3635 | _associateWithNewInverses(association) {
|
3636 | if (!this.__isSavingNewChildren) {
|
3637 | let modelOrCollection = this[association.key];
|
3638 |
|
3639 | if (modelOrCollection instanceof Model) {
|
3640 | this._associateModelWithInverse(modelOrCollection, association);
|
3641 | } else if (modelOrCollection instanceof Collection || modelOrCollection instanceof PolymorphicCollection) {
|
3642 | modelOrCollection.models.forEach(model => {
|
3643 | this._associateModelWithInverse(model, association);
|
3644 | });
|
3645 | }
|
3646 |
|
3647 | delete this._tempAssociations[association.key];
|
3648 | }
|
3649 | }
|
3650 |
|
3651 | _associateModelWithInverse(model, association) {
|
3652 | if (model.hasInverseFor(association)) {
|
3653 | let inverse = model.inverseFor(association);
|
3654 | let inverseFk = inverse.getForeignKey();
|
3655 | let ownerId = this.id;
|
3656 |
|
3657 | if (inverse instanceof BelongsTo) {
|
3658 | let newId;
|
3659 |
|
3660 | if (inverse.isPolymorphic) {
|
3661 | newId = {
|
3662 | type: this.modelName,
|
3663 | id: ownerId
|
3664 | };
|
3665 | } else {
|
3666 | newId = ownerId;
|
3667 | }
|
3668 |
|
3669 | this._schema.db[this._schema.toInternalCollectionName(model.modelName)].update(model.id, {
|
3670 | [inverseFk]: newId
|
3671 | });
|
3672 | } else {
|
3673 | let inverseCollection = this._schema.db[this._schema.toInternalCollectionName(model.modelName)];
|
3674 |
|
3675 | let currentIdsForInverse = inverseCollection.find(model.id)[inverse.getForeignKey()] || [];
|
3676 | let newIdsForInverse = Object.assign([], currentIdsForInverse);
|
3677 | let newId, alreadyAssociatedWith;
|
3678 |
|
3679 | if (inverse.isPolymorphic) {
|
3680 | newId = {
|
3681 | type: this.modelName,
|
3682 | id: ownerId
|
3683 | };
|
3684 | alreadyAssociatedWith = newIdsForInverse.some(key => key.type == this.modelName && key.id == ownerId);
|
3685 | } else {
|
3686 | newId = ownerId;
|
3687 | alreadyAssociatedWith = newIdsForInverse.includes(ownerId);
|
3688 | }
|
3689 |
|
3690 | if (!alreadyAssociatedWith) {
|
3691 | newIdsForInverse.push(newId);
|
3692 | }
|
3693 |
|
3694 | inverseCollection.update(model.id, {
|
3695 | [inverseFk]: newIdsForInverse
|
3696 | });
|
3697 | }
|
3698 | }
|
3699 | }
|
3700 |
|
3701 |
|
3702 |
|
3703 | _updateInDb(attrs) {
|
3704 | this.attrs = this._schema.db[this._schema.toInternalCollectionName(this.modelName)].update(this.attrs.id, attrs);
|
3705 | }
|
3706 | |
3707 |
|
3708 |
|
3709 |
|
3710 |
|
3711 |
|
3712 |
|
3713 |
|
3714 |
|
3715 |
|
3716 | _syncTempAssociations(tempAssociate) {
|
3717 | Object.keys(this._tempAssociations).forEach(key => {
|
3718 | if (this._tempAssociations[key] && this._tempAssociations[key].toString() === tempAssociate.toString()) {
|
3719 | this._tempAssociations[key] = tempAssociate;
|
3720 | }
|
3721 | });
|
3722 | }
|
3723 | |
3724 |
|
3725 |
|
3726 |
|
3727 |
|
3728 |
|
3729 |
|
3730 |
|
3731 |
|
3732 |
|
3733 |
|
3734 |
|
3735 | toString() {
|
3736 | let idLabel = this.id ? `(${this.id})` : "";
|
3737 | return `model:${this.modelName}${idLabel}`;
|
3738 | }
|
3739 | |
3740 |
|
3741 |
|
3742 |
|
3743 |
|
3744 |
|
3745 |
|
3746 |
|
3747 |
|
3748 |
|
3749 | equals(model) {
|
3750 | return this.toString() === model.toString();
|
3751 | }
|
3752 |
|
3753 | }
|
3754 |
|
3755 | Model.extend = extend;
|
3756 |
|
3757 | Model.findBelongsToAssociation = function (associationType) {
|
3758 | return this.prototype.belongsToAssociations[associationType];
|
3759 | };
|
3760 |
|
3761 |
|
3762 |
|
3763 |
|
3764 |
|
3765 |
|
3766 |
|
3767 |
|
3768 |
|
3769 |
|
3770 |
|
3771 |
|
3772 |
|
3773 |
|
3774 |
|
3775 |
|
3776 |
|
3777 |
|
3778 |
|
3779 |
|
3780 |
|
3781 |
|
3782 |
|
3783 |
|
3784 |
|
3785 |
|
3786 |
|
3787 |
|
3788 |
|
3789 |
|
3790 |
|
3791 |
|
3792 |
|
3793 |
|
3794 |
|
3795 |
|
3796 |
|
3797 |
|
3798 |
|
3799 |
|
3800 |
|
3801 |
|
3802 |
|
3803 |
|
3804 |
|
3805 |
|
3806 |
|
3807 |
|
3808 |
|
3809 |
|
3810 |
|
3811 |
|
3812 |
|
3813 |
|
3814 |
|
3815 |
|
3816 |
|
3817 |
|
3818 |
|
3819 |
|
3820 |
|
3821 |
|
3822 |
|
3823 |
|
3824 |
|
3825 |
|
3826 |
|
3827 |
|
3828 |
|
3829 |
|
3830 |
|
3831 |
|
3832 |
|
3833 |
|
3834 |
|
3835 |
|
3836 |
|
3837 |
|
3838 |
|
3839 |
|
3840 |
|
3841 |
|
3842 |
|
3843 |
|
3844 |
|
3845 |
|
3846 |
|
3847 |
|
3848 |
|
3849 |
|
3850 |
|
3851 |
|
3852 |
|
3853 | class Serializer {
|
3854 | constructor(registry, type, request = {}) {
|
3855 | this.registry = registry;
|
3856 | this.type = type;
|
3857 | this.request = request;
|
3858 | |
3859 |
|
3860 |
|
3861 |
|
3862 |
|
3863 |
|
3864 |
|
3865 |
|
3866 |
|
3867 |
|
3868 |
|
3869 |
|
3870 |
|
3871 |
|
3872 |
|
3873 |
|
3874 |
|
3875 |
|
3876 |
|
3877 |
|
3878 |
|
3879 |
|
3880 |
|
3881 |
|
3882 |
|
3883 |
|
3884 |
|
3885 |
|
3886 | this.attrs = this.attrs || undefined;
|
3887 |
|
3888 | |
3889 |
|
3890 |
|
3891 |
|
3892 |
|
3893 |
|
3894 |
|
3895 |
|
3896 |
|
3897 |
|
3898 |
|
3899 |
|
3900 |
|
3901 |
|
3902 |
|
3903 |
|
3904 |
|
3905 |
|
3906 |
|
3907 |
|
3908 |
|
3909 |
|
3910 |
|
3911 |
|
3912 |
|
3913 |
|
3914 |
|
3915 |
|
3916 |
|
3917 |
|
3918 |
|
3919 |
|
3920 |
|
3921 |
|
3922 |
|
3923 |
|
3924 |
|
3925 |
|
3926 |
|
3927 |
|
3928 |
|
3929 |
|
3930 |
|
3931 |
|
3932 |
|
3933 |
|
3934 |
|
3935 |
|
3936 |
|
3937 |
|
3938 |
|
3939 |
|
3940 |
|
3941 |
|
3942 |
|
3943 |
|
3944 |
|
3945 |
|
3946 |
|
3947 |
|
3948 |
|
3949 |
|
3950 |
|
3951 |
|
3952 |
|
3953 |
|
3954 |
|
3955 | this.include = this.include || [];
|
3956 |
|
3957 | |
3958 |
|
3959 |
|
3960 |
|
3961 |
|
3962 |
|
3963 |
|
3964 |
|
3965 |
|
3966 |
|
3967 |
|
3968 |
|
3969 |
|
3970 |
|
3971 |
|
3972 |
|
3973 |
|
3974 |
|
3975 |
|
3976 |
|
3977 |
|
3978 |
|
3979 |
|
3980 |
|
3981 |
|
3982 |
|
3983 |
|
3984 |
|
3985 |
|
3986 |
|
3987 |
|
3988 | this.root = this.root || undefined;
|
3989 |
|
3990 | |
3991 |
|
3992 |
|
3993 |
|
3994 |
|
3995 |
|
3996 |
|
3997 |
|
3998 |
|
3999 |
|
4000 |
|
4001 |
|
4002 |
|
4003 |
|
4004 |
|
4005 |
|
4006 |
|
4007 |
|
4008 |
|
4009 |
|
4010 |
|
4011 |
|
4012 |
|
4013 |
|
4014 |
|
4015 |
|
4016 |
|
4017 |
|
4018 |
|
4019 |
|
4020 |
|
4021 |
|
4022 |
|
4023 |
|
4024 |
|
4025 |
|
4026 |
|
4027 |
|
4028 |
|
4029 |
|
4030 | this.embed = this.embed || undefined;
|
4031 |
|
4032 | |
4033 |
|
4034 |
|
4035 |
|
4036 |
|
4037 |
|
4038 |
|
4039 |
|
4040 |
|
4041 |
|
4042 | this.serializeIds = this.serializeIds || undefined;
|
4043 | }
|
4044 | |
4045 |
|
4046 |
|
4047 |
|
4048 |
|
4049 |
|
4050 |
|
4051 |
|
4052 |
|
4053 |
|
4054 |
|
4055 |
|
4056 |
|
4057 |
|
4058 |
|
4059 |
|
4060 |
|
4061 |
|
4062 | serialize(primaryResource
|
4063 |
|
4064 | ) {
|
4065 | return this.buildPayload(primaryResource);
|
4066 | }
|
4067 | |
4068 |
|
4069 |
|
4070 |
|
4071 |
|
4072 |
|
4073 |
|
4074 |
|
4075 |
|
4076 |
|
4077 | normalize(json) {
|
4078 | return json;
|
4079 | }
|
4080 |
|
4081 | buildPayload(primaryResource, toInclude, didSerialize, json) {
|
4082 | if (!primaryResource && isEmpty(toInclude)) {
|
4083 | return json;
|
4084 | } else if (primaryResource) {
|
4085 | let [resourceHash, newIncludes] = this.getHashForPrimaryResource(primaryResource);
|
4086 | let newDidSerialize = this.isCollection(primaryResource) ? primaryResource.models : [primaryResource];
|
4087 | return this.buildPayload(undefined, newIncludes, newDidSerialize, resourceHash);
|
4088 | } else {
|
4089 | let nextIncludedResource = toInclude.shift();
|
4090 | let [resourceHash, newIncludes] = this.getHashForIncludedResource(nextIncludedResource);
|
4091 | let newToInclude = newIncludes.filter(resource => {
|
4092 | return !didSerialize.map(m => m.toString()).includes(resource.toString());
|
4093 | }).concat(toInclude);
|
4094 | let newDidSerialize = (this.isCollection(nextIncludedResource) ? nextIncludedResource.models : [nextIncludedResource]).concat(didSerialize);
|
4095 | let newJson = this.mergePayloads(json, resourceHash);
|
4096 | return this.buildPayload(undefined, newToInclude, newDidSerialize, newJson);
|
4097 | }
|
4098 | }
|
4099 |
|
4100 | getHashForPrimaryResource(resource) {
|
4101 | let [hash, addToIncludes] = this.getHashForResource(resource);
|
4102 | let hashWithRoot;
|
4103 |
|
4104 | if (this.root) {
|
4105 | assert(!(resource instanceof PolymorphicCollection), `The base Serializer class cannot serialize a top-level PolymorphicCollection when root is true, since PolymorphicCollections have no type.`);
|
4106 | let serializer = this.serializerFor(resource.modelName);
|
4107 | let rootKey = serializer.keyForResource(resource);
|
4108 | hashWithRoot = {
|
4109 | [rootKey]: hash
|
4110 | };
|
4111 | } else {
|
4112 | hashWithRoot = hash;
|
4113 | }
|
4114 |
|
4115 | return [hashWithRoot, addToIncludes];
|
4116 | }
|
4117 |
|
4118 | getHashForIncludedResource(resource) {
|
4119 | let hashWithRoot, addToIncludes;
|
4120 |
|
4121 | if (resource instanceof PolymorphicCollection) {
|
4122 | hashWithRoot = {};
|
4123 | addToIncludes = resource.models;
|
4124 | } else {
|
4125 | let serializer = this.serializerFor(resource.modelName);
|
4126 | let [hash, newModels] = serializer.getHashForResource(resource);
|
4127 |
|
4128 | let rootKey = serializer.keyForRelationship(resource.modelName);
|
4129 | hashWithRoot = Array.isArray(hash) ? {
|
4130 | [rootKey]: hash
|
4131 | } : {
|
4132 | [rootKey]: [hash]
|
4133 | };
|
4134 | addToIncludes = newModels;
|
4135 | }
|
4136 |
|
4137 | return [hashWithRoot, addToIncludes];
|
4138 | }
|
4139 |
|
4140 | getHashForResource(resource, removeForeignKeys = false, didSerialize = {}, lookupSerializer = false) {
|
4141 | let hash, serializer;
|
4142 |
|
4143 | if (!lookupSerializer) {
|
4144 | serializer = this;
|
4145 | }
|
4146 |
|
4147 |
|
4148 |
|
4149 | if (lookupSerializer && resource.modelName) {
|
4150 | serializer = this.serializerFor(resource.modelName);
|
4151 | }
|
4152 |
|
4153 | if (this.isModel(resource)) {
|
4154 | hash = serializer._hashForModel(resource, removeForeignKeys, didSerialize);
|
4155 | } else {
|
4156 | hash = resource.models.map(m => {
|
4157 | let modelSerializer = serializer;
|
4158 |
|
4159 | if (!modelSerializer) {
|
4160 |
|
4161 | modelSerializer = this.serializerFor(m.modelName);
|
4162 | }
|
4163 |
|
4164 | return modelSerializer._hashForModel(m, removeForeignKeys, didSerialize);
|
4165 | });
|
4166 | }
|
4167 |
|
4168 | if (this.embed) {
|
4169 | return [hash, []];
|
4170 | } else {
|
4171 | let addToIncludes = uniqBy(compact(flatten(serializer.getKeysForIncluded().map(key => {
|
4172 | if (this.isCollection(resource)) {
|
4173 | return resource.models.map(m => m[key]);
|
4174 | } else {
|
4175 | return resource[key];
|
4176 | }
|
4177 | }))), m => m.toString());
|
4178 | return [hash, addToIncludes];
|
4179 | }
|
4180 | }
|
4181 | |
4182 |
|
4183 |
|
4184 |
|
4185 |
|
4186 |
|
4187 |
|
4188 |
|
4189 |
|
4190 |
|
4191 |
|
4192 |
|
4193 |
|
4194 |
|
4195 |
|
4196 |
|
4197 |
|
4198 |
|
4199 |
|
4200 |
|
4201 |
|
4202 |
|
4203 |
|
4204 |
|
4205 |
|
4206 |
|
4207 | mergePayloads(json, resourceHash) {
|
4208 | let newJson;
|
4209 | let [resourceHashKey] = Object.keys(resourceHash);
|
4210 |
|
4211 | if (json[resourceHashKey]) {
|
4212 | newJson = json;
|
4213 | newJson[resourceHashKey] = json[resourceHashKey].concat(resourceHash[resourceHashKey]);
|
4214 | } else {
|
4215 | newJson = Object.assign(json, resourceHash);
|
4216 | }
|
4217 |
|
4218 | return newJson;
|
4219 | }
|
4220 |
|
4221 | keyForResource(resource) {
|
4222 | let {
|
4223 | modelName
|
4224 | } = resource;
|
4225 | return this.isModel(resource) ? this.keyForModel(modelName) : this.keyForCollection(modelName);
|
4226 | }
|
4227 | |
4228 |
|
4229 |
|
4230 |
|
4231 |
|
4232 |
|
4233 |
|
4234 |
|
4235 |
|
4236 |
|
4237 |
|
4238 |
|
4239 |
|
4240 |
|
4241 |
|
4242 |
|
4243 |
|
4244 |
|
4245 |
|
4246 |
|
4247 |
|
4248 |
|
4249 |
|
4250 |
|
4251 |
|
4252 |
|
4253 |
|
4254 |
|
4255 |
|
4256 |
|
4257 |
|
4258 |
|
4259 |
|
4260 |
|
4261 |
|
4262 | keyForModel(modelName) {
|
4263 | return camelize(modelName);
|
4264 | }
|
4265 | |
4266 |
|
4267 |
|
4268 |
|
4269 |
|
4270 |
|
4271 |
|
4272 |
|
4273 |
|
4274 |
|
4275 |
|
4276 |
|
4277 |
|
4278 |
|
4279 |
|
4280 |
|
4281 |
|
4282 |
|
4283 |
|
4284 |
|
4285 |
|
4286 |
|
4287 |
|
4288 |
|
4289 |
|
4290 |
|
4291 |
|
4292 |
|
4293 |
|
4294 |
|
4295 |
|
4296 |
|
4297 |
|
4298 |
|
4299 |
|
4300 |
|
4301 |
|
4302 |
|
4303 |
|
4304 |
|
4305 |
|
4306 |
|
4307 | keyForCollection(modelName) {
|
4308 | return this._container.inflector.pluralize(this.keyForModel(modelName));
|
4309 | }
|
4310 |
|
4311 | _hashForModel(model, removeForeignKeys, didSerialize = {}) {
|
4312 | let attrs = this._attrsForModel(model);
|
4313 |
|
4314 | if (removeForeignKeys) {
|
4315 | model.fks.forEach(fk => {
|
4316 | delete attrs[fk];
|
4317 | });
|
4318 | }
|
4319 |
|
4320 | if (this.embed) {
|
4321 | let newDidSerialize = Object.assign({}, didSerialize);
|
4322 | newDidSerialize[model.modelName] = newDidSerialize[model.modelName] || {};
|
4323 | newDidSerialize[model.modelName][model.id] = true;
|
4324 | this.getKeysForIncluded().forEach(key => {
|
4325 | let associatedResource = model[key];
|
4326 |
|
4327 | if (associatedResource && !get(newDidSerialize, `${associatedResource.modelName}.${associatedResource.id}`)) {
|
4328 | let [associatedResourceHash] = this.getHashForResource(associatedResource, true, newDidSerialize, true);
|
4329 | let formattedKey = this.keyForEmbeddedRelationship(key);
|
4330 | attrs[formattedKey] = associatedResourceHash;
|
4331 |
|
4332 | if (this.isModel(associatedResource)) {
|
4333 | let fk = `${camelize(key)}Id`;
|
4334 | delete attrs[fk];
|
4335 | }
|
4336 | }
|
4337 | });
|
4338 | return attrs;
|
4339 | } else {
|
4340 | return this._maybeAddAssociationIds(model, attrs);
|
4341 | }
|
4342 | }
|
4343 | /**
|
4344 | @method _attrsForModel
|
4345 | @param model
|
4346 | @private
|
4347 | @hide
|
4348 | */
|
4349 |
|
4350 |
|
4351 | _attrsForModel(model) {
|
4352 | let attrs = {};
|
4353 |
|
4354 | if (this.attrs) {
|
4355 | attrs = this.attrs.reduce((memo, attr) => {
|
4356 | memo[attr] = model[attr];
|
4357 | return memo;
|
4358 | }, {});
|
4359 | } else {
|
4360 | attrs = Object.assign(attrs, model.attrs);
|
4361 | } // Remove fks
|
4362 |
|
4363 |
|
4364 | model.fks.forEach(key => delete attrs[key]);
|
4365 | return this._formatAttributeKeys(attrs);
|
4366 | }
|
4367 | /**
|
4368 | @method _maybeAddAssociationIds
|
4369 | @param model
|
4370 | @param attrs
|
4371 | @private
|
4372 | @hide
|
4373 | */
|
4374 |
|
4375 |
|
4376 | _maybeAddAssociationIds(model, attrs) {
|
4377 | let newHash = Object.assign({}, attrs);
|
4378 |
|
4379 | if (this.serializeIds === "always") {
|
4380 | model.associationKeys.forEach(key => {
|
4381 | let resource = model[key];
|
4382 | let association = model.associationFor(key);
|
4383 |
|
4384 | if (this.isCollection(resource)) {
|
4385 | let formattedKey = this.keyForRelationshipIds(key);
|
4386 | newHash[formattedKey] = model[`${this._container.inflector.singularize(key)}Ids`];
|
4387 | } else if (this.isModel(resource) && association.isPolymorphic) {
|
4388 | let formattedTypeKey = this.keyForPolymorphicForeignKeyType(key);
|
4389 | let formattedIdKey = this.keyForPolymorphicForeignKeyId(key);
|
4390 | newHash[formattedTypeKey] = model[`${key}Id`].type;
|
4391 | newHash[formattedIdKey] = model[`${key}Id`].id;
|
4392 | } else if (resource) {
|
4393 | let formattedKey = this.keyForForeignKey(key);
|
4394 | newHash[formattedKey] = model[`${key}Id`];
|
4395 | }
|
4396 | });
|
4397 | } else if (this.serializeIds === "included") {
|
4398 | this.getKeysForIncluded().forEach(key => {
|
4399 | let resource = model[key];
|
4400 | let association = model.associationFor(key);
|
4401 |
|
4402 | if (this.isCollection(resource)) {
|
4403 | let formattedKey = this.keyForRelationshipIds(key);
|
4404 | newHash[formattedKey] = model[`${this._container.inflector.singularize(key)}Ids`];
|
4405 | } else if (this.isModel(resource) && association.isPolymorphic) {
|
4406 | let formattedTypeKey = this.keyForPolymorphicForeignKeyType(key);
|
4407 | let formattedIdKey = this.keyForPolymorphicForeignKeyId(key);
|
4408 | newHash[formattedTypeKey] = model[`${key}Id`].type;
|
4409 | newHash[formattedIdKey] = model[`${key}Id`].id;
|
4410 | } else if (this.isModel(resource)) {
|
4411 | let formattedKey = this.keyForForeignKey(key);
|
4412 | newHash[formattedKey] = model[`${key}Id`];
|
4413 | }
|
4414 | });
|
4415 | }
|
4416 |
|
4417 | return newHash;
|
4418 | }
|
4419 | /**
|
4420 | Used to customize how a model's attribute is formatted in your JSON payload.
|
4421 | By default, model attributes are camelCase:
|
4422 | ```
|
4423 | GET /authors/1
|
4424 | {
|
4425 | author: {
|
4426 | firstName: 'Link',
|
4427 | lastName: 'The WoodElf'
|
4428 | }
|
4429 | }
|
4430 | ```
|
4431 | If your API expects snake case, you could write the following:
|
4432 | ```js
|
4433 |
|
4434 | export default Serializer.extend({
|
4435 | keyForAttribute(attr) {
|
4436 | return underscore(attr);
|
4437 | }
|
4438 | });
|
4439 | ```
|
4440 | Now the response would look like:
|
4441 | ```
|
4442 | {
|
4443 | author: {
|
4444 | first_name: 'Link',
|
4445 | last_name: 'The WoodElf'
|
4446 | }
|
4447 | }
|
4448 | ```
|
4449 | @method keyForAttribute
|
4450 | @param attr
|
4451 | @public
|
4452 | */
|
4453 |
|
4454 |
|
4455 | keyForAttribute(attr) {
|
4456 | return attr;
|
4457 | }
|
4458 | /**
|
4459 | Use this hook to format the key for collections related to this model. *modelName* is the named parameter for the relationship.
|
4460 | For example, if you're serializing an `author` that
|
4461 | sideloads many `blogPosts`, the default response will look like:
|
4462 | ```
|
4463 | {
|
4464 | author: {...},
|
4465 | blogPosts: [...]
|
4466 | }
|
4467 | ```
|
4468 | Overwrite `keyForRelationship` to format this key:
|
4469 | ```js
|
4470 |
|
4471 | export default Serializer.extend({
|
4472 | keyForRelationship(modelName) {
|
4473 | return underscore(modelName);
|
4474 | }
|
4475 | });
|
4476 | ```
|
4477 | Now the response will look like this:
|
4478 | ```
|
4479 | {
|
4480 | author: {...},
|
4481 | blog_posts: [...]
|
4482 | }
|
4483 | ```
|
4484 | @method keyForRelationship
|
4485 | @param modelName
|
4486 | @public
|
4487 | */
|
4488 |
|
4489 |
|
4490 | keyForRelationship(modelName) {
|
4491 | return camelize(this._container.inflector.pluralize(modelName));
|
4492 | }
|
4493 | /**
|
4494 | Like `keyForRelationship`, but for embedded relationships.
|
4495 | @method keyForEmbeddedRelationship
|
4496 | @param attributeName
|
4497 | @public
|
4498 | */
|
4499 |
|
4500 |
|
4501 | keyForEmbeddedRelationship(attributeName) {
|
4502 | return camelize(attributeName);
|
4503 | }
|
4504 | /**
|
4505 | Use this hook to format the key for the IDS of a `hasMany` relationship
|
4506 | in this model's JSON representation.
|
4507 | For example, if you're serializing an `author` that
|
4508 | sideloads many `blogPosts`, by default your `author` JSON would include a `blogPostIds` key:
|
4509 | ```
|
4510 | {
|
4511 | author: {
|
4512 | id: 1,
|
4513 | blogPostIds: [1, 2, 3]
|
4514 | },
|
4515 | blogPosts: [...]
|
4516 | }
|
4517 | ```
|
4518 | Overwrite `keyForRelationshipIds` to format this key:
|
4519 | ```js
|
4520 |
|
4521 | export default Serializer.extend({
|
4522 | keyForRelationshipIds(relationship) {
|
4523 | return underscore(relationship) + '_ids';
|
4524 | }
|
4525 | });
|
4526 | ```
|
4527 | Now the response will look like:
|
4528 | ```
|
4529 | {
|
4530 | author: {
|
4531 | id: 1,
|
4532 | blog_post_ids: [1, 2, 3]
|
4533 | },
|
4534 | blogPosts: [...]
|
4535 | }
|
4536 | ```
|
4537 | @method keyForRelationshipIds
|
4538 | @param modelName
|
4539 | @public
|
4540 | */
|
4541 |
|
4542 |
|
4543 | keyForRelationshipIds(relationshipName) {
|
4544 | return `${this._container.inflector.singularize(camelize(relationshipName))}Ids`;
|
4545 | }
|
4546 | /**
|
4547 | Like `keyForRelationshipIds`, but for `belongsTo` relationships.
|
4548 | For example, if you're serializing a `blogPost` that sideloads one `author`,
|
4549 | your `blogPost` JSON would include a `authorId` key:
|
4550 | ```
|
4551 | {
|
4552 | blogPost: {
|
4553 | id: 1,
|
4554 | authorId: 1
|
4555 | },
|
4556 | author: ...
|
4557 | }
|
4558 | ```
|
4559 | Overwrite `keyForForeignKey` to format this key:
|
4560 | ```js
|
4561 |
|
4562 | export default Serializer.extend({
|
4563 | keyForForeignKey(relationshipName) {
|
4564 | return underscore(relationshipName) + '_id';
|
4565 | }
|
4566 | });
|
4567 | ```
|
4568 | Now the response will look like:
|
4569 | ```js
|
4570 | {
|
4571 | blogPost: {
|
4572 | id: 1,
|
4573 | author_id: 1
|
4574 | },
|
4575 | author: ...
|
4576 | }
|
4577 | ```
|
4578 | @method keyForForeignKey
|
4579 | @param relationshipName
|
4580 | @public
|
4581 | */
|
4582 |
|
4583 |
|
4584 | keyForForeignKey(relationshipName) {
|
4585 | return `${camelize(relationshipName)}Id`;
|
4586 | }
|
4587 | /**
|
4588 | Polymorphic relationships are represented with type-id pairs.
|
4589 | Given the following model
|
4590 | ```js
|
4591 | Model.extend({
|
4592 | commentable: belongsTo({ polymorphic: true })
|
4593 | });
|
4594 | ```
|
4595 | the default Serializer would produce
|
4596 | ```js
|
4597 | {
|
4598 | comment: {
|
4599 | id: 1,
|
4600 | commentableType: 'post',
|
4601 | commentableId: '1'
|
4602 | }
|
4603 | }
|
4604 | ```
|
4605 | This hook controls how the `id` field (`commentableId` in the above example)
|
4606 | is serialized. By default it camelizes the relationship and adds `Id` as a suffix.
|
4607 | @method keyForPolymorphicForeignKeyId
|
4608 | @param {String} relationshipName
|
4609 | @return {String}
|
4610 | @public
|
4611 | */
|
4612 |
|
4613 |
|
4614 | keyForPolymorphicForeignKeyId(relationshipName) {
|
4615 | return `${camelize(relationshipName)}Id`;
|
4616 | }
|
4617 | /**
|
4618 | Polymorphic relationships are represented with type-id pairs.
|
4619 | Given the following model
|
4620 | ```js
|
4621 | Model.extend({
|
4622 | commentable: belongsTo({ polymorphic: true })
|
4623 | });
|
4624 | ```
|
4625 | the default Serializer would produce
|
4626 | ```js
|
4627 | {
|
4628 | comment: {
|
4629 | id: 1,
|
4630 | commentableType: 'post',
|
4631 | commentableId: '1'
|
4632 | }
|
4633 | }
|
4634 | ```
|
4635 | This hook controls how the `type` field (`commentableType` in the above example)
|
4636 | is serialized. By default it camelizes the relationship and adds `Type` as a suffix.
|
4637 | @method keyForPolymorphicForeignKeyType
|
4638 | @param {String} relationshipName
|
4639 | @return {String}
|
4640 | @public
|
4641 | */
|
4642 |
|
4643 |
|
4644 | keyForPolymorphicForeignKeyType(relationshipName) {
|
4645 | return `${camelize(relationshipName)}Type`;
|
4646 | }
|
4647 | /**
|
4648 | @method isModel
|
4649 | @param object
|
4650 | @return {Boolean}
|
4651 | @public
|
4652 | @hide
|
4653 | */
|
4654 |
|
4655 |
|
4656 | isModel(object) {
|
4657 | return object instanceof Model;
|
4658 | }
|
4659 | /**
|
4660 | @method isCollection
|
4661 | @param object
|
4662 | @return {Boolean}
|
4663 | @public
|
4664 | @hide
|
4665 | */
|
4666 |
|
4667 |
|
4668 | isCollection(object) {
|
4669 | return object instanceof Collection || object instanceof PolymorphicCollection;
|
4670 | }
|
4671 | /**
|
4672 | @method isModelOrCollection
|
4673 | @param object
|
4674 | @return {Boolean}
|
4675 | @public
|
4676 | @hide
|
4677 | */
|
4678 |
|
4679 |
|
4680 | isModelOrCollection(object) {
|
4681 | return this.isModel(object) || this.isCollection(object);
|
4682 | }
|
4683 | /**
|
4684 | @method serializerFor
|
4685 | @param type
|
4686 | @public
|
4687 | @hide
|
4688 | */
|
4689 |
|
4690 |
|
4691 | serializerFor(type) {
|
4692 | return this.registry.serializerFor(type);
|
4693 | }
|
4694 |
|
4695 | getKeysForIncluded() {
|
4696 | return isFunction(this.include) ? this.include(this.request) : this.include;
|
4697 | }
|
4698 | /**
|
4699 | Foo bar.
|
4700 | @property schema
|
4701 | @public
|
4702 | @hide
|
4703 | */
|
4704 |
|
4705 |
|
4706 | get schema() {
|
4707 | return this.registry.schema;
|
4708 | }
|
4709 | /**
|
4710 | @method _formatAttributeKeys
|
4711 | @param attrs
|
4712 | @private
|
4713 | @hide
|
4714 | */
|
4715 |
|
4716 |
|
4717 | _formatAttributeKeys(attrs) {
|
4718 | let formattedAttrs = {};
|
4719 |
|
4720 | for (let key in attrs) {
|
4721 | let formattedKey = this.keyForAttribute(key);
|
4722 | formattedAttrs[formattedKey] = attrs[key];
|
4723 | }
|
4724 |
|
4725 | return formattedAttrs;
|
4726 | }
|
4727 |
|
4728 | getCoalescedIds()
|
4729 | /* request */
|
4730 | {}
|
4731 |
|
4732 | } // Defaults
|
4733 |
|
4734 |
|
4735 | Serializer.prototype.include = [];
|
4736 | Serializer.prototype.root = true;
|
4737 | Serializer.prototype.embed = false;
|
4738 | Serializer.prototype.serializeIds = "included"; // can be 'included', 'always', or 'never'
|
4739 |
|
4740 | Serializer.extend = extend;
|
4741 |
|
4742 | /**
|
4743 | The JSONAPISerializer. Subclass of Serializer.
|
4744 |
|
4745 | @class JSONAPISerializer
|
4746 | @constructor
|
4747 | @public
|
4748 | */
|
4749 |
|
4750 | class JSONAPISerializer extends Serializer {
|
4751 | constructor() {
|
4752 | super(...arguments);
|
4753 | /**
|
4754 | By default, JSON:API's linkage data is only added for relationships that are being included in the current request.
|
4755 | That means given an `author` model with a `posts` relationship, a GET request to /authors/1 would return a JSON:API document with an empty `relationships` hash:
|
4756 | ```js
|
4757 | {
|
4758 | data: {
|
4759 | type: 'authors',
|
4760 | id: '1',
|
4761 | attributes: { ... }
|
4762 | }
|
4763 | }
|
4764 | ```
|
4765 | but a request to GET /authors/1?include=posts would have linkage data added (in addition to the included resources):
|
4766 | ```js
|
4767 | {
|
4768 | data: {
|
4769 | type: 'authors',
|
4770 | id: '1',
|
4771 | attributes: { ... },
|
4772 | relationships: {
|
4773 | data: [
|
4774 | { type: 'posts', id: '1' },
|
4775 | { type: 'posts', id: '2' },
|
4776 | { type: 'posts', id: '3' }
|
4777 | ]
|
4778 | }
|
4779 | },
|
4780 | included: [ ... ]
|
4781 | }
|
4782 | ```
|
4783 | To add the linkage data for all relationships, you could set `alwaysIncludeLinkageData` to `true`:
|
4784 | ```js
|
4785 | JSONAPISerializer.extend({
|
4786 | alwaysIncludeLinkageData: true
|
4787 | });
|
4788 | ```
|
4789 | Then, a GET to /authors/1 would respond with
|
4790 | ```js
|
4791 | {
|
4792 | data: {
|
4793 | type: 'authors',
|
4794 | id: '1',
|
4795 | attributes: { ... },
|
4796 | relationships: {
|
4797 | posts: {
|
4798 | data: [
|
4799 | { type: 'posts', id: '1' },
|
4800 | { type: 'posts', id: '2' },
|
4801 | { type: 'posts', id: '3' }
|
4802 | ]
|
4803 | }
|
4804 | }
|
4805 | }
|
4806 | }
|
4807 | ```
|
4808 | even though the related `posts` are not included in the same document.
|
4809 | You can also use the `links` method (on the Serializer base class) to add relationship links (which will always be added regardless of the relationship is being included document), or you could use `shouldIncludeLinkageData` for more granular control.
|
4810 | For more background on the behavior of this API, see [this blog post](http://www.ember-cli-mirage.com/blog/changing-mirages-default-linkage-data-behavior-1475).
|
4811 | @property alwaysIncludeLinkageData
|
4812 | @type {Boolean}
|
4813 | @public
|
4814 | */
|
4815 |
|
4816 | this.alwaysIncludeLinkageData = this.alwaysIncludeLinkageData || undefined; // this is just here so I can add the doc comment. Better way?
|
4817 | } // Don't think this is used?
|
4818 |
|
4819 |
|
4820 | keyForModel(modelName) {
|
4821 | return dasherize(modelName);
|
4822 | } // Don't think this is used?
|
4823 |
|
4824 |
|
4825 | keyForCollection(modelName) {
|
4826 | return dasherize(modelName);
|
4827 | }
|
4828 | /**
|
4829 | Used to customize the key for an attribute. By default, compound attribute names are dasherized.
|
4830 | For example, the JSON:API document for a `post` model with a `commentCount` attribute would be:
|
4831 | ```js
|
4832 | {
|
4833 | data: {
|
4834 | id: 1,
|
4835 | type: 'posts',
|
4836 | attributes: {
|
4837 | 'comment-count': 28
|
4838 | }
|
4839 | }
|
4840 | }
|
4841 | ```
|
4842 | @method keyForAttribute
|
4843 | @param {String} attr
|
4844 | @return {String}
|
4845 | @public
|
4846 | */
|
4847 |
|
4848 |
|
4849 | keyForAttribute(attr) {
|
4850 | return dasherize(attr);
|
4851 | }
|
4852 | /**
|
4853 | Used to customize the key for a relationships. By default, compound relationship names are dasherized.
|
4854 | For example, the JSON:API document for an `author` model with a `blogPosts` relationship would be:
|
4855 | ```js
|
4856 | {
|
4857 | data: {
|
4858 | id: 1,
|
4859 | type: 'author',
|
4860 | attributes: {
|
4861 | ...
|
4862 | },
|
4863 | relationships: {
|
4864 | 'blog-posts': {
|
4865 | ...
|
4866 | }
|
4867 | }
|
4868 | }
|
4869 | }
|
4870 | ```
|
4871 | @method keyForRelationship
|
4872 | @param {String} key
|
4873 | @return {String}
|
4874 | @public
|
4875 | */
|
4876 |
|
4877 |
|
4878 | keyForRelationship(key) {
|
4879 | return dasherize(key);
|
4880 | }
|
4881 | /**
|
4882 | Use this hook to add top-level `links` data to JSON:API resource objects. The argument is the model being serialized.
|
4883 | ```js
|
4884 |
|
4885 | import { JSONAPISerializer } from 'miragejs';
|
4886 | export default JSONAPISerializer.extend({
|
4887 | links(author) {
|
4888 | return {
|
4889 | 'posts': {
|
4890 | related: `/api/authors/${author.id}/posts`
|
4891 | }
|
4892 | };
|
4893 | }
|
4894 | });
|
4895 | ```
|
4896 | @method links
|
4897 | @param model
|
4898 | */
|
4899 |
|
4900 |
|
4901 | links() {}
|
4902 |
|
4903 | getHashForPrimaryResource(resource) {
|
4904 | this._createRequestedIncludesGraph(resource);
|
4905 |
|
4906 | let resourceHash = this.getHashForResource(resource);
|
4907 | let hashWithRoot = {
|
4908 | data: resourceHash
|
4909 | };
|
4910 | let addToIncludes = this.getAddToIncludesForResource(resource);
|
4911 | return [hashWithRoot, addToIncludes];
|
4912 | }
|
4913 |
|
4914 | getHashForIncludedResource(resource) {
|
4915 | let serializer = this.serializerFor(resource.modelName);
|
4916 | let hash = serializer.getHashForResource(resource);
|
4917 | let hashWithRoot = {
|
4918 | included: this.isModel(resource) ? [hash] : hash
|
4919 | };
|
4920 | let addToIncludes = [];
|
4921 |
|
4922 | if (!this.hasQueryParamIncludes()) {
|
4923 | addToIncludes = this.getAddToIncludesForResource(resource);
|
4924 | }
|
4925 |
|
4926 | return [hashWithRoot, addToIncludes];
|
4927 | }
|
4928 |
|
4929 | getHashForResource(resource) {
|
4930 | let hash;
|
4931 |
|
4932 | if (this.isModel(resource)) {
|
4933 | hash = this.getResourceObjectForModel(resource);
|
4934 | } else {
|
4935 | hash = resource.models.map(m => this.getResourceObjectForModel(m));
|
4936 | }
|
4937 |
|
4938 | return hash;
|
4939 | }
|
4940 | /*
|
4941 | Returns a flat unique list of resources that need to be added to includes
|
4942 | */
|
4943 |
|
4944 |
|
4945 | getAddToIncludesForResource(resource) {
|
4946 | let relationshipPaths;
|
4947 |
|
4948 | if (this.hasQueryParamIncludes()) {
|
4949 | relationshipPaths = this.request.queryParams.include.split(",");
|
4950 | } else {
|
4951 | let serializer = this.serializerFor(resource.modelName);
|
4952 | relationshipPaths = serializer.getKeysForIncluded();
|
4953 | }
|
4954 |
|
4955 | return this.getAddToIncludesForResourceAndPaths(resource, relationshipPaths);
|
4956 | }
|
4957 |
|
4958 | getAddToIncludesForResourceAndPaths(resource, relationshipPaths) {
|
4959 | let includes = [];
|
4960 | relationshipPaths.forEach(path => {
|
4961 | let relationshipNames = path.split(".");
|
4962 | let newIncludes = this.getIncludesForResourceAndPath(resource, ...relationshipNames);
|
4963 | includes.push(newIncludes);
|
4964 | });
|
4965 | return uniqBy(compact(flatten(includes)), m => m.toString());
|
4966 | }
|
4967 |
|
4968 | getIncludesForResourceAndPath(resource, ...names) {
|
4969 | let nameForCurrentResource = camelize(names.shift());
|
4970 | let includes = [];
|
4971 | let modelsToAdd = [];
|
4972 |
|
4973 | if (this.isModel(resource)) {
|
4974 | let relationship = resource[nameForCurrentResource];
|
4975 |
|
4976 | if (this.isModel(relationship)) {
|
4977 | modelsToAdd = [relationship];
|
4978 | } else if (this.isCollection(relationship)) {
|
4979 | modelsToAdd = relationship.models;
|
4980 | }
|
4981 | } else {
|
4982 | resource.models.forEach(model => {
|
4983 | let relationship = model[nameForCurrentResource];
|
4984 |
|
4985 | if (this.isModel(relationship)) {
|
4986 | modelsToAdd.push(relationship);
|
4987 | } else if (this.isCollection(relationship)) {
|
4988 | modelsToAdd = modelsToAdd.concat(relationship.models);
|
4989 | }
|
4990 | });
|
4991 | }
|
4992 |
|
4993 | includes = includes.concat(modelsToAdd);
|
4994 |
|
4995 | if (names.length) {
|
4996 | modelsToAdd.forEach(model => {
|
4997 | includes = includes.concat(this.getIncludesForResourceAndPath(model, ...names));
|
4998 | });
|
4999 | }
|
5000 |
|
5001 | return includes;
|
5002 | }
|
5003 |
|
5004 | getResourceObjectForModel(model) {
|
5005 | let attrs = this._attrsForModel(model, true);
|
5006 |
|
5007 | delete attrs.id;
|
5008 | let hash = {
|
5009 | type: this.typeKeyForModel(model),
|
5010 | id: model.id,
|
5011 | attributes: attrs
|
5012 | };
|
5013 | return this._maybeAddRelationshipsToResourceObjectForModel(hash, model);
|
5014 | }
|
5015 |
|
5016 | _maybeAddRelationshipsToResourceObjectForModel(hash, model) {
|
5017 | const relationships = {};
|
5018 | model.associationKeys.forEach(key => {
|
5019 | let relationship = model[key];
|
5020 | let relationshipKey = this.keyForRelationship(key);
|
5021 | let relationshipHash = {};
|
5022 |
|
5023 | if (this.hasLinksForRelationship(model, key)) {
|
5024 | let serializer = this.serializerFor(model.modelName);
|
5025 | let links = serializer.links(model);
|
5026 | relationshipHash.links = links[key];
|
5027 | }
|
5028 |
|
5029 | if (this.alwaysIncludeLinkageData || this.shouldIncludeLinkageData(key, model) || this._relationshipIsIncludedForModel(key, model)) {
|
5030 | let data = null;
|
5031 |
|
5032 | if (this.isModel(relationship)) {
|
5033 | data = {
|
5034 | type: this.typeKeyForModel(relationship),
|
5035 | id: relationship.id
|
5036 | };
|
5037 | } else if (this.isCollection(relationship)) {
|
5038 | data = relationship.models.map(model => {
|
5039 | return {
|
5040 | type: this.typeKeyForModel(model),
|
5041 | id: model.id
|
5042 | };
|
5043 | });
|
5044 | }
|
5045 |
|
5046 | relationshipHash.data = data;
|
5047 | }
|
5048 |
|
5049 | if (!isEmpty(relationshipHash)) {
|
5050 | relationships[relationshipKey] = relationshipHash;
|
5051 | }
|
5052 | });
|
5053 |
|
5054 | if (!isEmpty(relationships)) {
|
5055 | hash.relationships = relationships;
|
5056 | }
|
5057 |
|
5058 | return hash;
|
5059 | }
|
5060 |
|
5061 | hasLinksForRelationship(model, relationshipKey) {
|
5062 | let serializer = this.serializerFor(model.modelName);
|
5063 | let links = serializer.links && serializer.links(model);
|
5064 | return links && links[relationshipKey] != null;
|
5065 | }
|
5066 | /*
|
5067 | This code (and a lot of this serializer) need to be re-worked according to
|
5068 | the graph logic...
|
5069 | */
|
5070 |
|
5071 |
|
5072 | _relationshipIsIncludedForModel(relationshipKey, model) {
|
5073 | if (this.hasQueryParamIncludes()) {
|
5074 | let graph = this.request._includesGraph;
|
5075 |
|
5076 | let graphKey = this._graphKeyForModel(model); // Find the resource in the graph
|
5077 |
|
5078 |
|
5079 | let graphResource; // Check primary data
|
5080 |
|
5081 | if (graph.data[graphKey]) {
|
5082 | graphResource = graph.data[graphKey]; // Check includes
|
5083 | } else if (graph.included[this._container.inflector.pluralize(model.modelName)]) {
|
5084 | graphResource = graph.included[this._container.inflector.pluralize(model.modelName)][graphKey];
|
5085 | } // If the model's in the graph, check if relationshipKey should be included
|
5086 |
|
5087 |
|
5088 | return graphResource && graphResource.relationships && Object.prototype.hasOwnProperty.call(graphResource.relationships, dasherize(relationshipKey));
|
5089 | } else {
|
5090 | let relationshipPaths = this.getKeysForIncluded();
|
5091 | return relationshipPaths.includes(relationshipKey);
|
5092 | }
|
5093 | }
|
5094 | /*
|
5095 | This is needed for _relationshipIsIncludedForModel - see the note there for
|
5096 | more background.
|
5097 | If/when we can refactor this serializer, the logic in this method would
|
5098 | probably be the basis for the new overall json/graph creation.
|
5099 | */
|
5100 |
|
5101 |
|
5102 | _createRequestedIncludesGraph(primaryResource, secondaryResource = null) {
|
5103 | let graph = {
|
5104 | data: {}
|
5105 | };
|
5106 |
|
5107 | if (this.isModel(primaryResource)) {
|
5108 | let primaryResourceKey = this._graphKeyForModel(primaryResource);
|
5109 |
|
5110 | graph.data[primaryResourceKey] = {};
|
5111 |
|
5112 | this._addPrimaryModelToRequestedIncludesGraph(graph, primaryResource);
|
5113 | } else if (this.isCollection(primaryResource)) {
|
5114 | primaryResource.models.forEach(model => {
|
5115 | let primaryResourceKey = this._graphKeyForModel(model);
|
5116 |
|
5117 | graph.data[primaryResourceKey] = {};
|
5118 |
|
5119 | this._addPrimaryModelToRequestedIncludesGraph(graph, model);
|
5120 | });
|
5121 | } // Hack :/ Need to think of a better palce to put this if
|
5122 | // refactoring json:api serializer.
|
5123 |
|
5124 |
|
5125 | this.request._includesGraph = graph;
|
5126 | }
|
5127 |
|
5128 | _addPrimaryModelToRequestedIncludesGraph(graph, model) {
|
5129 | if (this.hasQueryParamIncludes()) {
|
5130 | let graphKey = this._graphKeyForModel(model);
|
5131 |
|
5132 | let queryParamIncludes = this.getQueryParamIncludes();
|
5133 | queryParamIncludes.split(",").forEach(includesPath => {
|
5134 | // includesPath is post.comments, for example
|
5135 | graph.data[graphKey].relationships = graph.data[graphKey].relationships || {};
|
5136 | let relationshipKeys = includesPath.split(".").map(dasherize);
|
5137 | let relationshipKey = relationshipKeys[0];
|
5138 | let graphRelationshipKey = relationshipKey;
|
5139 | let normalizedRelationshipKey = camelize(relationshipKey);
|
5140 | let hasAssociation = model.associationKeys.has(normalizedRelationshipKey);
|
5141 | assert(hasAssociation, `You tried to include "${relationshipKey}" with ${model} but no association named "${normalizedRelationshipKey}" is defined on the model.`);
|
5142 | let relationship = model[normalizedRelationshipKey];
|
5143 | let relationshipData;
|
5144 |
|
5145 | if (this.isModel(relationship)) {
|
5146 | relationshipData = this._graphKeyForModel(relationship);
|
5147 | } else if (this.isCollection(relationship)) {
|
5148 | relationshipData = relationship.models.map(this._graphKeyForModel);
|
5149 | } else {
|
5150 | relationshipData = null;
|
5151 | }
|
5152 |
|
5153 | graph.data[graphKey].relationships[graphRelationshipKey] = relationshipData;
|
5154 |
|
5155 | if (relationship) {
|
5156 | this._addResourceToRequestedIncludesGraph(graph, relationship, relationshipKeys.slice(1));
|
5157 | }
|
5158 | });
|
5159 | }
|
5160 | }
|
5161 |
|
5162 | _addResourceToRequestedIncludesGraph(graph, resource, relationshipNames) {
|
5163 | graph.included = graph.included || {};
|
5164 | let models = this.isCollection(resource) ? resource.models : [resource];
|
5165 | models.forEach(model => {
|
5166 | let collectionName = this._container.inflector.pluralize(model.modelName);
|
5167 |
|
5168 | graph.included[collectionName] = graph.included[collectionName] || {};
|
5169 |
|
5170 | this._addModelToRequestedIncludesGraph(graph, model, relationshipNames);
|
5171 | });
|
5172 | }
|
5173 |
|
5174 | _addModelToRequestedIncludesGraph(graph, model, relationshipNames) {
|
5175 | let collectionName = this._container.inflector.pluralize(model.modelName);
|
5176 |
|
5177 | let resourceKey = this._graphKeyForModel(model);
|
5178 |
|
5179 | graph.included[collectionName][resourceKey] = graph.included[collectionName][resourceKey] || {};
|
5180 |
|
5181 | if (relationshipNames.length) {
|
5182 | this._addResourceRelationshipsToRequestedIncludesGraph(graph, collectionName, resourceKey, model, relationshipNames);
|
5183 | }
|
5184 | }
|
5185 | /*
|
5186 | Lot of the same logic here from _addPrimaryModelToRequestedIncludesGraph, could refactor & share
|
5187 | */
|
5188 |
|
5189 |
|
5190 | _addResourceRelationshipsToRequestedIncludesGraph(graph, collectionName, resourceKey, model, relationshipNames) {
|
5191 | graph.included[collectionName][resourceKey].relationships = graph.included[collectionName][resourceKey].relationships || {};
|
5192 | let relationshipName = relationshipNames[0];
|
5193 | let relationship = model[camelize(relationshipName)];
|
5194 | let relationshipData;
|
5195 |
|
5196 | if (this.isModel(relationship)) {
|
5197 | relationshipData = this._graphKeyForModel(relationship);
|
5198 | } else if (this.isCollection(relationship)) {
|
5199 | relationshipData = relationship.models.map(this._graphKeyForModel);
|
5200 | }
|
5201 |
|
5202 | graph.included[collectionName][resourceKey].relationships[relationshipName] = relationshipData;
|
5203 |
|
5204 | if (relationship) {
|
5205 | this._addResourceToRequestedIncludesGraph(graph, relationship, relationshipNames.slice(1));
|
5206 | }
|
5207 | }
|
5208 |
|
5209 | _graphKeyForModel(model) {
|
5210 | return `${model.modelName}:${model.id}`;
|
5211 | }
|
5212 |
|
5213 | getQueryParamIncludes() {
|
5214 | return get(this, "request.queryParams.include");
|
5215 | }
|
5216 |
|
5217 | hasQueryParamIncludes() {
|
5218 | return !!this.getQueryParamIncludes();
|
5219 | }
|
5220 | /**
|
5221 | Used to customize the `type` field of the document. By default, pluralizes and dasherizes the model's `modelName`.
|
5222 | For example, the JSON:API document for a `blogPost` model would be:
|
5223 | ```js
|
5224 | {
|
5225 | data: {
|
5226 | id: 1,
|
5227 | type: 'blog-posts'
|
5228 | }
|
5229 | }
|
5230 | ```
|
5231 | @method typeKeyForModel
|
5232 | @param {Model} model
|
5233 | @return {String}
|
5234 | @public
|
5235 | */
|
5236 |
|
5237 |
|
5238 | typeKeyForModel(model) {
|
5239 | return dasherize(this._container.inflector.pluralize(model.modelName));
|
5240 | }
|
5241 |
|
5242 | getCoalescedIds(request) {
|
5243 | let ids = request.queryParams && request.queryParams["filter[id]"];
|
5244 |
|
5245 | if (typeof ids === "string") {
|
5246 | return ids.split(",");
|
5247 | }
|
5248 |
|
5249 | return ids;
|
5250 | }
|
5251 | /**
|
5252 | Allows for per-relationship inclusion of linkage data. Use this when `alwaysIncludeLinkageData` is not granular enough.
|
5253 | ```js
|
5254 | export default JSONAPISerializer.extend({
|
5255 | shouldIncludeLinkageData(relationshipKey, model) {
|
5256 | if (relationshipKey === 'author' || relationshipKey === 'ghostWriter') {
|
5257 | return true;
|
5258 | }
|
5259 | return false;
|
5260 | }
|
5261 | });
|
5262 | ```
|
5263 | @method shouldIncludeLinkageData
|
5264 | @param {String} relationshipKey
|
5265 | @param {Model} model
|
5266 | @return {Boolean}
|
5267 | @public
|
5268 | */
|
5269 |
|
5270 |
|
5271 | shouldIncludeLinkageData(relationshipKey, model) {
|
5272 | return false;
|
5273 | }
|
5274 |
|
5275 | }
|
5276 |
|
5277 | JSONAPISerializer.prototype.alwaysIncludeLinkageData = false;
|
5278 |
|
5279 | /**
|
5280 | * @hide
|
5281 | */
|
5282 |
|
5283 | class SerializerRegistry {
|
5284 | constructor(schema, serializerMap = {}, server) {
|
5285 | this.schema = schema;
|
5286 | this._serializerMap = serializerMap;
|
5287 | }
|
5288 |
|
5289 | normalize(payload, modelName) {
|
5290 | return this.serializerFor(modelName).normalize(payload);
|
5291 | }
|
5292 |
|
5293 | serialize(response, request) {
|
5294 | this.request = request;
|
5295 |
|
5296 | if (this._isModelOrCollection(response)) {
|
5297 | let serializer = this.serializerFor(response.modelName);
|
5298 | return serializer.serialize(response, request);
|
5299 | } else if (Array.isArray(response) && response.some(this._isCollection)) {
|
5300 | return response.reduce((json, collection) => {
|
5301 | let serializer = this.serializerFor(collection.modelName);
|
5302 |
|
5303 | if (serializer.embed) {
|
5304 | json[this._container.inflector.pluralize(collection.modelName)] = serializer.serialize(collection, request);
|
5305 | } else {
|
5306 | json = Object.assign(json, serializer.serialize(collection, request));
|
5307 | }
|
5308 |
|
5309 | return json;
|
5310 | }, {});
|
5311 | } else {
|
5312 | return response;
|
5313 | }
|
5314 | }
|
5315 |
|
5316 | serializerFor(type, {
|
5317 | explicit = false
|
5318 | } = {}) {
|
5319 | let SerializerForResponse = type && this._serializerMap && this._serializerMap[camelize(type)];
|
5320 |
|
5321 | if (explicit) {
|
5322 | assert(!!SerializerForResponse, `You passed in ${type} as an explicit serializer type but that serializer doesn't exist.`);
|
5323 | } else {
|
5324 | SerializerForResponse = SerializerForResponse || this._serializerMap.application || Serializer;
|
5325 | assert(!SerializerForResponse || SerializerForResponse.prototype.embed || SerializerForResponse.prototype.root || new SerializerForResponse() instanceof JSONAPISerializer, "You cannot have a serializer that sideloads (embed: false) and disables the root (root: false).");
|
5326 | }
|
5327 |
|
5328 | return new SerializerForResponse(this, type, this.request);
|
5329 | }
|
5330 |
|
5331 | _isModel(object) {
|
5332 | return object instanceof Model;
|
5333 | }
|
5334 |
|
5335 | _isCollection(object) {
|
5336 | return object instanceof Collection || object instanceof PolymorphicCollection;
|
5337 | }
|
5338 |
|
5339 | _isModelOrCollection(object) {
|
5340 | return this._isModel(object) || this._isCollection(object);
|
5341 | }
|
5342 |
|
5343 | registerSerializers(newSerializerMaps) {
|
5344 | let currentSerializerMap = this._serializerMap || {};
|
5345 | this._serializerMap = Object.assign(currentSerializerMap, newSerializerMaps);
|
5346 | }
|
5347 |
|
5348 | getCoalescedIds(request, modelName) {
|
5349 | return this.serializerFor(modelName).getCoalescedIds(request);
|
5350 | }
|
5351 |
|
5352 | }
|
5353 |
|
5354 | const collectionNameCache = {};
|
5355 | const internalCollectionNameCache = {};
|
5356 | const modelNameCache = {};
|
5357 |
|
5358 |
|
5359 |
|
5360 |
|
5361 |
|
5362 |
|
5363 |
|
5364 |
|
5365 |
|
5366 |
|
5367 |
|
5368 |
|
5369 |
|
5370 |
|
5371 |
|
5372 |
|
5373 |
|
5374 |
|
5375 |
|
5376 |
|
5377 |
|
5378 |
|
5379 |
|
5380 |
|
5381 | class Schema {
|
5382 | constructor(db, modelsMap = {}) {
|
5383 | assert(db, "A schema requires a db");
|
5384 | |
5385 |
|
5386 |
|
5387 |
|
5388 |
|
5389 |
|
5390 |
|
5391 | this.db = db;
|
5392 | this._registry = {};
|
5393 | this._dependentAssociations = {
|
5394 | polymorphic: []
|
5395 | };
|
5396 | this.registerModels(modelsMap);
|
5397 | this.isSaving = {};
|
5398 | }
|
5399 | |
5400 |
|
5401 |
|
5402 |
|
5403 |
|
5404 |
|
5405 |
|
5406 |
|
5407 | registerModels(hash = {}) {
|
5408 | forIn(hash, (model, key) => {
|
5409 | this.registerModel(key, hash[key]);
|
5410 | });
|
5411 | }
|
5412 | |
5413 |
|
5414 |
|
5415 |
|
5416 |
|
5417 |
|
5418 |
|
5419 |
|
5420 |
|
5421 | registerModel(type, ModelClass) {
|
5422 | let camelizedModelName = camelize(type);
|
5423 | let modelName = dasherize(camelizedModelName);
|
5424 |
|
5425 | ModelClass = ModelClass.extend();
|
5426 |
|
5427 |
|
5428 | this._registry[camelizedModelName] = this._registry[camelizedModelName] || {
|
5429 | class: null,
|
5430 | foreignKeys: []
|
5431 | };
|
5432 |
|
5433 | this._registry[camelizedModelName].class = ModelClass;
|
5434 |
|
5435 | ModelClass.prototype._schema = this;
|
5436 | ModelClass.prototype.modelName = modelName;
|
5437 |
|
5438 | ModelClass.prototype.hasManyAssociations = {};
|
5439 |
|
5440 | ModelClass.prototype.hasManyAssociationFks = {};
|
5441 |
|
5442 | ModelClass.prototype.belongsToAssociations = {};
|
5443 |
|
5444 | ModelClass.prototype.belongsToAssociationFks = {};
|
5445 |
|
5446 | ModelClass.prototype.associationKeys = new Set();
|
5447 |
|
5448 | ModelClass.prototype.associationIdKeys = new Set();
|
5449 |
|
5450 | ModelClass.prototype.dependentAssociations = [];
|
5451 |
|
5452 | let fksAddedFromThisModel = {};
|
5453 |
|
5454 | for (let associationProperty in ModelClass.prototype) {
|
5455 | if (ModelClass.prototype[associationProperty] instanceof Association) {
|
5456 | let association = ModelClass.prototype[associationProperty];
|
5457 | association.key = associationProperty;
|
5458 | association.modelName = association.modelName || this.toModelName(associationProperty);
|
5459 | association.ownerModelName = modelName;
|
5460 | association.setSchema(this);
|
5461 |
|
5462 |
|
5463 | let [fkHolder, fk] = association.getForeignKeyArray();
|
5464 | fksAddedFromThisModel[fkHolder] = fksAddedFromThisModel[fkHolder] || [];
|
5465 | assert(!fksAddedFromThisModel[fkHolder].includes(fk), `Your '${type}' model definition has multiple possible inverse relationships of type '${fkHolder}'. Please use explicit inverses.`);
|
5466 | fksAddedFromThisModel[fkHolder].push(fk);
|
5467 |
|
5468 | this._addForeignKeyToRegistry(fkHolder, fk);
|
5469 |
|
5470 |
|
5471 | association.addMethodsToModelClass(ModelClass, associationProperty);
|
5472 | }
|
5473 | }
|
5474 |
|
5475 |
|
5476 | let collection = this.toCollectionName(modelName);
|
5477 |
|
5478 | if (!this.db[collection]) {
|
5479 | this.db.createCollection(collection);
|
5480 | }
|
5481 |
|
5482 |
|
5483 | this[collection] = {
|
5484 | camelizedModelName,
|
5485 | new: attrs => this.new(camelizedModelName, attrs),
|
5486 | create: attrs => this.create(camelizedModelName, attrs),
|
5487 | all: attrs => this.all(camelizedModelName, attrs),
|
5488 | find: attrs => this.find(camelizedModelName, attrs),
|
5489 | findBy: attrs => this.findBy(camelizedModelName, attrs),
|
5490 | findOrCreateBy: attrs => this.findOrCreateBy(camelizedModelName, attrs),
|
5491 | where: attrs => this.where(camelizedModelName, attrs),
|
5492 | none: attrs => this.none(camelizedModelName, attrs),
|
5493 | first: attrs => this.first(camelizedModelName, attrs)
|
5494 | };
|
5495 | return this;
|
5496 | }
|
5497 | |
5498 |
|
5499 |
|
5500 |
|
5501 |
|
5502 |
|
5503 |
|
5504 |
|
5505 | modelFor(type) {
|
5506 | return this._registry[type];
|
5507 | }
|
5508 | |
5509 |
|
5510 |
|
5511 |
|
5512 |
|
5513 |
|
5514 |
|
5515 |
|
5516 |
|
5517 |
|
5518 |
|
5519 |
|
5520 |
|
5521 |
|
5522 |
|
5523 | new(type, attrs) {
|
5524 | return this._instantiateModel(dasherize(type), attrs);
|
5525 | }
|
5526 | |
5527 |
|
5528 |
|
5529 |
|
5530 |
|
5531 |
|
5532 |
|
5533 |
|
5534 |
|
5535 |
|
5536 |
|
5537 |
|
5538 |
|
5539 |
|
5540 |
|
5541 | create(type, attrs) {
|
5542 | return this.new(type, attrs).save();
|
5543 | }
|
5544 | |
5545 |
|
5546 |
|
5547 |
|
5548 |
|
5549 |
|
5550 |
|
5551 |
|
5552 |
|
5553 |
|
5554 |
|
5555 |
|
5556 | all(type) {
|
5557 | let collection = this.collectionForType(type);
|
5558 | return this._hydrate(collection, dasherize(type));
|
5559 | }
|
5560 | |
5561 |
|
5562 |
|
5563 |
|
5564 |
|
5565 |
|
5566 |
|
5567 |
|
5568 | none(type) {
|
5569 | return this._hydrate([], dasherize(type));
|
5570 | }
|
5571 | |
5572 |
|
5573 |
|
5574 |
|
5575 |
|
5576 |
|
5577 |
|
5578 |
|
5579 |
|
5580 |
|
5581 |
|
5582 |
|
5583 |
|
5584 | find(type, ids) {
|
5585 | let collection = this.collectionForType(type);
|
5586 | let records = collection.find(ids);
|
5587 |
|
5588 | if (Array.isArray(ids)) {
|
5589 | assert(records.length === ids.length, `Couldn't find all ${this._container.inflector.pluralize(type)} with ids: (${ids.join(",")}) (found ${records.length} results, but was looking for ${ids.length})`);
|
5590 | }
|
5591 |
|
5592 | return this._hydrate(records, dasherize(type));
|
5593 | }
|
5594 | |
5595 |
|
5596 |
|
5597 |
|
5598 |
|
5599 |
|
5600 |
|
5601 |
|
5602 |
|
5603 |
|
5604 |
|
5605 |
|
5606 |
|
5607 | findBy(type, query) {
|
5608 | let collection = this.collectionForType(type);
|
5609 | let record = collection.findBy(query);
|
5610 | return this._hydrate(record, dasherize(type));
|
5611 | }
|
5612 | |
5613 |
|
5614 |
|
5615 |
|
5616 |
|
5617 |
|
5618 |
|
5619 |
|
5620 |
|
5621 |
|
5622 |
|
5623 |
|
5624 |
|
5625 | findOrCreateBy(type, attrs) {
|
5626 | let collection = this.collectionForType(type);
|
5627 | let record = collection.findBy(attrs);
|
5628 | let model;
|
5629 |
|
5630 | if (!record) {
|
5631 | model = this.create(type, attrs);
|
5632 | } else {
|
5633 | model = this._hydrate(record, dasherize(type));
|
5634 | }
|
5635 |
|
5636 | return model;
|
5637 | }
|
5638 | |
5639 |
|
5640 |
|
5641 |
|
5642 |
|
5643 |
|
5644 |
|
5645 |
|
5646 |
|
5647 |
|
5648 |
|
5649 |
|
5650 |
|
5651 |
|
5652 |
|
5653 | where(type, query) {
|
5654 | let collection = this.collectionForType(type);
|
5655 | let records = collection.where(query);
|
5656 | return this._hydrate(records, dasherize(type));
|
5657 | }
|
5658 | |
5659 |
|
5660 |
|
5661 |
|
5662 |
|
5663 |
|
5664 |
|
5665 |
|
5666 |
|
5667 |
|
5668 |
|
5669 |
|
5670 | first(type) {
|
5671 | let collection = this.collectionForType(type);
|
5672 | let record = collection[0];
|
5673 | return this._hydrate(record, dasherize(type));
|
5674 | }
|
5675 | |
5676 |
|
5677 |
|
5678 |
|
5679 |
|
5680 |
|
5681 |
|
5682 |
|
5683 | modelClassFor(modelName) {
|
5684 | let model = this._registry[camelize(modelName)];
|
5685 |
|
5686 | assert(model, `Model not registered: ${modelName}`);
|
5687 | return model.class.prototype;
|
5688 | }
|
5689 | |
5690 |
|
5691 |
|
5692 |
|
5693 |
|
5694 |
|
5695 |
|
5696 |
|
5697 |
|
5698 |
|
5699 |
|
5700 |
|
5701 |
|
5702 |
|
5703 |
|
5704 |
|
5705 |
|
5706 |
|
5707 |
|
5708 |
|
5709 |
|
5710 | addDependentAssociation(association, modelName) {
|
5711 | if (association.isPolymorphic) {
|
5712 | this._dependentAssociations.polymorphic.push(association);
|
5713 | } else {
|
5714 | this._dependentAssociations[modelName] = this._dependentAssociations[modelName] || [];
|
5715 |
|
5716 | this._dependentAssociations[modelName].push(association);
|
5717 | }
|
5718 | }
|
5719 |
|
5720 | dependentAssociationsFor(modelName) {
|
5721 | let directDependents = this._dependentAssociations[modelName] || [];
|
5722 | let polymorphicAssociations = this._dependentAssociations.polymorphic || [];
|
5723 | return directDependents.concat(polymorphicAssociations);
|
5724 | }
|
5725 |
|
5726 | associationsFor(modelName) {
|
5727 | let modelClass = this.modelClassFor(modelName);
|
5728 | return Object.assign({}, modelClass.belongsToAssociations, modelClass.hasManyAssociations);
|
5729 | }
|
5730 |
|
5731 | hasModelForModelName(modelName) {
|
5732 | return this.modelFor(camelize(modelName));
|
5733 | }
|
5734 | |
5735 |
|
5736 |
|
5737 |
|
5738 | |
5739 |
|
5740 |
|
5741 |
|
5742 |
|
5743 |
|
5744 |
|
5745 |
|
5746 | collectionForType(type) {
|
5747 | let collection = this.toCollectionName(type);
|
5748 | assert(this.db[collection], `You're trying to find model(s) of type ${type} but this collection doesn't exist in the database.`);
|
5749 | return this.db[collection];
|
5750 | }
|
5751 |
|
5752 | toCollectionName(type) {
|
5753 | if (typeof collectionNameCache[type] !== "string") {
|
5754 | let modelName = dasherize(type);
|
5755 | const collectionName = camelize(this._container.inflector.pluralize(modelName));
|
5756 | collectionNameCache[type] = collectionName;
|
5757 | }
|
5758 |
|
5759 | return collectionNameCache[type];
|
5760 | }
|
5761 |
|
5762 |
|
5763 |
|
5764 | toInternalCollectionName(type) {
|
5765 | if (typeof internalCollectionNameCache[type] !== "string") {
|
5766 | const internalCollectionName = `_${this.toCollectionName(type)}`;
|
5767 | internalCollectionNameCache[type] = internalCollectionName;
|
5768 | }
|
5769 |
|
5770 | return internalCollectionNameCache[type];
|
5771 | }
|
5772 |
|
5773 | toModelName(type) {
|
5774 | if (typeof modelNameCache[type] !== "string") {
|
5775 | let dasherized = dasherize(type);
|
5776 |
|
5777 | const modelName = this._container.inflector.singularize(dasherized);
|
5778 |
|
5779 | modelNameCache[type] = modelName;
|
5780 | }
|
5781 |
|
5782 | return modelNameCache[type];
|
5783 | }
|
5784 | |
5785 |
|
5786 |
|
5787 |
|
5788 |
|
5789 |
|
5790 |
|
5791 |
|
5792 |
|
5793 | _addForeignKeyToRegistry(type, fk) {
|
5794 | this._registry[type] = this._registry[type] || {
|
5795 | class: null,
|
5796 | foreignKeys: []
|
5797 | };
|
5798 | let fks = this._registry[type].foreignKeys;
|
5799 |
|
5800 | if (!fks.includes(fk)) {
|
5801 | fks.push(fk);
|
5802 | }
|
5803 | }
|
5804 | |
5805 |
|
5806 |
|
5807 |
|
5808 |
|
5809 |
|
5810 |
|
5811 |
|
5812 |
|
5813 | _instantiateModel(modelName, attrs) {
|
5814 | let ModelClass = this._modelFor(modelName);
|
5815 |
|
5816 | let fks = this._foreignKeysFor(modelName);
|
5817 |
|
5818 | return new ModelClass(this, modelName, attrs, fks);
|
5819 | }
|
5820 | |
5821 |
|
5822 |
|
5823 |
|
5824 |
|
5825 |
|
5826 |
|
5827 |
|
5828 | _modelFor(modelName) {
|
5829 | return this._registry[camelize(modelName)].class;
|
5830 | }
|
5831 | |
5832 |
|
5833 |
|
5834 |
|
5835 |
|
5836 |
|
5837 |
|
5838 |
|
5839 | _foreignKeysFor(modelName) {
|
5840 | return this._registry[camelize(modelName)].foreignKeys;
|
5841 | }
|
5842 | |
5843 |
|
5844 |
|
5845 |
|
5846 |
|
5847 |
|
5848 |
|
5849 |
|
5850 |
|
5851 |
|
5852 |
|
5853 |
|
5854 | _hydrate(records, modelName) {
|
5855 | if (Array.isArray(records)) {
|
5856 | let models = records.map(function (record) {
|
5857 | return this._instantiateModel(modelName, record);
|
5858 | }, this);
|
5859 | return new Collection(modelName, models);
|
5860 | } else if (records) {
|
5861 | return this._instantiateModel(modelName, records);
|
5862 | } else {
|
5863 | return null;
|
5864 | }
|
5865 | }
|
5866 |
|
5867 | }
|
5868 |
|
5869 | const classes = {
|
5870 | Db,
|
5871 | Association,
|
5872 | RouteHandler,
|
5873 | BaseRouteHandler,
|
5874 | Serializer,
|
5875 | SerializerRegistry,
|
5876 | Schema
|
5877 | };
|
5878 | let defaultInflector = {
|
5879 | singularize: inflected.singularize,
|
5880 | pluralize: inflected.pluralize
|
5881 | };
|
5882 |
|
5883 |
|
5884 |
|
5885 |
|
5886 |
|
5887 |
|
5888 |
|
5889 |
|
5890 | class Container {
|
5891 | constructor() {
|
5892 | this.inflector = defaultInflector;
|
5893 | }
|
5894 |
|
5895 | register(key, value) {
|
5896 | this[key] = value;
|
5897 | }
|
5898 |
|
5899 | create(className, ...args) {
|
5900 | let Class = classes[className];
|
5901 | Class.prototype._container = this;
|
5902 | return new Class(...args);
|
5903 | }
|
5904 |
|
5905 | }
|
5906 |
|
5907 |
|
5908 |
|
5909 |
|
5910 |
|
5911 |
|
5912 |
|
5913 |
|
5914 |
|
5915 | let defaultContainer = new Container();
|
5916 | Db.prototype._container = defaultContainer;
|
5917 | Association.prototype._container = defaultContainer;
|
5918 | BaseRouteHandler.prototype._container = defaultContainer;
|
5919 | RouteHandler.prototype._container = defaultContainer;
|
5920 | Serializer.prototype._container = defaultContainer;
|
5921 | SerializerRegistry.prototype._container = defaultContainer;
|
5922 | Schema.prototype._container = defaultContainer;
|
5923 |
|
5924 |
|
5925 | const isPluralForModelCache = {};
|
5926 |
|
5927 |
|
5928 |
|
5929 |
|
5930 |
|
5931 |
|
5932 |
|
5933 |
|
5934 |
|
5935 | function createPretender(server) {
|
5936 | if (typeof window !== "undefined") {
|
5937 | return new Pretender(function () {
|
5938 | this.passthroughRequest = function (verb, path, request) {
|
5939 | if (server.shouldLog()) {
|
5940 | console.log(`Mirage: Passthrough request for ${verb.toUpperCase()} ${request.url}`);
|
5941 | }
|
5942 | };
|
5943 |
|
5944 | this.handledRequest = function (verb, path, request) {
|
5945 | if (server.shouldLog()) {
|
5946 | console.groupCollapsed(`Mirage: [${request.status}] ${verb.toUpperCase()} ${request.url}`);
|
5947 | let {
|
5948 | requestBody,
|
5949 | responseText
|
5950 | } = request;
|
5951 | let loggedRequest, loggedResponse;
|
5952 |
|
5953 | try {
|
5954 | loggedRequest = JSON.parse(requestBody);
|
5955 | } catch (e) {
|
5956 | loggedRequest = requestBody;
|
5957 | }
|
5958 |
|
5959 | try {
|
5960 | loggedResponse = JSON.parse(responseText);
|
5961 | } catch (e) {
|
5962 | loggedResponse = responseText;
|
5963 | }
|
5964 |
|
5965 | console.groupCollapsed("Response");
|
5966 | console.log(loggedResponse);
|
5967 | console.groupEnd();
|
5968 | console.groupCollapsed("Request (data)");
|
5969 | console.log(loggedRequest);
|
5970 | console.groupEnd();
|
5971 | console.groupCollapsed("Request (raw)");
|
5972 | console.log(request);
|
5973 | console.groupEnd();
|
5974 | console.groupEnd();
|
5975 | }
|
5976 | };
|
5977 |
|
5978 | let originalCheckPassthrough = this.checkPassthrough;
|
5979 |
|
5980 | this.checkPassthrough = function (request) {
|
5981 | let shouldPassthrough = server.passthroughChecks.some(passthroughCheck => passthroughCheck(request));
|
5982 |
|
5983 | if (shouldPassthrough) {
|
5984 | let url = request.url.includes("?") ? request.url.substr(0, request.url.indexOf("?")) : request.url;
|
5985 | this[request.method.toLowerCase()](url, this.passthrough);
|
5986 | }
|
5987 |
|
5988 | return originalCheckPassthrough.apply(this, arguments);
|
5989 | };
|
5990 |
|
5991 | this.unhandledRequest = function (verb, path) {
|
5992 | path = decodeURI(path);
|
5993 | assert(`Your app tried to ${verb} '${path}', but there was no route defined to handle this request. Define a route for this endpoint in your routes() config. Did you forget to define a namespace?`);
|
5994 | };
|
5995 | }, {
|
5996 | trackRequests: server.shouldTrackRequests()
|
5997 | });
|
5998 | }
|
5999 | }
|
6000 |
|
6001 | const defaultRouteOptions = {
|
6002 | coalesce: false,
|
6003 | timing: undefined
|
6004 | };
|
6005 | const defaultInflector$1 = {
|
6006 | singularize: inflected.singularize,
|
6007 | pluralize: inflected.pluralize
|
6008 | };
|
6009 |
|
6010 |
|
6011 |
|
6012 |
|
6013 | const defaultPassthroughs = ["http://localhost:0/chromecheckurl",
|
6014 | "http://localhost:30820/socket.io",
|
6015 | request => {
|
6016 | return /.+\.hot-update.json$/.test(request.url);
|
6017 | }];
|
6018 |
|
6019 |
|
6020 |
|
6021 |
|
6022 |
|
6023 |
|
6024 |
|
6025 |
|
6026 |
|
6027 | function isOption(option) {
|
6028 | if (!option || typeof option !== "object") {
|
6029 | return false;
|
6030 | }
|
6031 |
|
6032 | let allOptions = Object.keys(defaultRouteOptions);
|
6033 | let optionKeys = Object.keys(option);
|
6034 |
|
6035 | for (let i = 0; i < optionKeys.length; i++) {
|
6036 | let key = optionKeys[i];
|
6037 |
|
6038 | if (allOptions.indexOf(key) > -1) {
|
6039 | return true;
|
6040 | }
|
6041 | }
|
6042 |
|
6043 | return false;
|
6044 | }
|
6045 |
|
6046 |
|
6047 |
|
6048 |
|
6049 |
|
6050 |
|
6051 |
|
6052 |
|
6053 |
|
6054 |
|
6055 |
|
6056 |
|
6057 | function extractRouteArguments(args) {
|
6058 | let [lastArg] = args.splice(-1);
|
6059 |
|
6060 | if (isOption(lastArg)) {
|
6061 | lastArg = assign({}, defaultRouteOptions, lastArg);
|
6062 | } else {
|
6063 | args.push(lastArg);
|
6064 | lastArg = defaultRouteOptions;
|
6065 | }
|
6066 |
|
6067 | let t = 2 - args.length;
|
6068 |
|
6069 | while (t-- > 0) {
|
6070 | args.push(undefined);
|
6071 | }
|
6072 |
|
6073 | args.push(lastArg);
|
6074 | return args;
|
6075 | }
|
6076 |
|
6077 |
|
6078 |
|
6079 |
|
6080 |
|
6081 |
|
6082 |
|
6083 |
|
6084 |
|
6085 |
|
6086 | class Server {
|
6087 | constructor(options = {}) {
|
6088 | this._container = new Container();
|
6089 | this.config(options);
|
6090 | |
6091 |
|
6092 |
|
6093 |
|
6094 |
|
6095 |
|
6096 | this.db = this.db || undefined;
|
6097 | |
6098 |
|
6099 |
|
6100 |
|
6101 |
|
6102 |
|
6103 | this.schema = this.schema || undefined;
|
6104 | }
|
6105 |
|
6106 | config(config = {}) {
|
6107 | this.passthroughChecks = this.passthroughChecks || [];
|
6108 | let didOverrideConfig = config.environment && this.environment && this.environment !== config.environment;
|
6109 | assert(!didOverrideConfig, "You cannot modify Mirage's environment once the server is created");
|
6110 | this.environment = config.environment || this.environment || "development";
|
6111 |
|
6112 | if (config.routes) {
|
6113 | assert(!config.baseConfig, "The routes option is an alias for the baseConfig option. You can't pass both options into your server definition.");
|
6114 | config.baseConfig = config.routes;
|
6115 | }
|
6116 |
|
6117 | if (config.seeds) {
|
6118 | assert(!config.scenarios, "The seeds option is an alias for the scenarios.default option. You can't pass both options into your server definition.");
|
6119 | config.scenarios = {
|
6120 | default: config.seeds
|
6121 | };
|
6122 | }
|
6123 |
|
6124 | this._config = config;
|
6125 | |
6126 |
|
6127 |
|
6128 |
|
6129 |
|
6130 |
|
6131 |
|
6132 |
|
6133 |
|
6134 |
|
6135 |
|
6136 |
|
6137 |
|
6138 |
|
6139 |
|
6140 |
|
6141 |
|
6142 |
|
6143 |
|
6144 |
|
6145 |
|
6146 |
|
6147 |
|
6148 |
|
6149 |
|
6150 |
|
6151 |
|
6152 |
|
6153 |
|
6154 |
|
6155 |
|
6156 | this.namespace = this.namespace || config.namespace || "";
|
6157 | |
6158 |
|
6159 |
|
6160 |
|
6161 | this.inflector = config.inflector || defaultInflector$1;
|
6162 |
|
6163 | this._container.register("inflector", this.inflector);
|
6164 | |
6165 |
|
6166 |
|
6167 |
|
6168 |
|
6169 |
|
6170 |
|
6171 |
|
6172 |
|
6173 |
|
6174 |
|
6175 |
|
6176 |
|
6177 | this.urlPrefix = this.urlPrefix || config.urlPrefix || "";
|
6178 | |
6179 |
|
6180 |
|
6181 |
|
6182 |
|
6183 |
|
6184 |
|
6185 |
|
6186 |
|
6187 |
|
6188 |
|
6189 |
|
6190 |
|
6191 |
|
6192 |
|
6193 |
|
6194 | this.timing = this.timing || config.timing || 400;
|
6195 | |
6196 |
|
6197 |
|
6198 |
|
6199 |
|
6200 |
|
6201 |
|
6202 |
|
6203 |
|
6204 |
|
6205 |
|
6206 |
|
6207 |
|
6208 |
|
6209 |
|
6210 |
|
6211 |
|
6212 |
|
6213 |
|
6214 |
|
6215 |
|
6216 |
|
6217 |
|
6218 |
|
6219 |
|
6220 |
|
6221 |
|
6222 |
|
6223 |
|
6224 | this.logging = this.logging !== undefined ? this.logging : undefined;
|
6225 | this.testConfig = this.testConfig || undefined;
|
6226 | this.trackRequests = config.trackRequests;
|
6227 |
|
6228 | this._defineRouteHandlerHelpers();
|
6229 |
|
6230 | if (this.db) {
|
6231 | this.db.registerIdentityManagers(config.identityManagers);
|
6232 | } else {
|
6233 | this.db = this._container.create("Db", undefined, config.identityManagers);
|
6234 | }
|
6235 |
|
6236 | if (this.schema) {
|
6237 | this.schema.registerModels(config.models);
|
6238 | this.serializerOrRegistry.registerSerializers(config.serializers || {});
|
6239 | } else {
|
6240 | this.schema = this._container.create("Schema", this.db, config.models);
|
6241 | this.serializerOrRegistry = this._container.create("SerializerRegistry", this.schema, config.serializers);
|
6242 | }
|
6243 |
|
6244 | let hasFactories = this._hasModulesOfType(config, "factories");
|
6245 |
|
6246 | let hasDefaultScenario = config.scenarios && Object.prototype.hasOwnProperty.call(config.scenarios, "default");
|
6247 | let didOverridePretenderConfig = config.trackRequests !== undefined && this.pretender;
|
6248 | assert(!didOverridePretenderConfig, "You cannot modify Pretender's request tracking once the server is created");
|
6249 | |
6250 |
|
6251 |
|
6252 |
|
6253 |
|
6254 |
|
6255 |
|
6256 |
|
6257 |
|
6258 |
|
6259 |
|
6260 |
|
6261 |
|
6262 |
|
6263 |
|
6264 |
|
6265 |
|
6266 | this.pretender = this.pretender || config.pretender || createPretender(this);
|
6267 |
|
6268 | if (config.baseConfig) {
|
6269 | this.loadConfig(config.baseConfig);
|
6270 | }
|
6271 |
|
6272 | if (this.isTest()) {
|
6273 | if (config.testConfig) {
|
6274 | this.loadConfig(config.testConfig);
|
6275 | }
|
6276 |
|
6277 | if (typeof window !== "undefined") {
|
6278 | window.server = this;
|
6279 | }
|
6280 | }
|
6281 |
|
6282 | if (this.isTest() && hasFactories) {
|
6283 | this.loadFactories(config.factories);
|
6284 | } else if (!this.isTest() && hasDefaultScenario) {
|
6285 | this.loadFactories(config.factories);
|
6286 | config.scenarios.default(this);
|
6287 | } else {
|
6288 | this.loadFixtures();
|
6289 | }
|
6290 |
|
6291 | let useDefaultPassthroughs = typeof config.useDefaultPassthroughs !== "undefined" ? config.useDefaultPassthroughs : true;
|
6292 |
|
6293 | if (useDefaultPassthroughs) {
|
6294 | this._configureDefaultPassthroughs();
|
6295 | }
|
6296 | }
|
6297 | |
6298 |
|
6299 |
|
6300 |
|
6301 |
|
6302 |
|
6303 |
|
6304 |
|
6305 |
|
6306 |
|
6307 | isTest() {
|
6308 | return this.environment === "test";
|
6309 | }
|
6310 | |
6311 |
|
6312 |
|
6313 |
|
6314 |
|
6315 |
|
6316 |
|
6317 |
|
6318 |
|
6319 |
|
6320 | shouldLog() {
|
6321 | return typeof this.logging !== "undefined" ? this.logging : !this.isTest();
|
6322 | }
|
6323 | |
6324 |
|
6325 |
|
6326 |
|
6327 |
|
6328 |
|
6329 |
|
6330 |
|
6331 |
|
6332 |
|
6333 | shouldTrackRequests() {
|
6334 | return Boolean(this.trackRequests);
|
6335 | }
|
6336 | |
6337 |
|
6338 |
|
6339 |
|
6340 |
|
6341 |
|
6342 |
|
6343 |
|
6344 |
|
6345 |
|
6346 |
|
6347 | loadConfig(config) {
|
6348 | config.call(this);
|
6349 | this.timing = this.isTest() ? 0 : this.timing || 0;
|
6350 | }
|
6351 | |
6352 |
|
6353 |
|
6354 |
|
6355 |
|
6356 |
|
6357 |
|
6358 |
|
6359 |
|
6360 |
|
6361 |
|
6362 |
|
6363 |
|
6364 |
|
6365 |
|
6366 |
|
6367 |
|
6368 |
|
6369 |
|
6370 |
|
6371 |
|
6372 |
|
6373 |
|
6374 |
|
6375 |
|
6376 |
|
6377 |
|
6378 |
|
6379 |
|
6380 |
|
6381 |
|
6382 |
|
6383 |
|
6384 |
|
6385 |
|
6386 |
|
6387 |
|
6388 |
|
6389 |
|
6390 |
|
6391 |
|
6392 |
|
6393 |
|
6394 |
|
6395 | passthrough(...paths) {
|
6396 |
|
6397 |
|
6398 | if (typeof window !== "undefined") {
|
6399 | let verbs = ["get", "post", "put", "delete", "patch", "options", "head"];
|
6400 | let lastArg = paths[paths.length - 1];
|
6401 |
|
6402 | if (paths.length === 0) {
|
6403 | paths = ["/**", "/"];
|
6404 | } else if (Array.isArray(lastArg)) {
|
6405 | verbs = paths.pop();
|
6406 | }
|
6407 |
|
6408 | paths.forEach(path => {
|
6409 | if (typeof path === "function") {
|
6410 | this.passthroughChecks.push(path);
|
6411 | } else {
|
6412 | verbs.forEach(verb => {
|
6413 | let fullPath = this._getFullPath(path);
|
6414 |
|
6415 | this.pretender[verb](fullPath, this.pretender.passthrough);
|
6416 | });
|
6417 | }
|
6418 | });
|
6419 | }
|
6420 | }
|
6421 | |
6422 |
|
6423 |
|
6424 |
|
6425 |
|
6426 |
|
6427 |
|
6428 |
|
6429 |
|
6430 |
|
6431 |
|
6432 |
|
6433 |
|
6434 |
|
6435 |
|
6436 |
|
6437 |
|
6438 |
|
6439 |
|
6440 |
|
6441 |
|
6442 |
|
6443 |
|
6444 |
|
6445 |
|
6446 |
|
6447 |
|
6448 |
|
6449 |
|
6450 |
|
6451 |
|
6452 | loadFixtures(...args) {
|
6453 | let {
|
6454 | fixtures
|
6455 | } = this._config;
|
6456 |
|
6457 | if (args.length) {
|
6458 | let camelizedArgs = args.map(camelize);
|
6459 | let missingKeys = camelizedArgs.filter(key => !fixtures[key]);
|
6460 |
|
6461 | if (missingKeys.length) {
|
6462 | throw new Error(`Fixtures not found: ${missingKeys.join(", ")}`);
|
6463 | }
|
6464 |
|
6465 | fixtures = pick(fixtures, ...camelizedArgs);
|
6466 | }
|
6467 |
|
6468 | this.db.loadData(fixtures);
|
6469 | }
|
6470 | |
6471 |
|
6472 |
|
6473 |
|
6474 | |
6475 |
|
6476 |
|
6477 |
|
6478 |
|
6479 |
|
6480 |
|
6481 |
|
6482 |
|
6483 |
|
6484 | loadFactories(factoryMap = {}) {
|
6485 |
|
6486 | let currentFactoryMap = this._factoryMap || {};
|
6487 | this._factoryMap = assign(currentFactoryMap, factoryMap);
|
6488 |
|
6489 | Object.keys(factoryMap).forEach(type => {
|
6490 | let collectionName = this.schema.toCollectionName(type);
|
6491 | this.db.createCollection(collectionName);
|
6492 | });
|
6493 | }
|
6494 | |
6495 |
|
6496 |
|
6497 |
|
6498 |
|
6499 |
|
6500 |
|
6501 |
|
6502 |
|
6503 |
|
6504 | factoryFor(type) {
|
6505 | let camelizedType = camelize(type);
|
6506 |
|
6507 | if (this._factoryMap && this._factoryMap[camelizedType]) {
|
6508 | return this._factoryMap[camelizedType];
|
6509 | }
|
6510 | }
|
6511 |
|
6512 | build(type, ...traitsAndOverrides) {
|
6513 | let traits = traitsAndOverrides.filter(arg => arg && typeof arg === "string");
|
6514 | let overrides = find(traitsAndOverrides, arg => isPlainObject(arg));
|
6515 | let camelizedType = camelize(type);
|
6516 |
|
6517 | this.factorySequences = this.factorySequences || {};
|
6518 | this.factorySequences[camelizedType] = this.factorySequences[camelizedType] + 1 || 0;
|
6519 | let OriginalFactory = this.factoryFor(type);
|
6520 |
|
6521 | if (OriginalFactory) {
|
6522 | OriginalFactory = OriginalFactory.extend({});
|
6523 | let attrs = OriginalFactory.attrs || {};
|
6524 |
|
6525 | this._validateTraits(traits, OriginalFactory, type);
|
6526 |
|
6527 | let mergedExtensions = this._mergeExtensions(attrs, traits, overrides);
|
6528 |
|
6529 | this._mapAssociationsFromAttributes(type, attrs, overrides);
|
6530 |
|
6531 | this._mapAssociationsFromAttributes(type, mergedExtensions);
|
6532 |
|
6533 | let Factory = OriginalFactory.extend(mergedExtensions);
|
6534 | let factory = new Factory();
|
6535 | let sequence = this.factorySequences[camelizedType];
|
6536 | return factory.build(sequence);
|
6537 | } else {
|
6538 | return overrides;
|
6539 | }
|
6540 | }
|
6541 |
|
6542 | buildList(type, amount, ...traitsAndOverrides) {
|
6543 | assert(isInteger(amount), `second argument has to be an integer, you passed: ${typeof amount}`);
|
6544 | let list = [];
|
6545 | const buildArgs = [type, ...traitsAndOverrides];
|
6546 |
|
6547 | for (let i = 0; i < amount; i++) {
|
6548 | list.push(this.build.apply(this, buildArgs));
|
6549 | }
|
6550 |
|
6551 | return list;
|
6552 | }
|
6553 | |
6554 |
|
6555 |
|
6556 |
|
6557 |
|
6558 |
|
6559 |
|
6560 |
|
6561 |
|
6562 |
|
6563 |
|
6564 |
|
6565 |
|
6566 |
|
6567 |
|
6568 |
|
6569 |
|
6570 |
|
6571 |
|
6572 |
|
6573 |
|
6574 |
|
6575 |
|
6576 |
|
6577 |
|
6578 |
|
6579 |
|
6580 |
|
6581 |
|
6582 |
|
6583 |
|
6584 |
|
6585 |
|
6586 |
|
6587 |
|
6588 |
|
6589 | create(type, ...options) {
|
6590 | assert(this._modelOrFactoryExistsForType(type), `You called server.create('${type}') but no model or factory was found. Make sure you're passing in the singularized version of the model or factory name.`);
|
6591 |
|
6592 |
|
6593 | let traits = options.filter(arg => arg && typeof arg === "string");
|
6594 | let overrides = find(options, arg => isPlainObject(arg));
|
6595 | let collectionFromCreateList = find(options, arg => arg && Array.isArray(arg));
|
6596 | let attrs = this.build(type, ...traits, overrides);
|
6597 | let modelOrRecord;
|
6598 |
|
6599 | if (this.schema && this.schema[this.schema.toCollectionName(type)]) {
|
6600 | let modelClass = this.schema[this.schema.toCollectionName(type)];
|
6601 | modelOrRecord = modelClass.create(attrs);
|
6602 | } else {
|
6603 | let collection, collectionName;
|
6604 |
|
6605 | if (collectionFromCreateList) {
|
6606 | collection = collectionFromCreateList;
|
6607 | } else {
|
6608 | collectionName = this.schema ? this.schema.toInternalCollectionName(type) : `_${this.inflector.pluralize(type)}`;
|
6609 | collection = this.db[collectionName];
|
6610 | }
|
6611 |
|
6612 | assert(collection, `You called server.create('${type}') but no model or factory was found.`);
|
6613 | modelOrRecord = collection.insert(attrs);
|
6614 | }
|
6615 |
|
6616 | let OriginalFactory = this.factoryFor(type);
|
6617 |
|
6618 | if (OriginalFactory) {
|
6619 | OriginalFactory.extractAfterCreateCallbacks({
|
6620 | traits
|
6621 | }).forEach(afterCreate => {
|
6622 | afterCreate(modelOrRecord, this);
|
6623 | });
|
6624 | }
|
6625 |
|
6626 | return modelOrRecord;
|
6627 | }
|
6628 | |
6629 |
|
6630 |
|
6631 |
|
6632 |
|
6633 |
|
6634 |
|
6635 |
|
6636 |
|
6637 |
|
6638 |
|
6639 |
|
6640 |
|
6641 |
|
6642 |
|
6643 |
|
6644 |
|
6645 |
|
6646 |
|
6647 |
|
6648 |
|
6649 |
|
6650 |
|
6651 |
|
6652 |
|
6653 |
|
6654 |
|
6655 |
|
6656 |
|
6657 |
|
6658 |
|
6659 |
|
6660 | createList(type, amount, ...traitsAndOverrides) {
|
6661 | assert(this._modelOrFactoryExistsForType(type), `You called server.createList('${type}') but no model or factory was found. Make sure you're passing in the singularized version of the model or factory name.`);
|
6662 | assert(isInteger(amount), `second argument has to be an integer, you passed: ${typeof amount}`);
|
6663 | let list = [];
|
6664 | let collectionName = this.schema ? this.schema.toInternalCollectionName(type) : `_${this.inflector.pluralize(type)}`;
|
6665 | let collection = this.db[collectionName];
|
6666 | const createArguments = [type, ...traitsAndOverrides, collection];
|
6667 |
|
6668 | for (let i = 0; i < amount; i++) {
|
6669 | list.push(this.create.apply(this, createArguments));
|
6670 | }
|
6671 |
|
6672 | return list;
|
6673 | }
|
6674 | |
6675 |
|
6676 |
|
6677 |
|
6678 |
|
6679 |
|
6680 |
|
6681 | shutdown() {
|
6682 | if (typeof window !== "undefined") {
|
6683 | this.pretender.shutdown();
|
6684 | }
|
6685 |
|
6686 | if (typeof window !== "undefined" && this.environment === "test") {
|
6687 | window.server = undefined;
|
6688 | }
|
6689 | }
|
6690 |
|
6691 | resource(resourceName, {
|
6692 | only,
|
6693 | except,
|
6694 | path
|
6695 | } = {}) {
|
6696 | resourceName = this.inflector.pluralize(resourceName);
|
6697 | path = path || `/${resourceName}`;
|
6698 | only = only || [];
|
6699 | except = except || [];
|
6700 |
|
6701 | if (only.length > 0 && except.length > 0) {
|
6702 | throw "cannot use both :only and :except options";
|
6703 | }
|
6704 |
|
6705 | let actionsMethodsAndsPathsMappings = {
|
6706 | index: {
|
6707 | methods: ["get"],
|
6708 | path: `${path}`
|
6709 | },
|
6710 | show: {
|
6711 | methods: ["get"],
|
6712 | path: `${path}/:id`
|
6713 | },
|
6714 | create: {
|
6715 | methods: ["post"],
|
6716 | path: `${path}`
|
6717 | },
|
6718 | update: {
|
6719 | methods: ["put", "patch"],
|
6720 | path: `${path}/:id`
|
6721 | },
|
6722 | delete: {
|
6723 | methods: ["del"],
|
6724 | path: `${path}/:id`
|
6725 | }
|
6726 | };
|
6727 | let allActions = Object.keys(actionsMethodsAndsPathsMappings);
|
6728 | let actions = only.length > 0 && only || except.length > 0 && allActions.filter(action => except.indexOf(action) === -1) || allActions;
|
6729 | actions.forEach(action => {
|
6730 | let methodsWithPath = actionsMethodsAndsPathsMappings[action];
|
6731 | methodsWithPath.methods.forEach(method => {
|
6732 | return path === resourceName ? this[method](methodsWithPath.path) : this[method](methodsWithPath.path, resourceName);
|
6733 | });
|
6734 | });
|
6735 | }
|
6736 | |
6737 |
|
6738 |
|
6739 |
|
6740 |
|
6741 |
|
6742 |
|
6743 | _defineRouteHandlerHelpers() {
|
6744 | [["get"], ["post"], ["put"], ["delete", "del"], ["patch"], ["head"], ["options"]].forEach(([verb, alias]) => {
|
6745 | this[verb] = (path, ...args) => {
|
6746 | let [rawHandler, customizedCode, options] = extractRouteArguments(args);
|
6747 | return this._registerRouteHandler(verb, path, rawHandler, customizedCode, options);
|
6748 | };
|
6749 |
|
6750 | if (alias) {
|
6751 | this[alias] = this[verb];
|
6752 | }
|
6753 | });
|
6754 | }
|
6755 |
|
6756 | _serialize(body) {
|
6757 | if (typeof body === "string") {
|
6758 | return body;
|
6759 | } else {
|
6760 | return JSON.stringify(body);
|
6761 | }
|
6762 | }
|
6763 |
|
6764 | _registerRouteHandler(verb, path, rawHandler, customizedCode, options) {
|
6765 | let routeHandler = this._container.create("RouteHandler", {
|
6766 | schema: this.schema,
|
6767 | verb,
|
6768 | rawHandler,
|
6769 | customizedCode,
|
6770 | options,
|
6771 | path,
|
6772 | serializerOrRegistry: this.serializerOrRegistry
|
6773 | });
|
6774 |
|
6775 | let fullPath = this._getFullPath(path);
|
6776 |
|
6777 | let timing = options.timing !== undefined ? options.timing : () => this.timing;
|
6778 |
|
6779 | if (this.pretender) {
|
6780 | return this.pretender[verb](fullPath, request => {
|
6781 | return routeHandler.handle(request).then(mirageResponse => {
|
6782 | let [code, headers, response] = mirageResponse;
|
6783 | return [code, headers, this._serialize(response)];
|
6784 | });
|
6785 | }, timing);
|
6786 | }
|
6787 | }
|
6788 | |
6789 |
|
6790 |
|
6791 |
|
6792 |
|
6793 |
|
6794 |
|
6795 | _hasModulesOfType(modules, type) {
|
6796 | let modulesOfType = modules[type];
|
6797 | return modulesOfType ? Object.keys(modulesOfType).length > 0 : false;
|
6798 | }
|
6799 | |
6800 |
|
6801 |
|
6802 |
|
6803 |
|
6804 |
|
6805 |
|
6806 |
|
6807 |
|
6808 | _getFullPath(path) {
|
6809 | path = path[0] === "/" ? path.slice(1) : path;
|
6810 | let fullPath = "";
|
6811 | let urlPrefix = this.urlPrefix ? this.urlPrefix.trim() : "";
|
6812 | let namespace = "";
|
6813 |
|
6814 | if (this.urlPrefix && this.namespace) {
|
6815 | if (this.namespace[0] === "/" && this.namespace[this.namespace.length - 1] === "/") {
|
6816 | namespace = this.namespace.substring(0, this.namespace.length - 1).substring(1);
|
6817 | }
|
6818 |
|
6819 | if (this.namespace[0] === "/" && this.namespace[this.namespace.length - 1] !== "/") {
|
6820 | namespace = this.namespace.substring(1);
|
6821 | }
|
6822 |
|
6823 | if (this.namespace[0] !== "/" && this.namespace[this.namespace.length - 1] === "/") {
|
6824 | namespace = this.namespace.substring(0, this.namespace.length - 1);
|
6825 | }
|
6826 |
|
6827 | if (this.namespace[0] !== "/" && this.namespace[this.namespace.length - 1] !== "/") {
|
6828 | namespace = this.namespace;
|
6829 | }
|
6830 | }
|
6831 |
|
6832 |
|
6833 | if (this.namespace && !this.urlPrefix) {
|
6834 | if (this.namespace[0] === "/" && this.namespace[this.namespace.length - 1] === "/") {
|
6835 | namespace = this.namespace.substring(0, this.namespace.length - 1);
|
6836 | }
|
6837 |
|
6838 | if (this.namespace[0] === "/" && this.namespace[this.namespace.length - 1] !== "/") {
|
6839 | namespace = this.namespace;
|
6840 | }
|
6841 |
|
6842 | if (this.namespace[0] !== "/" && this.namespace[this.namespace.length - 1] === "/") {
|
6843 | let namespaceSub = this.namespace.substring(0, this.namespace.length - 1);
|
6844 | namespace = `/${namespaceSub}`;
|
6845 | }
|
6846 |
|
6847 | if (this.namespace[0] !== "/" && this.namespace[this.namespace.length - 1] !== "/") {
|
6848 | namespace = `/${this.namespace}`;
|
6849 | }
|
6850 | }
|
6851 |
|
6852 |
|
6853 | if (!this.namespace) {
|
6854 | namespace = "";
|
6855 | }
|
6856 |
|
6857 |
|
6858 | if (/^https?:\/\//.test(path)) {
|
6859 | fullPath += path;
|
6860 | } else {
|
6861 |
|
6862 | if (urlPrefix.length) {
|
6863 | fullPath += urlPrefix[urlPrefix.length - 1] === "/" ? urlPrefix : `${urlPrefix}/`;
|
6864 | }
|
6865 |
|
6866 |
|
6867 | fullPath += namespace;
|
6868 |
|
6869 | if (fullPath[fullPath.length - 1] !== "/") {
|
6870 | fullPath += "/";
|
6871 | }
|
6872 |
|
6873 |
|
6874 | fullPath += path;
|
6875 |
|
6876 |
|
6877 | if (!/^https?:\/\//.test(fullPath)) {
|
6878 | fullPath = `/${fullPath}`;
|
6879 | fullPath = fullPath.replace(/\/+/g, "/");
|
6880 | }
|
6881 | }
|
6882 |
|
6883 | return fullPath;
|
6884 | }
|
6885 | |
6886 |
|
6887 |
|
6888 |
|
6889 |
|
6890 |
|
6891 |
|
6892 | _configureDefaultPassthroughs() {
|
6893 | defaultPassthroughs.forEach(passthroughUrl => {
|
6894 | this.passthrough(passthroughUrl);
|
6895 | });
|
6896 | }
|
6897 | |
6898 |
|
6899 |
|
6900 |
|
6901 |
|
6902 |
|
6903 |
|
6904 | _typeIsPluralForModel(typeOrCollectionName) {
|
6905 | if (typeof isPluralForModelCache[typeOrCollectionName] !== "boolean") {
|
6906 | let modelOrFactoryExists = this._modelOrFactoryExistsForTypeOrCollectionName(typeOrCollectionName);
|
6907 |
|
6908 | let isPlural = typeOrCollectionName === this.inflector.pluralize(typeOrCollectionName);
|
6909 | let isUncountable = this.inflector.singularize(typeOrCollectionName) === this.inflector.pluralize(typeOrCollectionName);
|
6910 | const isPluralForModel = isPlural && !isUncountable && modelOrFactoryExists;
|
6911 | isPluralForModelCache[typeOrCollectionName] = isPluralForModel;
|
6912 | }
|
6913 |
|
6914 | return isPluralForModelCache[typeOrCollectionName];
|
6915 | }
|
6916 | |
6917 |
|
6918 |
|
6919 |
|
6920 |
|
6921 |
|
6922 |
|
6923 | _modelOrFactoryExistsForType(type) {
|
6924 | let modelExists = this.schema && this.schema.modelFor(camelize(type));
|
6925 | let dbCollectionExists = this.db[this.schema.toInternalCollectionName(type)];
|
6926 | return (modelExists || dbCollectionExists) && !this._typeIsPluralForModel(type);
|
6927 | }
|
6928 | |
6929 |
|
6930 |
|
6931 |
|
6932 |
|
6933 |
|
6934 |
|
6935 | _modelOrFactoryExistsForTypeOrCollectionName(typeOrCollectionName) {
|
6936 | let modelExists = this.schema && this.schema.modelFor(camelize(typeOrCollectionName));
|
6937 | let dbCollectionExists = this.db[this.schema.toInternalCollectionName(typeOrCollectionName)];
|
6938 | return modelExists || dbCollectionExists;
|
6939 | }
|
6940 | |
6941 |
|
6942 |
|
6943 |
|
6944 |
|
6945 |
|
6946 |
|
6947 | _validateTraits(traits, factory, type) {
|
6948 | traits.forEach(traitName => {
|
6949 | if (!factory.isTrait(traitName)) {
|
6950 | throw new Error(`'${traitName}' trait is not registered in '${type}' factory`);
|
6951 | }
|
6952 | });
|
6953 | }
|
6954 | |
6955 |
|
6956 |
|
6957 |
|
6958 |
|
6959 |
|
6960 |
|
6961 | _mergeExtensions(attrs, traits, overrides) {
|
6962 | let allExtensions = traits.map(traitName => {
|
6963 | return attrs[traitName].extension;
|
6964 | });
|
6965 | allExtensions.push(overrides || {});
|
6966 | return allExtensions.reduce((accum, extension) => {
|
6967 | return assign(accum, extension);
|
6968 | }, {});
|
6969 | }
|
6970 | |
6971 |
|
6972 |
|
6973 |
|
6974 |
|
6975 |
|
6976 |
|
6977 | _mapAssociationsFromAttributes(modelName, attributes, overrides = {}) {
|
6978 | Object.keys(attributes || {}).filter(attr => {
|
6979 | return isAssociation(attributes[attr]);
|
6980 | }).forEach(attr => {
|
6981 | let modelClass = this.schema.modelClassFor(modelName);
|
6982 | let association = modelClass.associationFor(attr);
|
6983 | assert(association && association instanceof BelongsTo, `You're using the \`association\` factory helper on the '${attr}' attribute of your ${modelName} factory, but that attribute is not a \`belongsTo\` association.`);
|
6984 | let isSelfReferentialBelongsTo = association && association instanceof BelongsTo && association.modelName === modelName;
|
6985 | assert(!isSelfReferentialBelongsTo, `You're using the association() helper on your ${modelName} factory for ${attr}, which is a belongsTo self-referential relationship. You can't do this as it will lead to infinite recursion. You can move the helper inside of a trait and use it selectively.`);
|
6986 | let isPolymorphic = association && association.opts && association.opts.polymorphic;
|
6987 | assert(!isPolymorphic, `You're using the association() helper on your ${modelName} factory for ${attr}, which is a polymorphic relationship. This is not currently supported.`);
|
6988 | let factoryAssociation = attributes[attr];
|
6989 | let foreignKey = `${camelize(attr)}Id`;
|
6990 |
|
6991 | if (!overrides[attr]) {
|
6992 | attributes[foreignKey] = this.create(association.modelName, ...factoryAssociation.traitsAndOverrides).id;
|
6993 | }
|
6994 |
|
6995 | delete attributes[attr];
|
6996 | });
|
6997 | }
|
6998 |
|
6999 | }
|
7000 |
|
7001 | var ActiveModelSerializer = Serializer.extend({
|
7002 | serializeIds: "always",
|
7003 | normalizeIds: true,
|
7004 |
|
7005 | keyForModel(type) {
|
7006 | return underscore(type);
|
7007 | },
|
7008 |
|
7009 | keyForAttribute(attr) {
|
7010 | return underscore(attr);
|
7011 | },
|
7012 |
|
7013 | keyForRelationship(type) {
|
7014 | return this._container.inflector.pluralize(underscore(type));
|
7015 | },
|
7016 |
|
7017 | keyForEmbeddedRelationship(attributeName) {
|
7018 | return underscore(attributeName);
|
7019 | },
|
7020 |
|
7021 | keyForRelationshipIds(type) {
|
7022 | return `${underscore(this._container.inflector.singularize(type))}_ids`;
|
7023 | },
|
7024 |
|
7025 | keyForForeignKey(relationshipName) {
|
7026 | return `${underscore(relationshipName)}_id`;
|
7027 | },
|
7028 |
|
7029 | keyForPolymorphicForeignKeyId(relationshipName) {
|
7030 | return `${underscore(relationshipName)}_id`;
|
7031 | },
|
7032 |
|
7033 | keyForPolymorphicForeignKeyType(relationshipName) {
|
7034 | return `${underscore(relationshipName)}_type`;
|
7035 | },
|
7036 |
|
7037 | normalize(payload) {
|
7038 | let type = Object.keys(payload)[0];
|
7039 | let attrs = payload[type];
|
7040 | let modelName = camelize(type);
|
7041 | let modelClass = this.schema.modelClassFor(modelName);
|
7042 | let {
|
7043 | belongsToAssociations,
|
7044 | hasManyAssociations
|
7045 | } = modelClass;
|
7046 | let belongsToKeys = Object.keys(belongsToAssociations);
|
7047 | let hasManyKeys = Object.keys(hasManyAssociations);
|
7048 | let jsonApiPayload = {
|
7049 | data: {
|
7050 | type: this._container.inflector.pluralize(type),
|
7051 | attributes: {}
|
7052 | }
|
7053 | };
|
7054 |
|
7055 | if (attrs.id) {
|
7056 | jsonApiPayload.data.id = attrs.id;
|
7057 | }
|
7058 |
|
7059 | let relationships = {};
|
7060 | Object.keys(attrs).forEach(key => {
|
7061 | if (key !== "id") {
|
7062 | if (this.normalizeIds) {
|
7063 | if (belongsToKeys.includes(key)) {
|
7064 | let association = belongsToAssociations[key];
|
7065 | let associationModel = association.modelName;
|
7066 | relationships[dasherize(key)] = {
|
7067 | data: {
|
7068 | type: associationModel,
|
7069 | id: attrs[key]
|
7070 | }
|
7071 | };
|
7072 | } else if (hasManyKeys.includes(key)) {
|
7073 | let association = hasManyAssociations[key];
|
7074 | let associationModel = association.modelName;
|
7075 | let data = attrs[key].map(id => {
|
7076 | return {
|
7077 | type: associationModel,
|
7078 | id
|
7079 | };
|
7080 | });
|
7081 | relationships[dasherize(key)] = {
|
7082 | data
|
7083 | };
|
7084 | } else {
|
7085 | jsonApiPayload.data.attributes[dasherize(key)] = attrs[key];
|
7086 | }
|
7087 | } else {
|
7088 | jsonApiPayload.data.attributes[dasherize(key)] = attrs[key];
|
7089 | }
|
7090 | }
|
7091 | });
|
7092 |
|
7093 | if (Object.keys(relationships).length) {
|
7094 | jsonApiPayload.data.relationships = relationships;
|
7095 | }
|
7096 |
|
7097 | return jsonApiPayload;
|
7098 | },
|
7099 |
|
7100 | getCoalescedIds(request) {
|
7101 | return request.queryParams && request.queryParams.ids;
|
7102 | }
|
7103 |
|
7104 | });
|
7105 |
|
7106 | var restSerializer = ActiveModelSerializer.extend({
|
7107 | serializeIds: "always",
|
7108 |
|
7109 | keyForModel(type) {
|
7110 | return camelize(type);
|
7111 | },
|
7112 |
|
7113 | keyForAttribute(attr) {
|
7114 | return camelize(attr);
|
7115 | },
|
7116 |
|
7117 | keyForRelationship(type) {
|
7118 | return camelize(this._container.inflector.pluralize(type));
|
7119 | },
|
7120 |
|
7121 | keyForEmbeddedRelationship(attributeName) {
|
7122 | return camelize(attributeName);
|
7123 | },
|
7124 |
|
7125 | keyForRelationshipIds(type) {
|
7126 | return camelize(this._container.inflector.pluralize(type));
|
7127 | },
|
7128 |
|
7129 | keyForForeignKey(relationshipName) {
|
7130 | return camelize(this._container.inflector.singularize(relationshipName));
|
7131 | },
|
7132 |
|
7133 | getCoalescedIds(request) {
|
7134 | return request.queryParams && request.queryParams.ids;
|
7135 | }
|
7136 |
|
7137 | });
|
7138 |
|
7139 |
|
7140 |
|
7141 |
|
7142 |
|
7143 |
|
7144 | function uuid () {
|
7145 | return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
7146 | let r = Math.random() * 16 | 0;
|
7147 | let v = c === "x" ? r : r & 0x3 | 0x8;
|
7148 | return v.toString(16);
|
7149 | });
|
7150 | }
|
7151 |
|
7152 |
|
7153 |
|
7154 |
|
7155 |
|
7156 | function hasMany(...args) {
|
7157 | return new HasMany(...args);
|
7158 | }
|
7159 |
|
7160 |
|
7161 |
|
7162 |
|
7163 |
|
7164 | function belongsTo(...args) {
|
7165 | return new BelongsTo(...args);
|
7166 | }
|
7167 | var index = {
|
7168 | Factory,
|
7169 | Response,
|
7170 | hasMany,
|
7171 | belongsTo
|
7172 | };
|
7173 |
|
7174 | exports.ActiveModelSerializer = ActiveModelSerializer;
|
7175 | exports.Collection = Collection;
|
7176 | exports.Factory = Factory;
|
7177 | exports.IdentityManager = IdentityManager;
|
7178 | exports.JSONAPISerializer = JSONAPISerializer;
|
7179 | exports.Model = Model;
|
7180 | exports.Response = Response;
|
7181 | exports.RestSerializer = restSerializer;
|
7182 | exports.Serializer = Serializer;
|
7183 | exports.Server = Server;
|
7184 | exports._Db = Db;
|
7185 | exports._DbCollection = DbCollection;
|
7186 | exports._RouteHandler = RouteHandler;
|
7187 | exports._SerializerRegistry = SerializerRegistry;
|
7188 | exports._assert = assert;
|
7189 | exports._ormAssociationsAssociation = Association;
|
7190 | exports._ormAssociationsBelongsTo = BelongsTo;
|
7191 | exports._ormAssociationsHasMany = HasMany;
|
7192 | exports._ormPolymorphicCollection = PolymorphicCollection;
|
7193 | exports._ormSchema = Schema;
|
7194 | exports._routeHandlersBase = BaseRouteHandler;
|
7195 | exports._routeHandlersFunction = FunctionRouteHandler;
|
7196 | exports._routeHandlersObject = ObjectRouteHandler;
|
7197 | exports._routeHandlersShorthandsBase = BaseShorthandRouteHandler;
|
7198 | exports._routeHandlersShorthandsDelete = DeleteShorthandRouteHandler;
|
7199 | exports._routeHandlersShorthandsGet = GetShorthandRouteHandler;
|
7200 | exports._routeHandlersShorthandsHead = HeadShorthandRouteHandler;
|
7201 | exports._routeHandlersShorthandsPost = PostShorthandRouteHandler;
|
7202 | exports._routeHandlersShorthandsPut = PutShorthandRouteHandler;
|
7203 | exports._utilsExtend = extend;
|
7204 | exports._utilsInflectorCamelize = camelize;
|
7205 | exports._utilsInflectorCapitalize = capitalize;
|
7206 | exports._utilsInflectorDasherize = dasherize;
|
7207 | exports._utilsInflectorUnderscore = underscore;
|
7208 | exports._utilsIsAssociation = isAssociation;
|
7209 | exports._utilsReferenceSort = referenceSort;
|
7210 | exports._utilsUuid = uuid;
|
7211 | exports.association = association;
|
7212 | exports.belongsTo = belongsTo;
|
7213 | exports.default = index;
|
7214 | exports.defaultPassthroughs = defaultPassthroughs;
|
7215 | exports.hasMany = hasMany;
|
7216 | exports.trait = trait;
|
7217 |
|