You can retrieve a Mongoose model like so:
let User = mongoose.model('User');
I am looking to do get an associative array of these models. Is there some clever way of getting a list of models using object destructuring? Something like:
const {User, Employees, Managers} = mongoose.model('x');
My current solution is to do this:
/project
/models
index.js
where index.js looks like:
module.exports = {
User: require('./user'),
Employee: require('./employee'),
Manager: require('./manager'),
};
Where the user.js, employee.js and manager.js files just look like:
let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let userSchema = new Schema({...});
module.exports = mongoose.model('User', userSchema, 'users');
Then I can do this:
const {User, Employees, Managers} = require('./models');
But I am looking for a better solution that requires no manual work if possible.
via Alexander Mills
No comments:
Post a Comment