Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Structure of Mongoose & NodeJS Project

I currently have all my models (Schema definitions) in the /models/models.js file for my Mongoose/NodeJS application.

I'd like to break these apart into different files as such: user_account.js, profile.js, etc. However I cannot seem to do so as my controllers break and report back "cannot find module" once I pull these classes apart.

My project structure is as follows:

/MyProject   /controllers     user.js     foo.js     bar.js     // ... etc, etc   /models     models.js   server.js 

The contents of my models.js file looks like this:

var mongoose = require('mongoose'),     Schema = mongoose.Schema,     ObjectId = Schema.ObjectId;  mongoose.connect('mongodb://localhost/mydb');  var UserAccount = new Schema({     user_name       : { type: String, required: true, lowercase: true, trim: true, index: { unique: true } },      password        : { type: String, required: true },     date_created    : { type: Date, required: true, default: Date.now } });   var Product = new Schema({     upc             : { type: String, required: true, index: { unique: true } },     description     : { type: String, trim: true },     size_weight     : { type: String, trim: true } }); 

My user.js file (controller) looks like this:

var mongoose    = require('mongoose'),      UserAccount = mongoose.model('user_account', UserAccount);  exports.create = function(req, res, next) {      var username = req.body.username;      var password = req.body.password;      // Do epic sh...what?! :) } 

How can I break the schema definition into multiple files and also reference it from my controller? When I do reference it (after the schema is in a new file) I get this error:

*Error: Schema hasn't been registered for model "user_account".*

Thoughts?

like image 310
Donn Felker Avatar asked Feb 10 '12 16:02

Donn Felker


People also ask

What defines the structure of documents in Mongoose?

A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

What is Mongoose schema explain with example?

When working with NodeJS, we can use mongoose ODM to define a schema for a MongoDB collection. A mongoose schema defines the shape of documents inside a particular collection. In this article, we will discuss how to create a schema in a mongoose with the help of an example.

What is Mongoose schema type?

What is a SchemaType? You can think of a Mongoose schema as the configuration object for a Mongoose model. A SchemaType is then a configuration object for an individual property. A SchemaType says what type a given path should have, whether it has any getters/setters, and what values are valid for that path.

Is Mongoose schema based?

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.


1 Answers

Here's a sample app/models/item.js

var mongoose = require("mongoose");  var ItemSchema = new mongoose.Schema({   name: {     type: String,     index: true   },   equipped: Boolean,   owner_id: {     type: mongoose.Schema.Types.ObjectId,     index: true   },   room_id: {     type: mongoose.Schema.Types.ObjectId,     index: true   } });  var Item = mongoose.model('Item', ItemSchema);  module.exports = {   Item: Item } 

To load this from an item controller in app/controllers/items.js I would do

  var Item = require("../models/item").Item;   //Now you can do Item.find, Item.update, etc 

In other words, define both the schema and the model in your model module and then export just the model. Load your model modules into your controller modules using relative require paths.

To make the connection, handle that early in your server startup code (server.js or whatever). Usually you'll want to read the connection parameters either from a configuration file or from environment variables and default to development mode localhost if no configuration is provided.

var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost'); 
like image 111
Peter Lyons Avatar answered Sep 22 '22 19:09

Peter Lyons