I'm using mongoose to work with my MongoDB documents and have thee models:
module.exports = mongoose.model('Doc', mongoose.Schema({
type: 'doc'
}, collection: "doc");
module.exports = mongoose.model('Folder', mongoose.Schema({
type: 'folder'
}, collection: "doc");
module.exports = mongoose.model('Unit', mongoose.Schema({
type: 'unit'
}, collection: "doc");
At some point (on ajax request coming for example) I need to create model at several type:
app.post('/doc/create/:type', function (req, res, next) {
var type = req.params.type;
var data = req.body;
// how to create model based on incoming type here?
// var model = new Factory.create(type); ???
});
I need to know best practices to work with similar models and create instance from factory or something else.
Please share your experience.