Skip to content

Commit

Permalink
RemoveRecord when part of has many relationship should not clobber th…
Browse files Browse the repository at this point in the history
…e whole relationsip
  • Loading branch information
tchak committed Mar 17, 2019
1 parent ffb14e6 commit 311c6a7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/@orbit/indexeddb/src/cache.ts
Expand Up @@ -6,7 +6,7 @@ import Orbit, {
RecordIdentity,
RecordOperation,
TransformBuilderFunc,
recordsInclude
equalRecordIdentities
} from '@orbit/data';
import {
RecordRelationshipIdentity,
Expand Down Expand Up @@ -455,7 +455,9 @@ export default class IndexedDBCache extends AsyncRecordCache {
const cursor = event.target.result;
if (cursor) {
let result = this._fromInverseRelationshipForIDB(cursor.value);
results.push(result);
if (equalRecordIdentities(result.record, recordIdentity)) {
results.push(result);
}
cursor.continue();
} else {
resolve(results);
Expand Down
28 changes: 28 additions & 0 deletions packages/@orbit/indexeddb/test/source-test.ts
Expand Up @@ -252,6 +252,34 @@ module('IndexedDBSource', function(hooks) {
assert.equal(await getRecordFromIndexedDB(source.cache, planet), undefined, 'indexeddb does not contain record');
});

test('#push - removeRecord when part of has many relationship', async function(assert) {
assert.expect(2);

let moon1 = { type: 'moon', id: 'moon1' };
let moon2 = { type: 'moon', id: 'moon2' }
let planet: Record = {
type: 'planet',
id: 'jupiter',
attributes: {
name: 'Jupiter',
classification: 'gas giant'
},
relationships: {
moons: {
data: [
moon1,
moon2
]
}
}
};

await source.push(t => [t.addRecord(moon1),t.addRecord(moon2),t.addRecord(planet)]);
assert.deepEqual((await getRecordFromIndexedDB(source.cache, planet)).relationships.moons.data.length, 2, 'record has 2 moons');
await source.push(t => t.removeRecord(moon1));
assert.deepEqual((await getRecordFromIndexedDB(source.cache, planet)).relationships.moons.data.length, 1, 'record has 1 moon');
});

test('#push - removeRecord - when record does not exist', async function(assert) {
assert.expect(1);

Expand Down
28 changes: 28 additions & 0 deletions packages/@orbit/local-storage/test/source-test.ts
Expand Up @@ -216,6 +216,34 @@ module('LocalStorageSource', function(hooks) {
assert.deepEqual(getRecordFromLocalStorage(source, planet), null, 'local storage does not contain record');
});

test('#push - removeRecord when part of has many relationship', async function(assert) {
assert.expect(2);

let moon1 = { type: 'moon', id: 'moon1' };
let moon2 = { type: 'moon', id: 'moon2' }
let planet: Record = {
type: 'planet',
id: 'jupiter',
attributes: {
name: 'Jupiter',
classification: 'gas giant'
},
relationships: {
moons: {
data: [
moon1,
moon2
]
}
}
};

await source.push(t => [t.addRecord(moon1),t.addRecord(moon2),t.addRecord(planet)]);
assert.deepEqual((await getRecordFromLocalStorage(source, planet)).relationships.moons.data.length, 2, 'record has 2 moons');
await source.push(t => t.removeRecord(moon1));
assert.deepEqual((await getRecordFromLocalStorage(source, planet)).relationships.moons.data.length, 1, 'record has 1 moon');
});

test('#push - removeRecord - when record does not exist', async function(assert) {
assert.expect(1);

Expand Down
2 changes: 2 additions & 0 deletions packages/@orbit/record-cache/test/async-record-cache-test.ts
Expand Up @@ -201,6 +201,8 @@ module('AsyncRecordCache', function(hooks) {

assert.equal(await cache.getRecordAsync({ type: 'moon', id: 'm1' }), null, 'Io is GONE');

assert.deepEqual((await cache.getRelatedRecordsAsync({ type: 'planet', id: 'p1' }, 'moons')), [{ type: 'moon', id: 'm2' }], 'Io have been cleared from Jupiter');

await cache.patch(t => t.removeRecord(europa));

assert.equal(await cache.getRecordAsync({ type: 'moon', id: 'm2' }), null, 'Europa is GONE');
Expand Down

0 comments on commit 311c6a7

Please sign in to comment.