Code coverage report for dist\lib\ModelHandlers.js

Statements: 100% (39 / 39)      Branches: 100% (19 / 19)      Functions: 100% (10 / 10)      Lines: 100% (38 / 38)      Ignored: none     

All files » dist/lib/ » ModelHandlers.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 571 1 1 1 31   1 412 412 412       412 412   412 410     412 35   412 412 35 412       1 116 116 228 228 19 228 228 228 8 220       1 16 16 16 4 16     1   1    
var _ = require('lodash');
var Bluebird = require('bluebird');
var ModelHandlers = (function () {
    function ModelHandlers(model) {
        this.model = model;
    }
    ModelHandlers.prototype.documentReceived = function (conditions, result, wrapper, options) {
        var _this = this;
        if (options === void 0) { options = {}; }
        _.defaults(options, {
            cache: true,
            partial: false
        });
        return Bluebird.resolve(result).then(function (target) {
            return Bluebird.resolve().then(function () {
                // Cache the document if caching is enabled
                if (_this.model.core.cache && options.cache && !options.fields) {
                    _this.model.cache.set(target); // Does not block execution pipeline - fire and forget
                }
                // Trigger the received hook
                if (_this.model.hooks.onRetrieved)
                    _this.model.hooks.onRetrieved(target);
                // Wrap the document and trigger the ready hook
                var wrapped = wrapper(target, false, !!options.fields);
                if (_this.model.hooks.onReady && wrapped instanceof _this.model.Instance)
                    _this.model.hooks.onReady(wrapped);
                return wrapped;
            });
        });
    };
    ModelHandlers.prototype.creatingDocuments = function (documents) {
        var _this = this;
        return Bluebird.all(documents.map(function (document) {
            return Bluebird.resolve().then(function () {
                if (_this.model.hooks.onCreating)
                    _this.model.hooks.onCreating(document);
                document = _this.model.helpers.convertToDB(document);
                var validation = _this.model.helpers.validate(document);
                if (validation.failed)
                    return Bluebird.reject(validation.error);
                return document;
            });
        }));
    };
    ModelHandlers.prototype.savingDocument = function (instance, changes) {
        var _this = this;
        return Bluebird.resolve().then(function () {
            if (_this.model.hooks.onSaving)
                _this.model.hooks.onSaving(instance, changes);
            return instance;
        });
    };
    return ModelHandlers;
})();
exports.default = ModelHandlers;
 
//# sourceMappingURL=../lib/ModelHandlers.js.map