10000 Fixed #376 · js-data/js-data@7259f1d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7259f1d

Browse files
committed
Fixed #376
1 parent a1f2178 commit 7259f1d

File tree

6 files changed

+68
-26
lines changed

6 files changed

+68
-26
lines changed

src/DataStore.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,13 +1316,13 @@ const props = {
13161316
return current
13171317
}
13181318
const inverseLocalField = def.getInverse(mapper).localField
1319+
// Update (unset) inverse relation
1320+
if (current) {
1321+
safeSetProp(current, foreignKey, undefined)
1322+
self.getCollection(relation).updateIndex(current, updateOpts)
1323+
safeSetLink(current, inverseLocalField, undefined)
1324+
}
13191325
if (record) {
1320-
// Update (unset) inverse relation
1321-
if (current) {
1322-
safeSetProp(current, foreignKey, undefined)
1323-
self.getCollection(relation).updateIndex(current, updateOpts)
1324-
safeSetLink(current, inverseLocalField, undefined)
1325-
}
13261326
const relatedId = utils.get(record, def.getRelation().idAttribute)
13271327
// Prefer store record
13281328
if (relatedId !== undefined) {

src/LinkedCollection.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ export default Collection.extend({
100100
}
101101

102102
if (mapper.relationList.length && record) {
103-
// Check the currently visited record for relations that need to be
104-
// inserted into their respective collections.
105103
mapper.relationList.forEach(function (def) {
106104
def.removeLinkedRecords(mapper, [record])
107105
})
@@ -116,8 +114,6 @@ export default Collection.extend({
116114
records.forEach(this._clearMeta, this)
117115

118116
if (mapper.relationList.length && records.length) {
119-
// Check the currently visited record for relations that need to be
120-
// inserted into their respective collections.
121117
mapper.relationList.forEach(function (def) {
122118
def.removeLinkedRecords(mapper, records)
123119
})

src/Relation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ utils.addHiddenPropsToTarget(Relation.prototype, {
156156
const localField = this.localField
157157
records.forEach((record) => {
158158
const relatedData = utils.get(record, localField)
159-
this.unlinkInverseRecords(relatedData)
159+
this.unlinkInverseRecords(record, relatedData)
160160
utils.set(record, localField, undefined)
161161
})
162162
},
@@ -165,7 +165,7 @@ utils.addHiddenPropsToTarget(Relation.prototype, {
165165
if (!record) {
166166
return
167167
}
168-
utils.set(record, this.getInverse(this.mapper).localField, undefined)
168+
utils.set(record, this.localField, undefined)
169169
},
170170

171171
linkRecord (record, relatedRecord) {

src/Relation/HasMany.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ export const HasManyRelation = Relation.extend({
1717
return !!(hasForeignKeys || (this.localKeys && utils.get(record, this.localKeys)))
1818
},
1919

20-
unlinkInverseRecords (records) {
21-
if (!records) {
22-
return
23-
}
24-
const localField = this.getInverse(this.mapper).localField
25-
records.forEach(function (record) {
26-
utils.set(record, localField, undefined)
27-
})
28-
},
29-
3020
linkRecord (record, relatedRecords) {
3121
const relatedCollection = this.relatedCollection
3222
const canAutoAddLinks = this.canAutoAddLinks

test/integration/datastore.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,60 @@ describe('DataStore integration tests', function () {
372372
assert.equal(store.unsaved('bar').length, 1)
373373
assert.equal(store.unsaved('foo').length, 1)
374374
})
375+
it('should correctly unlink inverse records', function () {
376+
const store = new JSData.DataStore()
377+
378+
store.defineMapper('A', {
379+
idAttribute: 'uid',
380+
properties: {
381+
uid: { type: 'string' }
382+
},
383+
relations: {
384+
hasMany: {
385+
B: {
386+
localField: 'b',
387+
foreignKey: 'a_uid'
388+
}
389+
}
390+
}
391+
})
392+
393+
store.defineMapper('B', {
394+
idAttribute: 'uid',
395+
properties: {
396+
uid: { type: 'string' },
397+
a_uid: { type: ['string', 'null'] }
398+
},
399+
relations: {
400+
belongsTo: {
401+
A: {
402+
localField: 'a',
403+
foreignKey: 'a_uid'
404+
}
405+
}
406+
}
407+
})
408+
409+
const aRecord = store.add('A', { uid: 'a1' })
410+
const bRecords = store.add('B', [{ uid: 'b1', a_uid: 'a1' }, { uid: 'b2', a_uid: 'a1' }])
411+
assert.objectsEqual(aRecord.b, bRecords)
412+
assert.strictEqual(bRecords[0].a, aRecord)
413+
assert.strictEqual(bRecords[1].a, aRecord)
414+
415+
const aRecord2 = store.add('A', { uid: 'a2' })
416+
const bRecords2 = store.add('B', [{ uid: 'b3', a_uid: 'a2' }, { uid: 'b4', a_uid: 'a2' }])
417+
assert.objectsEqual(aRecord2.b, bRecords2)
418+
assert.strictEqual(bRecords2[0].a, aRecord2)
419+
assert.strictEqual(bRecords2[1].a, aRecord2)
420+
421+
store.remove('B', 'b2')
422+
assert.objectsEqual(aRecord.b, [bRecords[0]])
423+
assert.strictEqual(bRecords[0].a, aRecord)
424+
assert.strictEqual(bRecords[1].a, undefined)
425+
426+
store.remove('A', 'a2')
427+
assert.objectsEqual(aRecord2.b, [])
428+
assert.equal(bRecords2[0].a, undefined)
429+
assert.equal(bRecords2[1].a, undefined)
430+
})
375431
})

test/unit/datastore/collection_methods.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('DataStore collection methods', function () {
2121
assert.equal(this.store.filter('comment', { userId: user.id }).length, 3)
2222
assert(!this.store.get('organization', user.organizationId))
2323
assert(removedUser.organization)
24-
assert.equal(this.store.filter('profile', { userId: user.id }).length, 1)
24+
assert.equal(this.store.getAll('profile').length, 2)
2525
})
2626
it('remove should remove multiple relations', function () {
2727
let user = this.store.add('user', this.data.user10)
@@ -41,7 +41,7 @@ describe('DataStore collection methods', function () {
4141
assert(!this.store.get('organization', user.organizationId))
4242
assert(removedUser.organization)
4343
assert.equal(this.store.filter('profile', { userId: user.id }).length, 0)
44-
assert(removedUser.profile)
44+
assert(!removedUser.profile)
4545
})
4646
it('removeAll should remove relations', function () {
4747
let user = this.store.add('user', this.data.user10)
@@ -59,7 +59,7 @@ describe('DataStore collection methods', function () {
5959
assert.equal(this.store.filter('comment', { userId: user.id }).length, 3)
6060
assert(!this.store.get('organization', user.organizationId))
6161
assert(removedUsers[0].organization)
62-
assert.equal(this.store.filter('profile', { userId: user.id }).length, 1)
62+
assert.equal(this.store.getAll('profile').length, 2)
6363
})
6464
it('removeAll should remove multiple relations', function () {
6565
let user = this.store.add('user', this.data.user10)
@@ -79,6 +79,6 @@ describe('DataStore collection methods', function () {
7979
assert(!this.store.get('organization', user.organizationId))
8080
assert(removedUsers[0].organization)
8181
assert.equal(this.store.filter('profile', { userId: user.id }).length, 0)
82-
assert(removedUsers[0].profile)
82+
assert(!removedUsers[0].profile)
8383
})
8484
})

0 commit comments

Comments
 (0)
0