UNPKG

mongodb-wrapper

Version:

Exactly-like-the-console wrapper for node-mongodb-native

231 lines (195 loc) 6.7 kB
// Generated by CoffeeScript 1.4.0 (function() { var CONN_CLOSED, CONN_OPEN, Collection, Cursor, EventEmitter, noop, sortSyntax, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; CONN_CLOSED = 0; CONN_OPEN = 1; EventEmitter = require('events').EventEmitter; sortSyntax = require("./sortSyntax"); Cursor = require("./Cursor"); noop = function() {}; Collection = (function(_super) { __extends(Collection, _super); function Collection(db, collName) { var _this = this; this.db = db; this.collName = collName; this.state = CONN_CLOSED; this.queue = []; this.db._getConnection(function(err, connection) { if (err) { return _this.emit("error", err); } return connection.collection(_this.collName, function(err, collection) { if (err) { return _this.emit("error", err); } _this.state = CONN_OPEN; _this._collection = collection; _this.emit("ready"); return _this.drainQueue(); }); }); } Collection.prototype.isOpen = function() { return this.state === CONN_OPEN; }; Collection.prototype.rawCollection = function() { return this._collection; }; Collection.prototype.name = function() { return this.collName; }; Collection.prototype.database = function() { return this.db; }; Collection.prototype.drainQueue = function() { var item, _i, _len, _ref, _results; _ref = this.queue; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; _results.push(this._runCommand(item.name, item.params, item.cb)); } return _results; }; Collection.prototype.runCommand = function(name, params, cb) { if (this.state === CONN_CLOSED) { return this.queue.push({ name: name, params: params, cb: cb }); } else { return this._runCommand(name, params, cb); } }; Collection.prototype._runCommand = function(name, params, cb) { if (typeof params === "function") { cb = params; params = []; } params || (params = []); params.push(cb); return this._collection[name].apply(this._collection, params); }; Collection.prototype.ensureIndex = function(index, options, cb) { return this.runCommand("ensureIndex", [index, options], cb); }; Collection.prototype.dropIndexes = function(cb) { return this.runCommand("dropAllIndexes", cb); }; Collection.prototype.renameCollection = function(targetName, dropTarget, cb) { var _this = this; if (typeof dropTarget === "function") { cb = dropTarget; dropTarget = false; } cb || (cb = noop); if (dropTarget) { return this.db.dropCollection(targetName, function(err) { if (err) { return cb(err); } return _this.db.renameCollection(_this.collName, _this.db.prefixName(targetName), cb); }); } else { return this.db.renameCollection(this.collName, this.db.prefixName(targetName), cb); } }; Collection.prototype.insert = function(docs, cb) { cb || (cb = noop); return this.runCommand("insert", [docs], cb); }; Collection.prototype.remove = function(selector, cb) { cb || (cb = noop); return this.runCommand("remove", [selector], cb); }; Collection.prototype.drop = function(cb) { cb || (cb = noop); return this.runCommand("drop", function(err) { if (err && err.message === "ns not found") { return cb(); } return cb(err); }); }; Collection.prototype.save = function(doc, cb) { cb || (cb = noop); return this.runCommand("save", [doc], cb); }; Collection.prototype.update = function(selector, updates, upsert, multi, cb) { var options; 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 || (cb = noop); return this.runCommand("update", [selector, updates, options], cb); }; Collection.prototype.count = function(cb) { return this.runCommand("count", cb); }; Collection.prototype.findAndModify = function(options, cb) { var fields, query, sort, update; cb || (cb = noop); query = options.query || {}; sort = options.sort ? sortSyntax(options.sort) : []; update = options.update || {}; fields = options.fields || {}; delete options.query; delete options.sort; delete options.update; return this.runCommand("findAndModify", [query, sort, update, options], cb); }; Collection.prototype.find = function(selector, fields) { selector || (selector = {}); fields || (fields = {}); return new Cursor(this, selector, fields); }; Collection.prototype.findOne = function(selector, fields, cb) { if (typeof selector === "function") { cb = selector; selector = {}; fields = {}; } else if (typeof fields === "function") { cb = fields; fields = {}; selector || (selector = {}); } return this.runCommand("findOne", [selector, fields], cb); }; Collection.prototype.group = function(options, cb) { var cond, initial, key, reduce; cb || (cb = noop); options || (options = {}); reduce = options.reduce || options['$reduce'] || noop; cond = options.cond || {}; key = options.key || {}; initial = options.intial || {}; return this.runCommand("group", [key, cond, initial, reduce], cb); }; Collection.prototype.mapReduce = function(map, reduce, options, cb) { cb || (cb = noop); return this.runCommand("mapReduce", [map, reduce, options], cb); }; Collection.prototype.distinct = function(key, query, cb) { if (typeof query === "function") { cb = query; query = null; } return this.runCommand("distinct", [key, query], cb); }; return Collection; })(EventEmitter); module.exports = Collection; }).call(this);