Skip to content

Instantly share code, notes, and snippets.

@shripadk
Created December 11, 2010 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shripadk/737569 to your computer and use it in GitHub Desktop.
Save shripadk/737569 to your computer and use it in GitHub Desktop.
Db = require('mongodb').Db
Connection = require('mongodb').Connection
Server = require('mongodb').Server
EventEmitter = require('events').EventEmitter
sys = require('sys')
colors = require('colors')
host = if process.env['MONGO_NODE_DRIVER_HOST'] then process.env['MONGO_NODE_DRIVER_HOST'] else 'localhost'
port = if process.env['MONGO_NODE_DRIVER_PORT'] then process.env['MONGO_NODE_DRIVER_PORT'] else Connection.DEFAULT_PORT
db = []
class Mongode extends EventEmitter
constructor: (database, host, port, serverConfig, options) ->
@database = database
@host = host or 'localhost'
@port = port or 27017
@serverConfig = serverConfig || {}
@options = options || {}
@connected = false
db = new Db(@database, new Server(@host, @port, @serverConfig), @options)
db.open (e, db) =>
if e
@emit 'error', e
@emit 'log', "#{new Date()} : Error: #{e}"
throw new Error(e)
if db
@db = db
@connected = true
@emit 'log', "#{new Date()} : Connected: #{db.databaseName}"
@emit 'connection', db
state : () ->
return @connected
logInfo : (info) ->
@emit 'log', "#{new Date()} : #{info}".blue
logError : (error) ->
@emit 'log', "#{new Date()} : Error: #{error.toString().replace(/error:/i, '')}".red
get : (db) ->
@db = db
handleCallback : () ->
@logError "Not supplied a callback!"
return true
insert : (data, callback) ->
if @connected
if !callback
@handleCallback()
return true
if !data
@logError "No data specified for insertion! Command: insertInto"
return true
if data
console.log @db.collectionName
@db.collection @db.collectionName, (e, collection) =>
if e
@emit 'error', e
@logError e
callback e, null
if collection
collection.insert data, (e, docs) =>
if e
@emit 'error', e
@logError e
callback e, null
if docs
@logInfo "Inserted doc(s)! Command: insertInto"
callback null, docs
findAll : (callback) ->
if @connected
if !callback
@handleCallback()
return true
if callback
@db.collection @db.collectionName, (e, collection) =>
if e
@emit 'error', e
@logError e
callback e, null
if collection
collection.find (e, cursor) =>
if e
@emit 'error', e
@logError e
callback e, null
if cursor
cursor.toArray (e, docs) =>
if e
@emit 'error', e
@logError e
callback e, null
if docs
@logInfo "Fetched all doc(s)! Command: findAll"
callback null, docs
findFirst : (callback) ->
if @connected
if !callback
@handleCallback()
return true
if callback
@db.collection @db.collectionName, (e, collection) =>
if e
@emit 'error', e
@logError e
callback e, null
if collection
collection.findOne {}, {}, (e, doc) =>
if e
@emit 'error', e
@logError e
callback e, null
if doc
@logInfo "Fetched doc! Command: findFirst"
callback null, doc
findOne : (query, options, callback) ->
if @connected
if !query or typeof query is 'function'
@logError "Query should be of type object! Command: fineOne"
return true
if !options or typeof options is 'function'
@logError "Option should be of type object! Command: fineOne"
return true
if !callback
@handleCallback()
return true
if callback
@db.collection @db.collectionName, (e, collection) =>
if e
@emit 'error', e
@logError e
callback e, null
if collection
collection.findOne query, options, (e, doc) =>
if e
@emit 'error', e
@logError e
callback e, null
if doc
@logInfo "Fetched doc! Command: findOne"
callback null, doc
class Collection extends Mongode
constructor: (db, name) ->
@db[name] = db
@db.collectionName = name
get : () ->
super @db
exports.Collection = Collection
exports.Mongode = Mongode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment