Created
February 3, 2012 17:56
-
-
Save troygoode/1731402 to your computer and use it in GitHub Desktop.
How I handle mongoose schemas & models.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require.dir = require 'require-directory' | |
| config = require './config' | |
| mongoose = require 'mongoose' | |
| schemas = require.dir module, './models/' | |
| # connect to mongo | |
| models = {} # I often do "global.models = {}" instead | |
| for key, schema of schemas | |
| mongoose.model key, schema | |
| models[key] = mongoose.model key | |
| mongoose.connect config.CONNECTION_STRING | |
| models.product.find {price: {$gt: 1.99}}, (err, products)-> | |
| console.log products | |
| # TODO: build your app here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #NOTE: this file is stored inside the /models/ directory | |
| mongoose = require 'mongoose' | |
| ProductSchema = new mongoose.Schema | |
| description: String | |
| price: | |
| type: Number | |
| min: 0 | |
| module.exports = ProductSchema |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment