relation.js
1.27 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
// Base Relation
// ---------------
import { assign, result } from 'lodash';
import CollectionBase from './collection';
import extend from '../extend';
// Used internally, the `Relation` helps in simplifying the relationship building,
// centralizing all logic dealing with type & option handling.
export default class RelationBase {
constructor(type, Target, options) {
if (Target != null) {
this.targetTableName = result(Target.prototype, 'tableName');
this.targetIdAttribute = result(Target.prototype, 'idAttribute');
}
assign(this, { 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.
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.)
createModel(data) {
if (this.target.prototype instanceof CollectionBase) {
return new this.target.prototype.model(data)._reset();
}
return new this.target(data)._reset();
}
// Eager pair the models.
eagerPair() {}
}
assign(RelationBase, { extend });