UNPKG

node-rigorous

Version:
39 lines (29 loc) 1.09 kB
const mongoose = require('mongoose'); module.exports = (modelName, attributesSettings) => { // Schema const collectionRules = {}; Object.keys(attributesSettings).forEach((key) => { collectionRules[key] = attributesSettings[key].schema; }); const objectCollectionSchema = new mongoose.Schema( collectionRules, { timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, toObject: { virtuals: true, transform: function (doc, ret, options) { ret.id = ret._id; delete ret._id; }, }, toJSON: { virtuals: true, transform: function (doc, ret, options) { ret.id = ret._id; delete ret._id; delete ret.__v; return ret; }, }, collection: `${modelName.toLowerCase()}s`, }, ); return objectCollectionSchema; };