Skip to content

Commit

Permalink
delete private nested objects
Browse files Browse the repository at this point in the history
  • Loading branch information
samkit5495 committed Dec 5, 2020
1 parent a8052f6 commit 5ba8628
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/models/plugins/toJSON.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
* - removes __v, createdAt, updatedAt, and any path that has private: true
* - replaces _id with id
*/


const deleteAtPath = (obj, path, index) => {
if (index === path.length - 1) {
delete obj[path[index]];
return;
}
deleteAtPath(obj[path[index]], path, index + 1);
};

const toJSON = (schema) => {
let transform;
if (schema.options.toJSON && schema.options.toJSON.transform) {
Expand All @@ -15,7 +25,7 @@ const toJSON = (schema) => {
transform(doc, ret, options) {
Object.keys(schema.paths).forEach((path) => {
if (schema.paths[path].options && schema.paths[path].options.private) {
delete ret[path];
deleteAtPath(ret, path.split('.'), 0);
}
});

Expand Down

0 comments on commit 5ba8628

Please sign in to comment.