Skip to content

Commit efa3d62

Browse files
author
theninj4
committed
Clone all objects passing in-to and out-of the in memory store
1 parent 1595316 commit efa3d62

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

lib/MemoryHandler.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ MemoryStore.prototype.search = function(request, callback) {
3636
if (request.params.page) {
3737
results = results.slice(request.params.page.offset, request.params.page.offset + request.params.page.limit);
3838
}
39-
return callback(null, results, resultCount);
39+
return callback(null, this._clone(results), resultCount);
4040
};
4141

4242
/**
@@ -59,17 +59,29 @@ MemoryStore.prototype.find = function(request, callback) {
5959
}
6060

6161
// Return the requested resource
62-
return callback(null, theResource);
62+
return callback(null, this._clone(theResource));
6363
};
6464

6565
/**
66-
Create (store) a new resource give a resource type and an object.
66+
Create (store) a new resource given a resource type and an object.
6767
*/
6868
MemoryStore.prototype.create = function(request, newResource, callback) {
69+
// Check to see if the ID already exists
70+
var matches = resources[request.params.type].filter(function(resource) {
71+
return resource.id === newResource.id;
72+
}).length;
73+
if (matches > 0) {
74+
return callback({
75+
status: "403",
76+
code: "EFORBIDDEN",
77+
title: "Requested resource already exists",
78+
detail: "The requested resource already exists of type " + request.params.type + " with id " + request.params.id
79+
});
80+
}
6981
// Push the newResource into our in-memory store.
7082
resources[request.params.type].push(newResource);
7183
// Return the newly created resource
72-
return callback(null, newResource);
84+
return callback(null, this._clone(newResource));
7385
};
7486

7587
/**
@@ -80,9 +92,14 @@ MemoryStore.prototype.delete = function(request, callback) {
8092
this.find(request, function(err, theResource) {
8193
if (err) return callback(err);
8294

83-
// Remove the resource from the in-meory store.
84-
var resourceIndex = resources[request.params.type].indexOf(theResource);
85-
resources[request.params.type].splice(resourceIndex, 1);
95+
// Remove the resource from the in-memory store.
96+
resources[request.params.type].some(function(resource, index) {
97+
var match = resource.id === theResource.id;
98+
if (match) {
99+
resources[request.params.type].splice(index, 1);
100+
}
101+
return match;
102+
});
86103

87104
// Return with no error
88105
return callback();
@@ -94,18 +111,25 @@ MemoryStore.prototype.delete = function(request, callback) {
94111
partialResource contains a subset of changes that need to be merged over the original.
95112
*/
96113
MemoryStore.prototype.update = function(request, partialResource, callback) {
114+
var self = this;
97115
// Find the requested resource
98-
this.find(request, function(err, theResource) {
116+
self.find(request, function(err, theResource) {
99117
if (err) return callback(err);
100118

101119
// Merge the partialResource over the original
102120
theResource = _.assign(theResource, partialResource);
103121

104122
// Push the newly updated resource back into the in-memory store
105-
resources[request.params.type][request.params.id] = theResource;
123+
resources[request.params.type].some(function(resource, index) {
124+
var match = resource.id === theResource.id;
125+
if (match) {
126+
resources[request.params.type][index] = theResource;
127+
}
128+
return match;
129+
});
106130

107131
// Return the newly updated resource
108-
return callback(null, theResource);
132+
return callback(null, self._clone(theResource));
109133
});
110134
};
111135

@@ -133,3 +157,7 @@ MemoryStore.prototype._sortList = function(request, list) {
133157
}
134158
});
135159
};
160+
161+
MemoryStore.prototype._clone = function(obj) {
162+
return JSON.parse(JSON.stringify(obj));
163+
};

0 commit comments

Comments
 (0)