Skip to content

Commit

Permalink
Clone all objects passing in-to and out-of the in memory store
Browse files Browse the repository at this point in the history
  • Loading branch information
theninj4 committed Jun 8, 2016
1 parent efa3d62 commit 3232ec9
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions lib/MemoryHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MemoryStore.prototype.search = function(request, callback) {
if (request.params.page) {
results = results.slice(request.params.page.offset, request.params.page.offset + request.params.page.limit);
}
return callback(null, this._clone(results), resultCount);
return callback(null, MemoryStore._clone(results), resultCount);
};

/**
Expand All @@ -59,18 +59,16 @@ MemoryStore.prototype.find = function(request, callback) {
}

// Return the requested resource
return callback(null, this._clone(theResource));
return callback(null, MemoryStore._clone(theResource));
};

/**
Create (store) a new resource given a resource type and an object.
*/
MemoryStore.prototype.create = function(request, newResource, callback) {
// Check to see if the ID already exists
var matches = resources[request.params.type].filter(function(resource) {
return resource.id === newResource.id;
}).length;
if (matches > 0) {
var index = MemoryStore._indexOf(resources[request.params.type], newResource);
if (index !== -1) {
return callback({
status: "403",
code: "EFORBIDDEN",
Expand All @@ -81,7 +79,7 @@ MemoryStore.prototype.create = function(request, newResource, callback) {
// Push the newResource into our in-memory store.
resources[request.params.type].push(newResource);
// Return the newly created resource
return callback(null, this._clone(newResource));
return callback(null, MemoryStore._clone(newResource));
};

/**
Expand All @@ -93,13 +91,8 @@ MemoryStore.prototype.delete = function(request, callback) {
if (err) return callback(err);

// Remove the resource from the in-memory store.
resources[request.params.type].some(function(resource, index) {
var match = resource.id === theResource.id;
if (match) {
resources[request.params.type].splice(index, 1);
}
return match;
});
var index = MemoryStore._indexOf(resources[request.params.type], theResource);
resources[request.params.type].splice(index, 1);

// Return with no error
return callback();
Expand All @@ -120,16 +113,11 @@ MemoryStore.prototype.update = function(request, partialResource, callback) {
theResource = _.assign(theResource, partialResource);

// Push the newly updated resource back into the in-memory store
resources[request.params.type].some(function(resource, index) {
var match = resource.id === theResource.id;
if (match) {
resources[request.params.type][index] = theResource;
}
return match;
});
var index = MemoryStore._indexOf(resources[request.params.type], theResource);
resources[request.params.type][index] = theResource;

// Return the newly updated resource
return callback(null, self._clone(theResource));
return callback(null, MemoryStore._clone(theResource));
});
};

Expand Down Expand Up @@ -158,6 +146,13 @@ MemoryStore.prototype._sortList = function(request, list) {
});
};

MemoryStore.prototype._clone = function(obj) {
MemoryStore._clone = function(obj) {
return JSON.parse(JSON.stringify(obj));
};

MemoryStore._indexOf = function(list, obj) {
for (var i in list) {
if (list[i].id === obj.id) return i;
}
return -1;
};

0 comments on commit 3232ec9

Please sign in to comment.