From b994fb559237eb8751a7ada0f4d8a3ae0afa2b7f Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 30 May 2015 19:45:35 +0200 Subject: [PATCH 1/2] Don't modify DB while using `_embed` - We skip embedding nonexistent collections to avoid auto-creating them by lowdb - We clone the object to avoid auto-saving embedded collection --- src/router.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/router.js b/src/router.js index 0f49075..fada390 100644 --- a/src/router.js +++ b/src/router.js @@ -149,12 +149,16 @@ module.exports = function (source) { .get(utils.toNative(req.params.id)) if (resource) { + // Clone resource to avoid making changes to the underlying object + resource = _.cloneDeep(resource) // Always use an array _embed = _.isArray(_embed) ? _embed : [_embed] // Embed other resources based on resource id _embed.forEach(function (otherResource) { if (otherResource && otherResource.trim().length > 0) { + // Skip non-existent collections + if (!db.object[otherResource]) return; var query = {} query[req.params.resource + 'Id'] = req.params.id resource[otherResource] = db(otherResource).where(query) From 465fbf72bb1c5f7abf5d05c64939efab2d0468d8 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 30 May 2015 19:54:01 +0200 Subject: [PATCH 2/2] Remove extra semicolon --- src/router.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/router.js b/src/router.js index fada390..2b94200 100644 --- a/src/router.js +++ b/src/router.js @@ -158,7 +158,7 @@ module.exports = function (source) { _embed.forEach(function (otherResource) { if (otherResource && otherResource.trim().length > 0) { // Skip non-existent collections - if (!db.object[otherResource]) return; + if (!db.object[otherResource]) return var query = {} query[req.params.resource + 'Id'] = req.params.id resource[otherResource] = db(otherResource).where(query)