8000 fix: dont throw when an inverse relation doesnt exist (#480) · correct-js/js-data@d4493a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit d4493a5

Browse files
mattlewis92crobinson42
authored andcommitted
fix: dont throw when an inverse relation doesnt exist (js-data#480)
1 parent 2fb5bf0 commit d4493a5

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/DataStore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const props = {
178178
const inverseDef = def.getInverse(mapper)
179179
const currentParentId = currentParent ? utils.get(currentParent, def.getRelation().idAttribute) : undefined
180180

181-
if (currentParent && currentParentId !== undefined && currentParentId !== value) {
181+
if (inverseDef && currentParent && currentParentId !== undefined && currentParentId !== value) {
182182
if (inverseDef.type === hasOneType) {
183183
safeSetLink(currentParent, inverseDef.localField, undefined)
184184
} else if (inverseDef.type === hasManyType) {

test/unit/collection/add.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,27 @@ describe('Collection#add', function () {
298298
// console.log('\tinject 10,000 users time taken: ', new Date().getTime() - start, 'ms')
299299
// console.log('\tusers age 40-44', User.between(40, 45, { index: 'age' }).length)
300300
})
301+
it('should update a records relation when the inverse relation does not exist', function () {
302+
const store = new JSData.DataStore()
303+
store.defineMapper('user')
304+
store.defineMapper('post', {
305+
relations: {
306+
belongsTo: {
307+
user: {
308+
localKey: 'user_id',
309+
localField: 'user'
310+
}
311+
}
312+
}
313+
})
314+
const [user1, user2] = store.add('user', [{
315+
id: 1, name: 'John'
316+
}, {
317+
id: 2, name: 'Jane'
318+
}])
319+
const post = store.add('post', { id: 2, title: 'foo', user_id: 1 })
320+
assert.strictEqual(post.user, user1)
321+
store.add('post', { id: 2, title: 'foo', user_id: 2 })
322+
assert.strictEqual(post.user, user2)
323+
})
301324
})

0 commit comments

Comments
 (0)
0