I have a decent size project and I need to do a little rearrangement.
I am using the Mongoos as my ORM for the node I would like to keep all my Mongoose models in a folder named 'Model'. I have read that when I do this, In an index I can put JS file, which will bring in all the models and store them.
app.js:
... var mongoose = expected ('mongos'); Var Model = Required ('./model') (Mongoose); App.configure (function () {mongoose.connect (dbpath, function (err) {if (err) err;}); ...}); // My modes include models so I need access ... To get back all my models, whatever I have to do in index.js, the same thing is stuck to me
index.js (this is what I have tried, even that has not been closed)
Function model (Mongoose) {Requires different counters ('. / Counter') (mongos); Var requires user = ('./user') (Mongoose); Var Token = Required ('./toolk') (Mongoose); Var Team = Required ('./ Team') (Mongoose); Var role = requirement ('. / Role') (); Var layer = requires ('./layer') (Mongol, counter); Var Feature = Requirement ('./service') (Mongo, counter, async); } Module.exports = Model; I should also go from apes to Manong, since I had to join Mango? To wit. I can get it again in index.js, but I'm not sure that different files require the same module.
Edit: (Here are my models)
Forgetting the forgiveness, I add an 'accelerator' type of work to my model classes. To wit. I would like to present a public interface for each model.
user.js:
module. Exports = function (mongos) {// creates a new mouth schema object Var Schema = Mongoose Sema; / / User collection to user user = new schema (user name: {string: string, required: true}, password: {type: string, required: true},}, {versionKey: false}); // user schema var user = mongos Model creates models for ('user', userskema); Var getUserById = Function (id, callback) {User.findById (ID, callback); } Var getUserByUsername = function (user name, callback) {var query = {username: username}; User.findOne (query, callback); } Return {getUserById: getUserById, getUserById, getUserByU name: getUserByU name}}
node In js, the module is cached after first load. So you app No need to pass from mongos to JS. For example, in model / index.js: is required ('./ counters') requires export.User = ('./user') is required ( '. / Token'); ('./tm') is required; ('./file') is required; ('./layer') is required; ('./ utility') is required; // I like to use a loop to require all JS files in the folder. I in the model / user.js:
var Mongoose = is required ('magos'); Var userSchema = mongoose.Schema ({// ... set your schema here}); Var user = module.exports = mongoose.model ('user', user schema); Module.exports.getUserById = Function (ID, Callback) {User.findById (ID, Callback); } Module.exports.getUserByUsername = Function (user name, callback) {var query = {username: username}; User.findOne (query, callback); } in app.js:
var mongoose = is required ('magos'); Var Model = Required ('./model'); Mongoose.connect (dbpath, function (mistake) {if (mistake) throwing;}); // Yes! You can use model / user.js directly defined models in UserMedal = mongosse Model ('user'); // Or, you can use it like this: UserModel = models.User; App.get ('/', function (req, res) {var user = new UserModel (); user.name = 'bob'; user.save (); // UserModel.getUserByU name (); ...} ); For more information about module caching, node.js:
No comments:
Post a Comment