Skip to content

Commit

Permalink
Merge pull request #676 from orbitjs/update-hints
Browse files Browse the repository at this point in the history
[@orbit/memory] Support hints for `update`
  • Loading branch information
dgeb authored Jul 10, 2019
2 parents 46e0f55 + e410d6e commit 3b106a8
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 13 deletions.
39 changes: 27 additions & 12 deletions packages/@orbit/memory/src/memory-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import Orbit, {
Transform,
TransformOrOperations,
coalesceRecordOperations,
buildTransform
buildTransform,
RecordIdentity
} from '@orbit/data';
import { Dict } from '@orbit/utils';
import MemoryCache, { MemoryCacheSettings } from './memory-cache';
Expand Down Expand Up @@ -82,7 +83,9 @@ export default class MemorySource extends Source
this.transformLog.on('truncate', this._logTruncated.bind(this));
this.transformLog.on('rollback', this._logRolledback.bind(this));

let cacheSettings: MemoryCacheSettings = settings.cacheSettings || {};
let cacheSettings: MemoryCacheSettings = settings.cacheSettings || {
schema
};
cacheSettings.schema = schema;
cacheSettings.keyMap = keyMap;
cacheSettings.queryBuilder =
Expand Down Expand Up @@ -126,9 +129,14 @@ export default class MemorySource extends Source
// Updatable interface implementation
/////////////////////////////////////////////////////////////////////////////

async _update(transform: Transform): Promise<any> {
async _update(transform: Transform, hints?: any): Promise<any> {
let results = this._applyTransform(transform);
return results.length === 1 ? results[0] : results;

if (hints && hints.data) {
return this._retrieveFromCache(hints.data);
} else {
return results.length === 1 ? results[0] : results;
}
}

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -137,13 +145,10 @@ export default class MemorySource extends Source

async _query(query: Query, hints?: any): Promise<any> {
if (hints && hints.data) {
if (Array.isArray(hints.data)) {
return this._cache.query(q => q.findRecords(hints.data));
} else if (hints.data) {
return this._cache.query(q => q.findRecord(hints.data));
}
return this._retrieveFromCache(hints.data);
} else {
return this._cache.query(query);
}
return this._cache.query(query);
}

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -160,8 +165,10 @@ export default class MemorySource extends Source
* @returns The forked source.
*/
fork(settings: MemorySourceSettings = {}): MemorySource {
settings.schema = this._schema;
settings.cacheSettings = settings.cacheSettings || {};
const schema = this._schema;

settings.schema = schema;
settings.cacheSettings = settings.cacheSettings || { schema };
settings.keyMap = this._keyMap;
settings.queryBuilder = this.queryBuilder;
settings.transformBuilder = this.transformBuilder;
Expand Down Expand Up @@ -289,6 +296,14 @@ export default class MemorySource extends Source
// Protected methods
/////////////////////////////////////////////////////////////////////////////

protected _retrieveFromCache(idOrIds: RecordIdentity | RecordIdentity[]) {
if (Array.isArray(idOrIds)) {
return this._cache.getRecordsSync(idOrIds);
} else {
return this._cache.getRecordSync(idOrIds);
}
}

protected _applyTransform(transform: Transform): PatchResultData[] {
const result = this.cache.patch(transform.operations as RecordOperation[]);
this._transforms[transform.id] = transform;
Expand Down
90 changes: 89 additions & 1 deletion packages/@orbit/memory/test/memory-source-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
SchemaSettings,
Source,
buildTransform,
RecordOperation
RecordOperation,
Transform
} from '@orbit/data';
import { clone } from '@orbit/utils';
import {
Expand Down Expand Up @@ -71,6 +72,7 @@ module('MemorySource', function(hooks) {
schema,
keyMap,
cacheSettings: {
schema,
processors: [
SyncCacheIntegrityProcessor,
SyncSchemaConsistencyProcessor
Expand Down Expand Up @@ -164,6 +166,92 @@ module('MemorySource', function(hooks) {
);
});

test('#update - accepts hints that can return a single record', async function(assert) {
assert.expect(2);

let jupiter = {
id: 'jupiter',
type: 'planet',
attributes: { name: 'Jupiter' }
};

let earth = {
id: 'earth',
type: 'planet',
attributes: { name: 'Earth' }
};

source.cache.patch(t => t.addRecord(earth));

source.on('beforeUpdate', (transform: Transform, hints: any) => {
if (transform.options.customizeResults) {
hints.data = earth;
}
});

let planet = await source.update(t => t.addRecord(jupiter), {
customizeResults: true
});

assert.equal(
source.cache.getRecordsSync('planet').length,
2,
'cache should contain two planets'
);

assert.deepEqual(planet, earth, 'added planet matches hinted record');
});

test('#update - accepts hints that can return a collection of records', async function(assert) {
assert.expect(2);

let jupiter = {
id: 'jupiter',
type: 'planet',
attributes: { name: 'Jupiter' }
};

let earth = {
id: 'earth',
type: 'planet',
attributes: { name: 'Earth' }
};

let uranus = {
id: 'uranus',
type: 'planet',
attributes: { name: 'Uranus' }
};

source.on('beforeUpdate', (transform: Transform, hints: any) => {
if (transform.options.customizeResults) {
hints.data = [
{ type: 'planet', id: 'uranus' },
{ type: 'planet', id: 'jupiter' }
];
}
});

let planets = await source.update(
t => [t.addRecord(jupiter), t.addRecord(earth), t.addRecord(uranus)],
{
customizeResults: true
}
);

assert.equal(
source.cache.getRecordsSync('planet').length,
3,
'cache should contain three planets'
);

assert.deepEqual(
planets,
[uranus, jupiter],
'planets match hinted records'
);
});

test("#query - queries the source's cache", async function(assert) {
assert.expect(2);

Expand Down

0 comments on commit 3b106a8

Please sign in to comment.