node-rigorous
Version:
Rigorous Framework
35 lines (33 loc) • 891 B
JavaScript
;
var mongoose = require('mongoose');
module.exports = function (modelName, attributesSettings) {
// Schema
var collectionRules = {};
Object.keys(attributesSettings).forEach(function (key) {
collectionRules[key] = attributesSettings[key].schema;
});
var objectCollectionSchema = new mongoose.Schema(collectionRules, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
},
toObject: {
virtuals: true,
transform: function transform(doc, ret, options) {
ret.id = ret._id;
delete ret._id;
}
},
toJSON: {
virtuals: true,
transform: function transform(doc, ret, options) {
ret.id = ret._id;
delete ret._id;
delete ret.__v;
return ret;
}
},
collection: "".concat(modelName.toLowerCase(), "s")
});
return objectCollectionSchema;
};