Skip to content

Instantly share code, notes, and snippets.

@ryancole
Created January 7, 2012 19:25
Show Gist options
  • Save ryancole/1575722 to your computer and use it in GitHub Desktop.
Save ryancole/1575722 to your computer and use it in GitHub Desktop.
express = require 'express'
routes = require './routes'
models = require './models'
# initialize the application server
app = module.exports = express.createServer()
...
models.user.count()
mongo = require 'mongodb'
model_user = require './user'
# define the database connection
db_server = new mongo.Server 'localhost', 27107, {}
db_client = new mongo.Db 'foo', db_server
# open the database connection
db_client.open ->
console.log 'database connection opened'
# export the db object modules
exports.user = new model_user(db_client)
# define the user db object
class User
constructor: (@db) ->
count: ->
@db.collection 'users', (err, collection) ->
collection.count (err, count) ->
if err
console.log err
else
console.log "there are #{ count } users"
# export the db object
module.exports = User
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment