Grateful
Grateful

Reputation: 10205

mongoose.model( 'Foo', FooSchema ) vs require( './models/Foos' )

Once a schema is created, and placed within the models folder. A line of code is added as such...

// within '/models/Foos.js'
mongoose.model( 'Foo', FooSchema ) 

... but a reference to this schema is also added within node's app.js, as such...

var mongoose = require( 'mongoose' );
require( './models/Foos' )

... but what's the difference? Why do we have the need to do the latter (i.e. add schema file reference to global mongoose object within app.js), when we have already done something seemingly similar, within the schema file itself (i.e. FoosSchema.js), by mongoose.model( 'Foo', FooSchema ). What's going on?

Upvotes: 3

Views: 570

Answers (2)

Blakes Seven
Blakes Seven

Reputation: 50416

Well actually it can be just:

var Foo = mongoose.model( 'Foo' );

After the initial model declaration has been made, which will just retrieve the specified model with it's already attached schema, and of course that you already have a require for mongoose at least within the scope.

Equally you could just use it completely inline:

mongoose.model( 'Foo' ).find({},function(err,foos) {
    // do something
});

In the end the "two" approaches with either using require for your defined module and the mongoose.model accessor generally achieve the same thing. The main "difference" being that require is "path centric" to the structure of your application. Where as mongoose.model is inspecting mongoose's own "registry" of defined model data and retrieving the required "service" by name.

Therefore using mongoose.model may be considered "cleaner" in a lot of cases, but on the other hand require may be more along the logic of other module imports that others are used to in style.

This really comes down to coding patterns and style as to which best suits you. Though as "name based", the mongoose.model style does lend itself a bit more readily to "dynamic" code to recall the required model, as compared to importing from a "path" in your application.

Consider this:

async.each(['Foo','Bar'],function(modelName,callback) {
    mongoose.model(modelName).remove({},callback);
},function(err) { 
   //done 
});

As an example of a programtic call that does not need require for a registered model.

Upvotes: 2

Yuri Zarubin
Yuri Zarubin

Reputation: 11677

The mongoose.model('Foo', FooSchema) line registers the model globally inside mongoose, which means it is possible to get the model via mongoose.model('Foo') anywhere inside the application. However, the model Foo is registered only after the foos.js is require'd inside the application. Therefore you still need to require the file somewhere, hence the line require( './models/Foos' )

EDIT: use a loop to require every file inside a model directory.

var normalizedPath = require('path').join(__dirname, "./models");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
  require("./models/" + file);
});

Upvotes: 3

Related Questions