relation.js
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _lodash = require('lodash');
var _collection = require('./collection');
var _collection2 = _interopRequireDefault(_collection);
var _extend = require('../extend');
var _extend2 = _interopRequireDefault(_extend);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Used internally, the `Relation` helps in simplifying the relationship building,
// centralizing all logic dealing with type & option handling.
var RelationBase = function () {
function RelationBase(type, Target, options) {
(0, _classCallCheck3.default)(this, RelationBase);
if (Target != null) {
this.targetTableName = (0, _lodash.result)(Target.prototype, 'tableName');
this.targetIdAttribute = (0, _lodash.result)(Target.prototype, 'idAttribute');
}
(0, _lodash.assign)(this, { type: type, target: Target }, options);
}
// Creates a new relation instance, used by the `Eager` relation in
// dealing with `morphTo` cases, where the same relation is targeting multiple models.
(0, _createClass3.default)(RelationBase, [{
key: 'instance',
value: function instance(type, Target, options) {
return new this.constructor(type, Target, options);
}
// Creates a new, unparsed model, used internally in the eager fetch helper
// methods. (Parsing may mutate information necessary for eager pairing.)
}, {
key: 'createModel',
value: function createModel(data) {
if (this.target.prototype instanceof _collection2.default) {
return new this.target.prototype.model(data)._reset();
}
return new this.target(data)._reset();
}
// Eager pair the models.
}, {
key: 'eagerPair',
value: function eagerPair() {}
}]);
return RelationBase;
}(); // Base Relation
// ---------------
exports.default = RelationBase;
(0, _lodash.assign)(RelationBase, { extend: _extend2.default });