Skip to content

Commit

Permalink
Merge pull request #763 from orbitjs/backport-762
Browse files Browse the repository at this point in the history
Backport PR 762 to v0.16
  • Loading branch information
dgeb authored Jul 23, 2020
2 parents 2d21ec6 + 8c8d408 commit 201703f
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ export function getInverseRelationship(
relatedRecord?: RecordIdentity | null
): RecordRelationshipIdentity | null {
if (relatedRecord) {
const relationshipDef = schema.getRelationship(record.type, relationship);
const recordIdentity = cloneRecordIdentity(record);

if (relationshipDef.inverse) {
return {
record,
relationship,
relatedRecord
};
}
return {
record: recordIdentity,
relationship,
relatedRecord
};
}
return null;
}
Expand All @@ -35,19 +33,15 @@ export function getInverseRelationships(
relatedRecords?: RecordIdentity[]
): RecordRelationshipIdentity[] {
if (relatedRecords && relatedRecords.length > 0) {
const relationshipDef = schema.getRelationship(record.type, relationship);

if (relationshipDef.inverse) {
const recordIdentity = cloneRecordIdentity(record);
const recordIdentity = cloneRecordIdentity(record);

return relatedRecords.map(relatedRecord => {
return {
record: recordIdentity,
relationship,
relatedRecord
};
});
}
return relatedRecords.map(relatedRecord => {
return {
record: recordIdentity,
relationship,
relatedRecord
};
});
}
return [];
}
Expand Down
85 changes: 85 additions & 0 deletions packages/@orbit/record-cache/test/async-record-cache-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ module('AsyncRecordCache', function(hooks) {
planet: { type: 'hasOne', model: 'planet', inverse: 'moons' },
star: { type: 'hasOne', model: 'star', inverse: 'celestialObjects' }
}
},
binaryStar: {
attributes: {
name: { type: 'string' }
},
relationships: {
starOne: { kind: 'hasOne', type: 'star' }, // no inverse
starTwo: { kind: 'hasOne', type: 'star' } // no inverse
}
},
planetarySystem: {
attributes: {
name: { type: 'string' }
},
relationships: {
star: { kind: 'hasOne', type: ['star', 'binaryStar'] } // no inverse
}
}
}
});
Expand Down Expand Up @@ -1445,6 +1462,74 @@ module('AsyncRecordCache', function(hooks) {
);
});

test('#patch allows replaceRelatedRecord to be called on a relationship with no inverse and to be followed up by removing the replaced record', async function(assert) {
assert.expect(2);

const cache = new Cache({ schema, keyMap });

const star1 = {
id: 'star1',
type: 'star',
attributes: { name: 'sun1' }
};

const star2 = {
id: 'star2',
type: 'star',
attributes: { name: 'sun2' }
};

const home = {
id: 'home',
type: 'planetarySystem',
attributes: { name: 'Home' },
relationships: {
star: {
data: { id: 'star1', type: 'star' }
}
}
};

await cache.patch(t => [
t.addRecord(star1),
t.addRecord(star2),
t.addRecord(home)
]);

let latestHome = await cache.getRecordAsync({
id: 'home',
type: 'planetarySystem'
});
assert.deepEqual(
(latestHome.relationships.star.data as Record).id,
star1.id,
'The original related record is in place.'
);

await cache.patch(t => [
t.replaceRelatedRecord(
{
id: 'home',
type: 'planetarySystem'
},
'star',
star2
),
t.removeRecord(star1)
]);

latestHome = await cache.getRecordAsync({
id: 'home',
type: 'planetarySystem'
});

assert.deepEqual(
(latestHome.relationships.star.data as Record).id,
star2.id,
'The related record was replaced.'
);
});

test('#query can retrieve an individual record', async function(assert) {
const cache = new Cache({ schema, keyMap });

Expand Down
85 changes: 85 additions & 0 deletions packages/@orbit/record-cache/test/sync-record-cache-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ module('SyncRecordCache', function(hooks) {
planet: { type: 'hasOne', model: 'planet', inverse: 'moons' },
star: { type: 'hasOne', model: 'star', inverse: 'celestialObjects' }
}
},
binaryStar: {
attributes: {
name: { type: 'string' }
},
relationships: {
starOne: { kind: 'hasOne', type: 'star' }, // no inverse
starTwo: { kind: 'hasOne', type: 'star' } // no inverse
}
},
planetarySystem: {
attributes: {
name: { type: 'string' }
},
relationships: {
star: { kind: 'hasOne', type: ['star', 'binaryStar'] } // no inverse
}
}
}
});
Expand Down Expand Up @@ -1418,6 +1435,74 @@ module('SyncRecordCache', function(hooks) {
);
});

test('#patch allows replaceRelatedRecord to be called on a relationship with no inverse and to be followed up by removing the replaced record', function(assert) {
assert.expect(2);

const cache = new Cache({ schema, keyMap });

const star1 = {
id: 'star1',
type: 'star',
attributes: { name: 'sun1' }
};

const star2 = {
id: 'star2',
type: 'star',
attributes: { name: 'sun2' }
};

const home = {
id: 'home',
type: 'planetarySystem',
attributes: { name: 'Home' },
relationships: {
star: {
data: { id: 'star1', type: 'star' }
}
}
};

cache.patch(t => [
t.addRecord(star1),
t.addRecord(star2),
t.addRecord(home)
]);

let latestHome = cache.getRecordSync({
id: 'home',
type: 'planetarySystem'
});
assert.deepEqual(
(latestHome.relationships.star.data as Record).id,
star1.id,
'The original related record is in place.'
);

cache.patch(t => [
t.replaceRelatedRecord(
{
id: 'home',
type: 'planetarySystem'
},
'star',
star2
),
t.removeRecord(star1)
]);

latestHome = cache.getRecordSync({
id: 'home',
type: 'planetarySystem'
});

assert.deepEqual(
(latestHome.relationships.star.data as Record).id,
star2.id,
'The related record was replaced.'
);
});

test('#query can retrieve an individual record', function(assert) {
const cache = new Cache({ schema, keyMap });

Expand Down

0 comments on commit 201703f

Please sign in to comment.