Last active
May 28, 2021 11:52
-
-
Save Kinjalrk2k/d3c8f7683c9454e7d6b4e9cf15507cf8 to your computer and use it in GitHub Desktop.
Cleanup a Mongoose Model to a simple object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cleanupMongooseSchema = (schema) => { | |
let cleanSchema = {}; | |
for (const field in schema.paths) { | |
const { path, instance, options } = schema.paths[field]; | |
cleanSchema[path] = { type: instance }; | |
if (schema.paths[field].hasOwnProperty("schema")) { | |
console.log(schema.paths[field].schema); | |
const nestedSchema = schema.paths[field].schema; | |
const nestedCleanup = cleanupMongooseSchema(nestedSchema); | |
cleanSchema[path].schema = nestedCleanup; | |
} | |
const toDestrcture = ["ref", "enum", "default"]; | |
toDestrcture.forEach((des) => { | |
if (options.hasOwnProperty(des)) { | |
cleanSchema[path][des] = options[des]; | |
} | |
}); | |
} | |
return cleanSchema; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment