periodicjs.core.data
Version:
Core data is the ORM wrapping component of periodicjs.core.controller that provides database adapters for commonly used databases (ie. mongo, sql, postgres). Adapters provide a standard set of methods and options regardless of the type of database and so
20 lines (19 loc) • 740 B
JavaScript
;
const mongoose = require('mongoose');
/**
* Depopulates a populated mongoose document
* @param {Object} data The mongoose document that should be depopulated
* @return {Object} Returns a fully depopulated mongoose document
*/
module.exports = function depopulate (data) {
let depopulated = (Array.isArray(data)) ? [] : {};
for (let key in data) {
if (data[key] && typeof data[key] === 'object') {
if (data[key] instanceof Date) depopulated[key] = data[key];
else if (data[key]._id && mongoose.Types.ObjectId.isValid(data[key]._id.toString())) depopulated[key] = data[key]._id.toString();
else depopulated[key] = depopulate(data[key]);
}
else depopulated[key] = data[key];
}
return depopulated;
};