8000 Filter related children by their own idAttribute, instead of that of … · js-data/js-data@8fc36c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8fc36c9

Browse files
mikeucrobinson42
authored andcommitted
Filter related children by their own idAttribute, instead of that of the parent (#499)
* Add failing test * Filter related collection based on its own idAttribute
1 parent 1cf5094 commit 8fc36c9

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Relation/HasMany.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const HasManyRelation = Relation.extend({
6262
findExistingLinksByLocalKeys (ids) {
6363
return this.relatedCollection.filter({
6464
where: {
65-
[this.mapper.idAttribute]: {
65+
[this.relatedCollection.mapper.idAttribute]: {
6666
'in': ids
6767
}
6868
}

test/integration/datastore.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,4 +653,54 @@ describe('DataStore integration tests', function () {
653653
assert.equal(nodes[4].parent, nodes[3])
654654
assert.deepEqual(nodes[4].children, [])
655655
})
656+
657+
it('should find hasMany foreignKeys with custom idAttribute', function () {
658+
const store = new JSData.DataStore()
659+
660+
store.defineMapper('foo', {
661+
relations: {
662+
hasMany: {
663+
bar: {
664+
localField: 'bars',
665+
localKeys: 'barIds'
666+
}
667+
}
668+
},
669+
idAttribute: 'fooId'
670+
})
671+
store.defineMapper('bar', {
672+
relations: {
673+
hasMany: {
674+
foo: {
675+
localField: 'foos',
676+
foreignKeys: 'barIds'
677+
}
678+
}
679+
}
680+
})
681+
682+
const bars = store.add('bar', [
683+
{
684+
id: 0,
685+
name: 'A'
686+
},
687+
{
688+
id: 1,
689+
name: 'B'
690+
},
691+
{
692+
id: 2,
693+
name: 'C'
694+
}
695+
])
696+
697+
const foo = store.add('foo', {
698+
fooId: 9,
699+
name: 'Z',
700+
barIds: [0, 2]
701+
})
702+
703+
assert.isOk(foo.bars)
704+
assert.deepEqual(foo.bars, [bars[0], bars[2]])
705+
})
656706
})

0 commit comments

Comments
 (0)
0