Skip to content

Commit

Permalink
Merge pull request #96 from orbitjs/transformation
Browse files Browse the repository at this point in the history
Transform Transformations
  • Loading branch information
dgeb committed Mar 2, 2015
2 parents 6b65454 + 250c6c0 commit 1a654cd
Show file tree
Hide file tree
Showing 25 changed files with 1,754 additions and 666 deletions.
55 changes: 32 additions & 23 deletions lib/orbit-common/cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Document from 'orbit/document';
import Evented from 'orbit/evented';
import Operation from 'orbit/operation';
import { Class, expose, isArray } from 'orbit/lib/objects';
import { OperationNotAllowed } from './lib/exceptions';

Expand Down Expand Up @@ -124,7 +125,15 @@ var Cache = Class.extend({
*/
transform: function(operation) {
var op = operation.op,
path = operation.path;
path = operation.path,
value = operation.value;

var normalizedOperation;
if (operation instanceof Operation) {
normalizedOperation = operation;
} else {
normalizedOperation = new Operation(operation);
}

path = this._doc.deserializePath(path);

Expand All @@ -137,13 +146,13 @@ var Cache = Class.extend({
}

if (this.trackRevLinks && (op === 'remove' || op === 'replace')) {
this._removeRevLinks(path);
this._removeRevLinks(path, normalizedOperation);
}

this._transform(operation, this.trackChanges);

if (this.trackRevLinks && (op === 'add' || op === 'replace')) {
this._addRevLinks(path, operation.value);
this._addRevLinks(path, value, normalizedOperation);
}
},

Expand All @@ -167,7 +176,7 @@ var Cache = Class.extend({
}
},

_addRevLinks: function(path, value) {
_addRevLinks: function(path, value, parentOperation) {
// console.log('_addRevLinks', path, value);
if (value) {
var _this = this,
Expand All @@ -185,11 +194,11 @@ var Cache = Class.extend({

if (linkSchema.type === 'hasMany') {
Object.keys(linkValue).forEach(function(v) {
_this._addRevLink(linkSchema, type, id, link, v);
_this._addRevLink(linkSchema, type, id, link, v, parentOperation);
});

} else {
_this._addRevLink(linkSchema, type, id, link, linkValue);
_this._addRevLink(linkSchema, type, id, link, linkValue, parentOperation);
}
});
}
Expand All @@ -205,12 +214,12 @@ var Cache = Class.extend({
linkValue = value;
}

this._addRevLink(linkSchema, type, id, link, linkValue);
this._addRevLink(linkSchema, type, id, link, linkValue, parentOperation);
}
}
},

_addRevLink: function(linkSchema, type, id, link, value) {
_addRevLink: function(linkSchema, type, id, link, value, parentOperation) {
// console.log('_addRevLink', linkSchema, type, id, link, value);

if (value && typeof value === 'string') {
Expand All @@ -225,19 +234,19 @@ var Cache = Class.extend({
if (!refs) {
refs = {};
refs[linkPath] = true;
this._transformRef('add', refsPath, refs);
this._transformRef('add', refsPath, refs, parentOperation);

} else {
refsPath.push(linkPath);
refs = this.retrieve(refsPath);
if (!refs) {
this._transformRef('add', refsPath, true);
this._transformRef('add', refsPath, true, parentOperation);
}
}
}
},

_removeRevLinks: function(path) {
_removeRevLinks: function(path, parentOperation) {
// console.log('_removeRevLinks', path);

var value = this.retrieve(path);
Expand All @@ -258,16 +267,16 @@ var Cache = Class.extend({
path = _this._doc.deserializePath(path);

if (path.length === 4) {
operation = {
operation = parentOperation.spawn({
op: 'replace',
path: path,
value: null
};
});
} else {
operation = {
operation = parentOperation.spawn({
op: 'remove',
path: path
};
});
}

try {
Expand All @@ -286,11 +295,11 @@ var Cache = Class.extend({

if (linkSchema.type === 'hasMany') {
Object.keys(linkValue).forEach(function(v) {
_this._removeRevLink(linkSchema, type, id, link, v);
_this._removeRevLink(linkSchema, type, id, link, v, parentOperation);
});

} else {
_this._removeRevLink(linkSchema, type, id, link, linkValue);
_this._removeRevLink(linkSchema, type, id, link, linkValue, parentOperation);
}
});
}
Expand All @@ -306,12 +315,12 @@ var Cache = Class.extend({
linkValue = value;
}

this._removeRevLink(linkSchema, type, id, link, linkValue);
this._removeRevLink(linkSchema, type, id, link, linkValue, parentOperation);
}
}
},

_removeRevLink: function(linkSchema, type, id, link, value) {
_removeRevLink: function(linkSchema, type, id, link, value, parentOperation) {
// console.log('_removeRevLink', linkSchema, type, id, link, value);

if (value && typeof value === 'string') {
Expand All @@ -322,15 +331,15 @@ var Cache = Class.extend({
linkPath = '/' + linkPath.join('/');

var revLinkPath = [linkSchema.model, value, '__rev', linkPath];
this._transformRef('remove', revLinkPath);
this._transformRef('remove', revLinkPath, null, parentOperation);
}
},

_transformRef: function(op, path, value) {
var operation = {
_transformRef: function(op, path, value, parentOperation) {
var operation = parentOperation.spawn({
op: op,
path: path
};
});
if (value) {
operation.value = value;
}
Expand Down
30 changes: 17 additions & 13 deletions lib/orbit-common/jsonapi-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ var JSONAPISource = Source.extend({

return this.ajax(this.resourceURL(type), 'POST', {data: json}).then(
function(raw) {
_this.deserialize(type, id, raw);
_this.deserialize(type, id, raw, operation);
}
);
},
Expand All @@ -186,7 +186,7 @@ var JSONAPISource = Source.extend({
return this.ajax(this.resourceURL(type), 'PATCH', {data: [ remoteOp ]}).then(
function(raw) {
if (raw && isArray(raw)) {
_this.deserialize(type, id, raw[0]);
_this.deserialize(type, id, raw[0], operation);
} else {
_this._transformCache(operation);
}
Expand Down Expand Up @@ -214,7 +214,7 @@ var JSONAPISource = Source.extend({
function(raw) {
// TODO - better 204 (no content) checking
if (raw && Object.keys(raw).length > 0) {
_this.deserialize(type, id, raw);
_this.deserialize(type, id, raw, operation);
} else {
_this._transformCache(operation);
}
Expand All @@ -237,7 +237,7 @@ var JSONAPISource = Source.extend({
return this.ajax(this.resourceURL(type, id), 'PATCH', {data: [ remoteOp ]}).then(
function(raw) {
if (raw && isArray(raw)) {
_this.deserialize(type, id, raw[0]);
_this.deserialize(type, id, raw[0], operation);
} else {
_this._transformCache(operation);
}
Expand Down Expand Up @@ -518,19 +518,23 @@ var JSONAPISource = Source.extend({
);
},

_addRecordsToCache: function(type, records) {
_addRecordsToCache: function(type, records, parentOperation) {
var _this = this;
records.forEach(function(record) {
_this._addRecordToCache(type, record);
_this._addRecordToCache(type, record, parentOperation);
});
},

_addRecordToCache: function(type, record) {
this._transformCache({
_addRecordToCache: function(type, record, parentOperation) {
var operation = {
op: 'add',
path: [type, this.getId(type, record)],
value: record
});
};

if (parentOperation) operation = parentOperation.spawn(operation);

this._transformCache(operation);
},

_findOne: function(type, id) {
Expand Down Expand Up @@ -714,21 +718,21 @@ var JSONAPISource = Source.extend({
return url;
},

deserialize: function(type, id, data) {
deserialize: function(type, id, data, parentOperation) {
var deserialized = this.serializer.deserialize(type, id, data);
var primaryRecords = deserialized[type];

if (this._cache) {
if (isArray(primaryRecords)) {
this._addRecordsToCache(type, primaryRecords);
this._addRecordsToCache(type, primaryRecords, parentOperation);
} else {
this._addRecordToCache(type, primaryRecords);
this._addRecordToCache(type, primaryRecords, parentOperation);
}

if (deserialized.linked) {
Object.keys(deserialized.linked).forEach(function(relType) {
var relRecords = deserialized.linked[relType];
this._addRecordsToCache(relType, relRecords);
this._addRecordsToCache(relType, relRecords, parentOperation);
}, this);
}
}
Expand Down
Loading

0 comments on commit 1a654cd

Please sign in to comment.