mongodb-wrapper
Version:
Exactly-like-the-console wrapper for node-mongodb-native
163 lines (124 loc) • 3.8 kB
text/coffeescript
CONN_CLOSED = 0
CONN_OPEN = 1
{EventEmitter} = require 'events'
sortSyntax = require "./sortSyntax"
Cursor = require "./Cursor"
noop = ->
class Collection extends EventEmitter
constructor: (, ) ->
= CONN_CLOSED
= []
._getConnection (err, connection) =>
return "error", err if err
connection.collection , (err, collection) =>
return "error", err if err
= CONN_OPEN
= collection
"ready"
isOpen: -> == CONN_OPEN
rawCollection: ->
name: ->
database: ->
drainQueue: ->
for item in
item.name, item.params, item.cb
runCommand: (name, params, cb) ->
if is CONN_CLOSED
return .push {name, params, cb}
else
name, params, cb
_runCommand: (name, params, cb) ->
if typeof params == "function"
cb = params
params = []
params ||= []
params.push cb
[name].apply , params
ensureIndex: (index, options, cb) ->
"ensureIndex", [index, options], cb
dropIndexes: (cb) ->
"dropAllIndexes", cb
renameCollection: (targetName, dropTarget, cb) ->
if typeof dropTarget == "function"
cb = dropTarget
dropTarget = false
# we sometimes do this without a cb...
cb ||= noop
if dropTarget
.dropCollection targetName, (err) =>
return cb err if err
.renameCollection , .prefixName(targetName), cb
else
.renameCollection , .prefixName(targetName), cb
insert: (docs, cb) ->
cb ||= noop
"insert", [docs], cb
remove: (selector, cb) ->
cb ||= noop
"remove", [selector], cb
drop: (cb) ->
cb ||= noop
"drop", (err) ->
return cb() if err and err.message == "ns not found"
cb err
save: (doc, cb) ->
cb ||= noop
"save", [doc], cb
update: (selector, updates, upsert, multi, cb) ->
if typeof upsert == "function"
cb = upsert
upsert = false
multi = false
else if typeof multi == "function"
cb = multi
multi = false
options = {
upsert: upsert
multi: multi
}
cb ||= noop
"update", [selector, updates, options], cb
count: (cb) ->
"count", cb
findAndModify: (options, cb) ->
cb ||= noop
query = options.query || {}
sort = if options.sort then sortSyntax(options.sort) else []
update = options.update || {}
fields = options.fields || {}
delete options.query
delete options.sort
delete options.update
"findAndModify", [query, sort, update, options], cb
find: (selector, fields) ->
selector ||= {}
fields ||= {}
return new Cursor @, selector, fields
findOne: (selector, fields, cb) ->
if typeof selector == "function"
cb = selector
selector = {}
fields = {}
else if typeof fields == "function"
cb = fields
fields = {}
selector ||= {}
"findOne", [selector, fields], cb
group: (options, cb) ->
cb ||= noop
options ||= {}
reduce = options.reduce || options['$reduce'] || noop
cond = options.cond || {}
key = options.key || {}
initial = options.intial || {}
"group", [key, cond, initial, reduce], cb
mapReduce: (map, reduce, options, cb) ->
cb ||= noop
"mapReduce", [map, reduce, options], cb
distinct: (key, query, cb) ->
if typeof query == "function"
cb = query
query = null
"distinct", [key, query], cb
module.exports = Collection