forked from 418sec/js-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelation.js
229 lines (179 loc) · 5.99 KB
/
Relation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import utils from './utils'
// TODO: remove this when the rest of the project is cleaned
export const belongsToType = 'belongsTo'
export const hasManyType = 'hasMany'
export const hasOneType = 'hasOne'
const DOMAIN = 'Relation'
export function Relation (relatedMapper, options = {}) {
utils.classCallCheck(this, Relation)
options.type = this.constructor.TYPE_NAME
this.validateOptions(relatedMapper, options)
if (typeof relatedMapper === 'object') {
Object.defineProperty(this, 'relatedMapper', { value: relatedMapper })
}
Object.defineProperty(this, 'inverse', { writable: true })
utils.fillIn(this, options)
}
Relation.extend = utils.extend
utils.addHiddenPropsToTarget(Relation.prototype, {
get canAutoAddLinks () {
return this.add === undefined || !!this.add
},
get relatedCollection () {
return this.mapper.datastore.getCollection(this.relation)
},
validateOptions (related, opts) {
const DOMAIN_ERR = `new ${DOMAIN}`
const localField = opts.localField
if (!localField) {
throw utils.err(DOMAIN_ERR, 'opts.localField')(400, 'string', localField)
}
const foreignKey = opts.foreignKey = opts.foreignKey || opts.localKey
if (!foreignKey && (opts.type === belongsToType || opts.type === hasOneType)) {
throw utils.err(DOMAIN_ERR, 'opts.foreignKey')(400, 'string', foreignKey)
}
if (utils.isString(related)) {
opts.relation = related
if (!utils.isFunction(opts.getRelation)) {
throw utils.err(DOMAIN_ERR, 'opts.getRelation')(400, 'function', opts.getRelation)
}
} else if (related) {
opts.relation = related.name
} else {
throw utils.err(DOMAIN_ERR, 'related')(400, 'Mapper or string', related)
}
},
assignTo (mapper) {
this.name = mapper.name
Object.defineProperty(this, 'mapper', { value: mapper })
mapper.relationList || Object.defineProperty(mapper, 'relationList', { value: [] })
mapper.relationFields || Object.defineProperty(mapper, 'relationFields', { value: [] })
mapper.relationList.push(this)
mapper.relationFields.push(this.localField)
},
canFindLinkFor () {
return !!(this.foreignKey || this.localKey)
},
getRelation () {
return this.relatedMapper
},
getForeignKey (record) {
return utils.get(record, this.mapper.idAttribute)
},
setForeignKey (record, relatedRecord) {
if (!record || !relatedRecord) {
return
}
this._setForeignKey(record, relatedRecord)
},
_setForeignKey (record, relatedRecords) {
const idAttribute = this.mapper.idAttribute
if (!utils.isArray(relatedRecords)) {
relatedRecords = [relatedRecords]
}
relatedRecords.forEach((relatedRecord) => {
utils.set(relatedRecord, this.foreignKey, utils.get(record, idAttribute))
})
},
getLocalField (record) {
return utils.get(record, this.localField)
},
setLocalField (record, relatedData) {
return utils.set(record, this.localField, relatedData)
},
getInverse (mapper) {
if (!this.inverse) {
this.findInverseRelation(mapper)
}
return this.inverse
},
findInverseRelation (mapper) {
this.getRelation().relationList.forEach((def) => {
if (def.getRelation() === mapper && this.isInversedTo(def) && this !== def) {
this.inverse = def
return true
}
})
},
isInversedTo (def) {
return !def.foreignKey || def.foreignKey === this.foreignKey
},
addLinkedRecords (records) {
const datastore = this.mapper.datastore
records.forEach((record) => {
let relatedData = this.getLocalField(record)
if (utils.isFunction(this.add)) {
relatedData = this.add(datastore, this, record)
} else if (relatedData) {
relatedData = this.linkRecord(record, relatedData)
}
const isEmptyLinks = !relatedData || (utils.isArray(relatedData) && !relatedData.length)
if (isEmptyLinks && this.canFindLinkFor(record)) {
relatedData = this.findExistingLinksFor(record)
}
if (relatedData) {
this.setLocalField(record, relatedData)
}
})
},
removeLinkedRecords (relatedMapper, records) {
const localField = this.localField
records.forEach((record) => {
utils.set(record, localField, undefined)
})
},
linkRecord (record, relatedRecord) {
const relatedId = utils.get(relatedRecord, this.mapper.idAttribute)
if (relatedId === undefined) {
const unsaved = this.relatedCollection.unsaved()
if (unsaved.indexOf(relatedRecord) === -1) {
if (this.canAutoAddLinks) {
relatedRecord = this.relatedCollection.add(relatedRecord)
}
}
} else {
if (relatedRecord !== this.relatedCollection.get(relatedId)) {
this.setForeignKey(record, relatedRecord)
if (this.canAutoAddLinks) {
relatedRecord = this.relatedCollection.add(relatedRecord)
}
}
}
return relatedRecord
},
// e.g. user hasMany post via "foreignKey", so find all posts of user
findExistingLinksByForeignKey (id) {
if (id === undefined || id === null) {
return
}
return this.relatedCollection.filter({
[this.foreignKey]: id
})
},
ensureLinkedDataHasProperType (props, opts) {
const relatedMapper = this.getRelation()
const relationData = this.getLocalField(props)
if (utils.isArray(relationData) && (!relationData.length || relatedMapper.is(relationData[0]))) {
return
}
if (relationData && !relatedMapper.is(relationData)) {
utils.set(props, this.localField, relatedMapper.createRecord(relationData, opts))
}
},
isRequiresParentId () {
return false
},
isRequiresChildId () {
return false
},
createChildRecord (props, relationData, opts) {
this.setForeignKey(props, relationData)
return this.createLinked(relationData, opts).then((result) => {
this.setLocalField(props, result)
})
},
createLinked (props, opts) {
const create = utils.isArray(props) ? 'createMany' : 'create'
return this.getRelation()[create](props, opts)
}
})