Skip to content

Commit

Permalink
Merge pull request #781 from orbitjs/backport-780
Browse files Browse the repository at this point in the history
Backport PR 780 to v0.16
  • Loading branch information
dgeb authored Sep 14, 2020
2 parents 7d669eb + 5cffe36 commit 839480d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/@orbit/data/src/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
Record,
RecordIdentity,
cloneRecordIdentity,
equalRecordIdentities
equalRecordIdentities,
mergeRecords
} from './record';
import { eq, deepGet, deepSet } from '@orbit/utils';

Expand Down Expand Up @@ -203,6 +204,15 @@ function mergeOperations(
superceded.op = 'updateRecord';
markOperationToDelete(superceding);
}
} else if (
(superceded.op === 'addRecord' ||
superceded.op === 'updateRecord' ||
(superceded as any).op === 'replaceRecord') &&
(superceding.op === 'updateRecord' ||
(superceding as any).op === 'replaceRecord')
) {
superceded.record = mergeRecords(superceded.record, superceding.record);
markOperationToDelete(superceding);
} else if (
(superceded.op === 'addRecord' ||
superceded.op === 'updateRecord' ||
Expand Down
84 changes: 84 additions & 0 deletions packages/@orbit/data/test/operation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,90 @@ module('Operation', function() {
);
});

test('can coalesce addRecord + updateRecord for the same record', function(assert) {
assert.deepEqual(
coalesceRecordOperations([
{
op: 'addRecord',
record: {
type: 'contact',
id: '1234'
}
},
{
op: 'updateRecord',
record: {
type: 'contact',
id: '1234',
attributes: { name: 'Joe' },
relationships: {
address: {
data: { type: 'address', id: 'abc' }
}
}
}
}
]),
[
{
op: 'addRecord',
record: {
type: 'contact',
id: '1234',
attributes: { name: 'Joe' },
relationships: {
address: {
data: { type: 'address', id: 'abc' }
}
}
}
}
]
);
});

test('can coalesce updateRecord + updateRecord for the same record', function(assert) {
assert.deepEqual(
coalesceRecordOperations([
{
op: 'updateRecord',
record: {
type: 'contact',
id: '1234',
attributes: { name: 'Joe' }
}
},
{
op: 'updateRecord',
record: {
type: 'contact',
id: '1234',
relationships: {
address: {
data: { type: 'address', id: 'abc' }
}
}
}
}
]),
[
{
op: 'updateRecord',
record: {
type: 'contact',
id: '1234',
attributes: { name: 'Joe' },
relationships: {
address: {
data: { type: 'address', id: 'abc' }
}
}
}
}
]
);
});

test('can coalesce replaceAttribute + replaceRelatedRecord with null', function(assert) {
assert.deepEqual(
coalesceRecordOperations([
Expand Down

0 comments on commit 839480d

Please sign in to comment.