Code coverage report for dist\lib\Model.js

Statements: 96.23% (255 / 265)      Branches: 91.8% (112 / 122)      Functions: 100% (71 / 71)      Lines: 96.14% (249 / 259)      Ignored: none     

All files » dist/lib/ » Model.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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492  1 1 1 1 1 1 1 1 1 1         1               1 38 38 1 37 1 36 3 33 2 31 31 31 31   1 31 31 31 31 31 31 31 31 9 31 6 31 22   31 26   5   1 31 31 31   1 31 31   1           533         1           121         1           1161         1             62         1             1929         1             544 1 543         1           5             2         1             644         1             426         1         444         1   709         1   31         1   2         1 36 36 36 1 36 36     36   1 85 85 18   85   1 97 97 97 29   97 97 97 97 29 2 27 20 4   16     7   97 97 97     97 97 97   97 2 95 95           95   95       97 7 90     1 7 7 9   7   1 116 116 116 4   116 116 116 116 2   114 114   116 32   84 116 116       116 116 116 2 2 2 2 2   2           114 107 107 107   107     107   218   109 29 80     1 8 8 1 1   8 8 1     8       8 8 8 8 8     8 8             1 6 6 6 1 1   6 6 1     6 6 6 6 6   6         1 205 205 205 1 1   205 1 1 1   205 205 205     205 1     205 205 205 205 205   205       205 63 205     1 10 10 1 1   10 10 10   10                 1 1 1 1     1 4 4 4 1   4   4 4 4   4                 1 11 11 11 11   11       1   1    
/// <reference path="../_references.d.ts" />
var MongoDB = require('mongodb');
var Bluebird = require('bluebird');
var _ = require('lodash');
var Core_1 = require('./Core');
var Instance_1 = require('./Instance');
var Cursor_1 = require('./Cursor');
var ModelCache_1 = require('./ModelCache');
var ModelHelpers_1 = require('./ModelHelpers');
var ModelHandlers_1 = require('./ModelHandlers');
var ModelSpecificInstance_1 = require('./ModelSpecificInstance');
/**
 * An Iridium Model which represents a structured MongoDB collection
 * @class
 */
var Model = (function () {
    /**
     * Creates a new Iridium model representing a given ISchema and backed by a collection whose name is specified
     * @param {Iridium} core The Iridium core that this model should use for database access
     * @param {ModelInterfaces.InstanceImplementation} instanceType The class which will be instantiated for each document retrieved from the database
     * @returns {Model}
     * @constructor
     */
    function Model(core, instanceType) {
        this._hooks = {};
        if (!(core instanceof Core_1.default))
            throw new Error("You failed to provide a valid Iridium core for this model");
        if (typeof instanceType != 'function')
            throw new Error("You failed to provide a valid instance constructor for this model");
        if (typeof instanceType.collection != 'string' || !instanceType.collection)
            throw new Error("You failed to provide a valid collection name for this model");
        if (!_.isPlainObject(instanceType.schema) || instanceType.schema._id === undefined)
            throw new Error("You failed to provide a valid schema for this model");
        this._core = core;
        this.loadExternal(instanceType);
        this.onNewModel();
        this.loadInternal();
    }
    Model.prototype.loadExternal = function (instanceType) {
        this._collection = instanceType.collection;
        this._schema = instanceType.schema;
        this._hooks = instanceType;
        this._cacheDirector = instanceType.cache;
        this._transforms = instanceType.transforms || {};
        this._validators = instanceType.validators || [];
        this._indexes = instanceType.indexes || [];
        if (!this._schema._id)
            this._schema._id = MongoDB.ObjectID;
        if (this._schema._id === MongoDB.ObjectID && !this._transforms['_id'])
            this._transforms['_id'] = {
                fromDB: function (value) { return value._bsontype == 'ObjectID' ? new MongoDB.ObjectID(value.id).toHexString() : value; },
                toDB: function (value) { return value && typeof value === 'string' ? new MongoDB.ObjectID(value) : value; }
            };
        if (instanceType.prototype instanceof Instance_1.default)
            this._Instance = ModelSpecificInstance_1.default(this, instanceType);
        else
            this._Instance = instanceType.bind(undefined, this);
    };
    Model.prototype.loadInternal = function () {
        this._cache = new ModelCache_1.default(this);
        this._helpers = new ModelHelpers_1.default(this);
        this._handlers = new ModelHandlers_1.default(this);
    };
    Model.prototype.onNewModel = function () {
        var _this = this;
        this._core.plugins.forEach(function (plugin) { return plugin.newModel && plugin.newModel(_this); });
    };
    Object.defineProperty(Model.prototype, "helpers", {
        /**
         * Provides helper methods used by Iridium for common tasks
         * @returns {ModelHelpers}
         */
        get: function () {
            return this._helpers;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "handlers", {
        /**
         * Provides helper methods used by Iridium for hook delegation and common processes
         * @returns {ModelHandlers}
         */
        get: function () {
            return this._handlers;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "hooks", {
        /**
         * Gets the even hooks subscribed on this model for a number of different state changes
         * @returns {Hooks}
         */
        get: function () {
            return this._hooks;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "schema", {
        /**
         * Gets the ISchema dictating the data structure represented by this model
         * @public
         * @returns {schema}
         */
        get: function () {
            return this._schema;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "core", {
        /**
         * Gets the Iridium core that this model is associated with
         * @public
         * @returns {Iridium}
         */
        get: function () {
            return this._core;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "collection", {
        /**
         * Gets the underlying MongoDB collection from which this model's documents are retrieved
         * @public
         * @returns {Collection}
         */
        get: function () {
            if (!this.core.connection)
                throw new Error("Iridium Core not connected to a database.");
            return this.core.connection.collection(this._collection);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "collectionName", {
        /**
         * Gets the name of the underlying MongoDB collection from which this model's documents are retrieved
         * @public
         */
        get: function () {
            return this._collection;
        },
        /**
         * Sets the name of the underlying MongoDB collection from which this model's documents are retrieved
         * @public
         */
        set: function (value) {
            this._collection = value;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "cacheDirector", {
        /**
         * Gets the cache controller which dictates which queries will be cached, and under which key
         * @public
         * @returns {CacheDirector}
         */
        get: function () {
            return this._cacheDirector;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "cache", {
        /**
         * Gets the cache responsible for storing objects for quick retrieval under certain conditions
         * @public
         * @returns {ModelCache}
         */
        get: function () {
            return this._cache;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "Instance", {
        /**
         * Gets the constructor responsible for creating instances for this model
         */
        get: function () {
            return this._Instance;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "transforms", {
        get: function () {
            return this._transforms;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "validators", {
        get: function () {
            return this._validators;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Model.prototype, "indexes", {
        get: function () {
            return this._indexes;
        },
        enumerable: true,
        configurable: true
    });
    Model.prototype.find = function (conditions, fields) {
        conditions = conditions || {};
        fields = fields || {};
        if (!_.isPlainObject(conditions))
            conditions = { _id: conditions };
        conditions = this._helpers.convertToDB(conditions);
        var cursor = this.collection.find(conditions, {
            fields: fields
        });
        return new Cursor_1.default(this, conditions, cursor);
    };
    Model.prototype.get = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i - 0] = arguments[_i];
        }
        return this.findOne.apply(this, args);
    };
    Model.prototype.findOne = function () {
        var _this = this;
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i - 0] = arguments[_i];
        }
        var conditions = null;
        var options = null;
        var callback = null;
        for (var argI = 0; argI < args.length; argI++) {
            if (typeof args[argI] == 'function')
                callback = callback || args[argI];
            else if (_.isPlainObject(args[argI])) {
                if (conditions)
                    options = args[argI];
                else
                    conditions = args[argI];
            }
            else
                conditions = { _id: args[argI] };
        }
        conditions = conditions || {};
        options = options || {};
        _.defaults(options, {
            cache: true
        });
        return Bluebird.resolve().bind(this).then(function () {
            conditions = _this._helpers.convertToDB(conditions);
            return _this._cache.get(conditions);
        }).then(function (cachedDocument) {
            if (cachedDocument)
                return cachedDocument;
            return new Bluebird(function (resolve, reject) {
                _this.collection.findOne(conditions, {
                    fields: options.fields,
                    skip: options.skip,
                    sort: options.sort,
                    limit: options.limit
                }, function (err, result) {
                    Iif (err)
                        return reject(err);
                    return resolve(result);
                });
            });
        }).then(function (document) {
            if (!document)
                return null;
            return _this._handlers.documentReceived(conditions, document, function (document, isNew, isPartial) { return _this._helpers.wrapDocument(document, isNew, isPartial); }, options);
        }).nodeify(callback);
    };
    Model.prototype.create = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i - 0] = arguments[_i];
        }
        return this.insert.apply(this, args);
    };
    Model.prototype.insert = function (objs) {
        var _this = this;
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        var objects;
        var options = {};
        var callback = null;
        if (typeof args[0] == 'function')
            callback = args[0];
        else {
            options = args[0];
            callback = args[1];
        }
        if (Array.isArray(objs))
            objects = objs;
        else
            objects = [objs];
        options = options || {};
        _.defaults(options, {
            w: 'majority',
            forceServerObjectId: true
        });
        return Bluebird.resolve().then(function () {
            var queryOptions = { w: options.w, upsert: options.upsert, new: true };
            if (options.upsert) {
                var docs = _this._handlers.creatingDocuments(objects);
                return docs.map(function (object) {
                    return new Bluebird(function (resolve, reject) {
                        _this.collection.findAndModify({ _id: object._id }, ["_id"], object, queryOptions, function (err, result) {
                            Iif (err)
                                return reject(err);
                            return resolve(result);
                        });
                    });
                });
            }
            else
                return _this._handlers.creatingDocuments(objects).then(function (objects) { return _.chunk(objects, 1000); }).map(function (objects) {
                    return new Bluebird(function (resolve, reject) {
                        _this.collection.insertMany(objects, queryOptions, function (err, result) {
                            Iif (err)
                                return reject(err);
                            return resolve(result.ops);
                        });
                    });
                }).then(function (results) { return _.flatten(results); });
        }).map(function (inserted) {
            return _this._handlers.documentReceived(null, inserted, function (document, isNew, isPartial) { return _this._helpers.wrapDocument(document, isNew, isPartial); }, { cache: options.cache });
        }).then(function (results) {
            if (Array.isArray(objs))
                return results;
            return results[0];
        }).nodeify(callback);
    };
    Model.prototype.update = function (conditions, changes, options, callback) {
        var _this = this;
        if (typeof options == 'function') {
            callback = options;
            options = {};
        }
        options = options || {};
        if (!_.isPlainObject(conditions))
            conditions = {
                _id: conditions
            };
        _.defaults(options, {
            w: 'majority',
            multi: true
        });
        return Bluebird.resolve().then(function () {
            conditions = _this._helpers.convertToDB(conditions);
            return new Bluebird(function (resolve, reject) {
                _this.collection.updateMany(conditions, changes, options, function (err, response) {
                    Iif (err)
                        return reject(err);
                    // New MongoDB 2.6+ response type
                    Eif (response.result && response.result.nModified !== undefined)
                        return resolve(response.result.nModified);
                    // Legacy response type
                    return resolve(response.result.n);
                });
            });
        }).nodeify(callback);
    };
    Model.prototype.count = function (conds, callback) {
        var _this = this;
        var conditions = conds;
        if (typeof conds == 'function') {
            callback = conds;
            conditions = {};
        }
        conditions = conditions || {};
        if (!_.isPlainObject(conditions))
            conditions = {
                _id: conditions
            };
        return Bluebird.resolve().then(function () {
            conditions = _this._helpers.convertToDB(conditions);
            return new Bluebird(function (resolve, reject) {
                _this.collection.count(conditions, function (err, results) {
                    Iif (err)
                        return reject(err);
                    return resolve(results);
                });
            });
        }).nodeify(callback);
    };
    Model.prototype.remove = function (conds, options, callback) {
        var _this = this;
        var conditions = conds;
        if (typeof options === 'function') {
            callback = options;
            options = {};
        }
        if (typeof conds == 'function') {
            callback = conds;
            options = {};
            conditions = {};
        }
        conditions = conditions || {};
        options = options || {};
        _.defaults(options, {
            w: 'majority'
        });
        if (!_.isPlainObject(conditions))
            conditions = {
                _id: conditions
            };
        return Bluebird.resolve().then(function () {
            conditions = _this._helpers.convertToDB(conditions);
            return new Bluebird(function (resolve, reject) {
                _this.collection.remove(conditions, options, function (err, response) {
                    Iif (err)
                        return reject(err);
                    return resolve(response.result.n);
                });
            });
        }).then(function (count) {
            if (count === 1)
                _this._cache.clear(conditions);
            return Bluebird.resolve(count);
        }).nodeify(callback);
    };
    Model.prototype.ensureIndex = function (specification, options, callback) {
        var _this = this;
        if (typeof options == 'function') {
            callback = options;
            options = {};
        }
        return new Bluebird(function (resolve, reject) {
            _this.collection.ensureIndex(specification, options, function (err, name) {
                Iif (err)
                    return reject(err);
                return resolve(name);
            });
        }).nodeify(callback);
    };
    /**
     * Ensures that all indexes defined in the model's options are created
     * @param {function(Error, String[])} callback A callback which is triggered when the operation completes
     * @returns {Promise<String[]>} The names of the indexes
     */
    Model.prototype.ensureIndexes = function (callback) {
        var _this = this;
        return Bluebird.resolve(this._indexes).map(function (index) {
            return _this.ensureIndex(index.spec || index, index.options || {});
        }).nodeify(callback);
    };
    Model.prototype.dropIndex = function (specification, callback) {
        var _this = this;
        var index;
        if (typeof (specification) === 'string')
            index = specification;
        else {
            index = _(specification).map(function (direction, key) { return key + '_' + direction; }).reduce(function (x, y) { return x + '_' + y; });
        }
        return new Bluebird(function (resolve, reject) {
            _this.collection.dropIndex(index, function (err, result) {
                Iif (err)
                    return reject(err);
                return resolve(!!result.ok);
            });
        }).nodeify(callback);
    };
    /**
     * Removes all indexes (except for _id) from the collection
     * @param {function(Error, Boolean)} callback A callback which is triggered when the operation completes
     * @returns {Promise<Boolean>} Whether the indexes were dropped
     */
    Model.prototype.dropIndexes = function (callback) {
        var _this = this;
        return new Bluebird(function (resolve, reject) {
            _this.collection.dropAllIndexes(function (err, count) {
                Iif (err)
                    return reject(err);
                return resolve(count);
            });
        }).nodeify(callback);
    };
    return Model;
})();
exports.default = Model;
 
//# sourceMappingURL=../lib/Model.js.map