Skip to content

Instantly share code, notes, and snippets.

@troygoode
Created February 3, 2012 17:56
Show Gist options
  • Select an option

  • Save troygoode/1731402 to your computer and use it in GitHub Desktop.

Select an option

Save troygoode/1731402 to your computer and use it in GitHub Desktop.
How I handle mongoose schemas & models.
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
#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