UNPKG

mongodb-wrapper

Version:

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

157 lines (135 loc) 4.03 kB
// Generated by CoffeeScript 1.4.0 (function() { var Cursor, EventEmitter, 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; }; sortSyntax = require("./sortSyntax"); EventEmitter = require('events').EventEmitter; Cursor = (function(_super) { __extends(Cursor, _super); function Cursor(collection, selector, fields) { var _this = this; this.collection = collection; this.selector = selector; this.fields = fields; this._order = []; if (this.collection.isOpen()) { this._collection = this.collection.rawCollection(); } else { this.collection.once("error", function(err) { _this.hasError = err; return _this.emit("error", err); }); this.collection.once("ready", function() { _this._collection = _this.collection.rawCollection(); return _this.emit("ready"); }); } } Cursor.prototype.resolve = function(cb) { var _this = this; if (this._collection) { return this._resolve(cb); } else if (this.hasError) { return cb(this.hasError); } else { this.once("ready", function() { return _this._resolve(cb); }); return this.once("error", cb); } }; Cursor.prototype._resolve = function(cb) { var cursor, item, _i, _len, _ref; if (this._cursor) { return cb(null, this._cursor); } else { cursor = this._collection.find(this.selector, this.fields); _ref = this._order; for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; cursor = cursor[item[0]].apply(cursor, item[1]); } this._cursor = cursor; return cb(null, this._cursor); } }; Cursor.prototype.limit = function(limit) { this._order.push(["limit", [limit]]); return this; }; Cursor.prototype.skip = function(skip) { this._order.push(["skip", [skip]]); return this; }; Cursor.prototype.sort = function(fields) { this._order.push(["sort", [sortSyntax(fields)]]); return this; }; Cursor.prototype.one = function(cb) { var _this = this; this.resolve(function(err, cursor) { if (err) { return cb(err); } return cursor.nextObject(cb); }); return this; }; Cursor.prototype.next = function(cb) { var _this = this; this.resolve(function(err, cursor) { if (err) { return cb(err); } return cursor.nextObject(cb); }); return this; }; Cursor.prototype.each = function(cb) { var _this = this; this.resolve(function(err, cursor) { if (err) { return cb(err); } return cursor.each(cb); }); return this; }; Cursor.prototype.explain = function(cb) { var _this = this; this.resolve(function(err, cursor) { if (err) { return cb(err); } return cursor.explain(cb); }); return this; }; Cursor.prototype.toArray = function(cb) { var _this = this; this.resolve(function(err, cursor) { if (err) { return cb(err); } return cursor.toArray(cb); }); return this; }; Cursor.prototype.count = function(cb) { var _this = this; this.resolve(function(err, cursor) { if (err) { return cb(err); } return cursor.count(cb); }); return this; }; Cursor.prototype.getRawCursor = function(cb) { return this.resolve(cb); }; return Cursor; })(EventEmitter); module.exports = Cursor; }).call(this);