-
Notifications
You must be signed in to change notification settings - Fork 114
Clone all objects passing in-to and out-of the in memory store #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, results, resultCount); | ||
| return callback(null, MemoryStore._clone(results), resultCount); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -59,17 +59,27 @@ MemoryStore.prototype.find = function(request, callback) { | |
| } | ||
|
|
||
| // Return the requested resource | ||
| return callback(null, theResource); | ||
| return callback(null, MemoryStore._clone(theResource)); | ||
| }; | ||
|
|
||
| /** | ||
| Create (store) a new resource give a resource type and an object. | ||
| 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 index = MemoryStore._indexOf(resources[request.params.type], newResource); | ||
| if (index !== -1) { | ||
| return callback({ | ||
| status: "403", | ||
| code: "EFORBIDDEN", | ||
| title: "Requested resource already exists", | ||
| detail: "The requested resource already exists of type " + request.params.type + " with id " + request.params.id | ||
| }); | ||
| } | ||
| // Push the newResource into our in-memory store. | ||
| resources[request.params.type].push(newResource); | ||
| // Return the newly created resource | ||
| return callback(null, newResource); | ||
| return callback(null, MemoryStore._clone(newResource)); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -80,9 +90,9 @@ MemoryStore.prototype.delete = function(request, callback) { | |
| this.find(request, function(err, theResource) { | ||
| if (err) return callback(err); | ||
|
|
||
| // Remove the resource from the in-meory store. | ||
| var resourceIndex = resources[request.params.type].indexOf(theResource); | ||
| resources[request.params.type].splice(resourceIndex, 1); | ||
| // Remove the resource from the in-memory store. | ||
| var index = MemoryStore._indexOf(resources[request.params.type], theResource); | ||
| resources[request.params.type].splice(index, 1); | ||
|
|
||
| // Return with no error | ||
| return callback(); | ||
|
|
@@ -102,10 +112,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][request.params.id] = theResource; | ||
| var index = MemoryStore._indexOf(resources[request.params.type], theResource); | ||
| resources[request.params.type][index] = theResource; | ||
|
|
||
| // Return the newly updated resource | ||
| return callback(null, theResource); | ||
| return callback(null, MemoryStore._clone(theResource)); | ||
| }); | ||
| }; | ||
|
|
||
|
|
@@ -133,3 +144,14 @@ MemoryStore.prototype._sortList = function(request, list) { | |
| } | ||
| }); | ||
| }; | ||
|
|
||
| MemoryStore._clone = function(obj) { | ||
| return JSON.parse(JSON.stringify(obj)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure we never have circular references?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This data is intended to be serialised at this point for storage. If anything does have a circular reference at this point, it's going to break for real when the memory handler gets replaced for a real storage mechanism?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't really need the |
||
| }; | ||
|
|
||
| MemoryStore._indexOf = function(list, obj) { | ||
| for (var i in list) { | ||
| if (list[i].id === obj.id) return i; | ||
| } | ||
| return -1; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for not using lodash's
cloneDeep?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd mean adding another dependency for something we really don't need.