# Code coverage report for node-mongoskin/lib/collection.js

## Statements: 97.06% (66 / 68) Branches: 88.89% (16 / 18) Functions: 100% (14 / 14) Lines: 98.51% (66 / 67) Ignored: none

All files » node-mongoskin/lib/ » collection.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207

    1
    1
    1
    1
    1

    1

    1
    2
    2
    2

    1
    22
    22
    22
    22

    1

    1
    12
    12
    12
    12
    2

    10

    12
    12

    1
    6
    6
    6
    6
    2

    4

    6
    6

    1
    24
    24
    24
    24

    1
    14

    1
    4
    4
    4
    2
    2

    4

    1
    6
    6
    6
    4
    4

    6

    1
    28
    28
    16

    12
    2
    2

    10
    10
    10
    10

     /*!
     * mongoskin - collection.js
     *
     * Copyright(c) 2011 - 2012 kissjs.org
     * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
     * MIT Licensed
     */

    "use strict";

    /**
     * Module dependencies.
     */
    var __slice = Array.prototype.slice;
    var Collection = require('mongodb').Collection;
    var SkinCursor = require('./cursor').SkinCursor;
    var helper = require('./helper');
    var utils = require('./utils');

    /**
     * Constructor
     */
    var SkinCollection = exports.SkinCollection = utils.makeSkinClass(Collection);

    /**
     * bind extend functions to collection
     *
     * e.g.
     *
     * db.bind('article').bind({
     *   getPostByAuthor: function(id, callback) {
     *      this.findOne({author_id: id}, callback);
     *   }
     * });
     *
     */
    SkinCollection.prototype.bind = function(extendObject) {
      for(var key in extendObject) {
        Eif(typeof extendObject[key] == 'function') {
          this[key] = extendObject[key].bind(this);
        } else {
          this[key] = extendObject[key];
        }
      }
    }

    SkinCollection.prototype._open = function(callback) {
      var collection_args = this._collection_args.concat([callback]);
      this._skin_db.open(function(err, db) {
          Iif(err) return callback(err);
          db.collection.apply(db, collection_args);
      });
    }

    /*
     * find is a special method, because it could return a SkinCursor instance
     */
    SkinCollection.prototype._find = SkinCollection.prototype.find;

    /**
     * same args as find, but use Array as callback result but not use Cursor
     *
     * findItems(args, function (err, items) {});
     *
     * same as
     *
     * find(args).toArray(function (err, items) {});
     *
     * or using `mongodb.collection.find()`
     *
     * find(args, function (err, cursor) {
     *   cursor.toArray(function (err, items) {
     *   });
     * });
     *
     * @param {Object} [query]
     * @param {Object} [options]
     * @param {Function(err, docs)} callback
     * @return {SkinCollection} this
     * @api public
     */
    SkinCollection.prototype.findItems = function (query, options, callback) {
      var args = __slice.call(arguments);
      var fn = args[args.length - 1];
      args[args.length - 1] = function (err, cursor) {
        if (err) {
          return fn(err);
        }
        cursor.toArray(fn);
      };
      this.find.apply(this, args);
      return this;
    };

    /**
     * find and cursor.each(fn).
     *
     * @param {Object} [query]
     * @param {Object} [options]
     * @param {Function(err, item)} eachCallback
     * @return {SkinCollection} this
     * @api public
     */
    SkinCollection.prototype.findEach = function (query, options, eachCallback) {
      var args = __slice.call(arguments);
      var fn = args[args.length - 1];
      args[args.length - 1] = function (err, cursor) {
        if (err) {
          return fn(err);
        }
        cursor.each(fn);
      };
      this.find.apply(this, args);
      return this;
    };

    /**
     * Operate by object.`_id`
     *
     * @param {String} methodName
     * @param {String|ObjectID|Number} id
     * @param {Arguments|Array} args
     * @return {SkinCollection} this
     * @api private
     */
    SkinCollection.prototype._operateById = function (methodName, id, args) {
      args = __slice.call(args);
      args[0] = {_id: helper.toObjectID(id)};
      this[methodName].apply(this, args);
      return this;
    };

    /**
     * Find one object by _id.
     *
     * @param {String|ObjectID|Number} id, doc primary key `_id`
     * @param {Function(err, doc)} callback
     * @return {SkinCollection} this
     * @api public
     */
    SkinCollection.prototype.findById = function (id, callback) {
      return this._operateById('findOne', id, arguments);
    };

    /**
     * Update doc by _id.
     * @param {String|ObjectID|Number} id, doc primary key `_id`
     * @param {Object} doc
     * @param {Function(err)} callback
     * @return {SkinCollection} this
     * @api public
     */
    SkinCollection.prototype.updateById = function (id, doc, callback) {
      var oldCb = callback;
      var _this = this;
      if (callback) {
        callback = function(error, res) {
          oldCb.call(_this, error, res.result);
        };
      }
      return this._operateById('update', id, [id, doc, callback]);
    };

    /**
     * Remove doc by _id.
     * @param {String|ObjectID|Number} id, doc primary key `_id`
     * @param {Function(err)} callback
     * @return {SkinCollection} this
     * @api public
     */
    SkinCollection.prototype.removeById = function (id, callback) {
      var oldCb = callback;
      var _this = this;
      if (callback) {
        callback = function(error, res) {
          oldCb.call(_this, error, res.result.n);
        };
      }
      return this._operateById('remove', id, [id, callback]);
    };

    /**
     * Creates a cursor for a query that can be used to iterate over results from MongoDB.
     *
     * @param {Object} query
     * @param {Object} options
     * @param {Function(err, docs)} callback
     * @return {SkinCursor|SkinCollection} if last argument is not a function, then returns a SkinCursor,
     *   otherise return this
     * @api public
     */
    SkinCollection.prototype.find = function (query, options, callback) {
      var args = __slice.call(arguments);
      if(this.isOpen()) {
        return this._native.find.apply(this._native, args);
      }
      if (args.length > 0 && typeof args[args.length - 1] === 'function') {
        this._find.apply(this, args);
        return this;
      } else {
        var cursor = new SkinCursor();
        cursor._skin_collection = this;
        cursor._find_args = args;
        return cursor;
      }
    };