mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 20:52:08 +08:00
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
This commit is contained in:
@ -149,12 +149,16 @@ module.exports = function (source) {
|
|||||||
.get(utils.toNative(req.params.id))
|
.get(utils.toNative(req.params.id))
|
||||||
|
|
||||||
if (resource) {
|
if (resource) {
|
||||||
|
// Clone resource to avoid making changes to the underlying object
|
||||||
|
resource = _.cloneDeep(resource)
|
||||||
// Always use an array
|
// Always use an array
|
||||||
_embed = _.isArray(_embed) ? _embed : [_embed]
|
_embed = _.isArray(_embed) ? _embed : [_embed]
|
||||||
|
|
||||||
// Embed other resources based on resource id
|
// Embed other resources based on resource id
|
||||||
_embed.forEach(function (otherResource) {
|
_embed.forEach(function (otherResource) {
|
||||||
if (otherResource && otherResource.trim().length > 0) {
|
if (otherResource && otherResource.trim().length > 0) {
|
||||||
|
// Skip non-existent collections
|
||||||
|
if (!db.object[otherResource]) return;
|
||||||
var query = {}
|
var query = {}
|
||||||
query[req.params.resource + 'Id'] = req.params.id
|
query[req.params.resource + 'Id'] = req.params.id
|
||||||
resource[otherResource] = db(otherResource).where(query)
|
resource[otherResource] = db(otherResource).where(query)
|
||||||
|
Reference in New Issue
Block a user