Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

node.js - mongoose schema creation

I've just started with mongoose. I have a creation script with mongoose that creates the schemas and db with sample data.

Now I write the actual application. Do I need to create the schema object each time my application runs, or is it already available somehow?

In other words do I need to run this code in every app that uses mongoose to access the db or just the first time:

var Comments = new Schema({
    title     : String
  , body      : String
  , date      : Date
});

How would the answer change if I have setters/validations/etc?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One defines Schema so application understands how to map data from the MongoDB into JavaScript objects. Schema is a part of application. It has nothing to do with database. It only maps database into JavaScript objects. So yes - if you want to have nice mapping you need to run this code in every application that needs it. It also applies to getters/setters/validations/etc.

Note however that doing this:

var mongoose = require('mongoose');
var Schema = mongoose.Schema; // <-- EDIT: missing in the original post
var Comments = new Schema({
    title     : String
  , body      : String
  , date      : Date
});
mongoose.model("Comments", Comments);

will register Schema globaly. This means that if the application you are running is using some exterior module, then in this module you can simply use

var mongoose = require('mongoose');
var Comments = mongoose.model("Comments");
Comments.find(function(err, comments) {
    // some code here
});

(note that you actually need to register the Schema before using this code, otherwise an exception will be thrown).

However all of this works only inside one node session, so if you are running another node app which needs the access to the Schema, then you need to call the registration code. So it is a good idea to define all Schemas in separate files, for example comments.js may look like this

var mongoose = require('mongoose');
var Schema = mongoose.Schema; // <-- EDIT: missing in the original post

module.exports = function() {
    var Comments = new Schema({
        title     : String
      , body      : String
      , date      : Date
    });
    mongoose.model("Comments", Comments);
};

then create file models.js which may look like this

var models = ['comments.js', 'someothermodel.js', ...];

exports.initialize = function() {
    var l = models.length;
    for (var i = 0; i < l; i++) {
        require(models[i])();
    }
};

Now calling require('models.js').initialize(); will initialize all of your Schemas for a given node session.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...