Skip to content

Instantly share code, notes, and snippets.

@Kinjalrk2k
Last active May 28, 2021 11:52
Show Gist options
  • Save Kinjalrk2k/d3c8f7683c9454e7d6b4e9cf15507cf8 to your computer and use it in GitHub Desktop.
Save Kinjalrk2k/d3c8f7683c9454e7d6b4e9cf15507cf8 to your computer and use it in GitHub Desktop.
Cleanup a Mongoose Model to a simple object
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