Code coverage report for dist\lib\Core.js

Statements: 100% (81 / 81)      Branches: 93.33% (28 / 30)      Functions: 100% (16 / 16)      Lines: 100% (81 / 81)      Ignored: none     

All files » dist/lib/ » Core.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  1 1 1 1 1 1 1 1 41 41 41 41 41 41 10 31 31   41 1 40 40   1           447         1             1         1             1089         1           38 38 6 32 32 7 7 7 7   32 32 4 3   1   32 5 10 6 4 2   2     32 7   25 32 32         1           444     7                   1 12 12             1 48 48 48 21 27   47 47             1 30 30 30 5 25 25 25 25               1 4   1   1    
/// <reference path="../_references.d.ts" />
var Bluebird = require('bluebird');
var MongoDB = require('mongodb');
var _ = require('lodash');
var Express_1 = require('./middleware/Express');
var NoOpCache_1 = require('./caches/NoOpCache');
var mongoConnectAsyc = Bluebird.promisify(MongoDB.MongoClient.connect);
var Core = (function () {
    function Core(uri, config) {
        this._plugins = [];
        this._cache = new NoOpCache_1.default();
        var args = Array.prototype.slice.call(arguments, 0);
        uri = config = null;
        for (var i = 0; i < args.length; i++) {
            if (typeof args[i] == 'string')
                uri = args[i];
            else Eif (typeof args[i] == 'object')
                config = args[i];
        }
        if (!uri && !config)
            throw new Error("Expected either a URI or config object to be supplied when initializing Iridium");
        this._url = uri;
        this._config = config;
    }
    Object.defineProperty(Core.prototype, "plugins", {
        /**
         * Gets the plugins registered with this Iridium Core
         * @returns {[Iridium.Plugin]}
         */
        get: function () {
            return this._plugins;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Core.prototype, "settings", {
        /**
         * Gets the configuration specified in the construction of this
         * Iridium Core.
         * @returns {Iridium.Configuration}
         */
        get: function () {
            return this._config;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Core.prototype, "connection", {
        /**
         * Gets the currently active database connection for this Iridium
         * Core.
         * @returns {MongoDB.Db}
         */
        get: function () {
            return this._connection;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Core.prototype, "url", {
        /**
         * Gets the URL used to connect to MongoDB
         * @returns {String}
         */
        get: function () {
            var _this = this;
            if (this._url)
                return this._url;
            var url = 'mongodb://';
            if (this._config.username) {
                url += this._config.username;
                Eif (this._config.password)
                    url += ':' + this._config.password;
                url += '@';
            }
            var hosts = [];
            if (this._config.host) {
                if (this._config.port)
                    hosts.push(this._config.host + ':' + this._config.port);
                else
                    hosts.push(this._config.host);
            }
            if (this._config.hosts) {
                _.each(this._config.hosts, function (host) {
                    if (host.port)
                        hosts.push(host.address + ':' + host.port);
                    else if (_this._config.port)
                        hosts.push(host.address + ':' + _this._config.port);
                    else
                        hosts.push(host.address);
                });
            }
            if (hosts.length)
                url += _.uniq(hosts).join(',');
            else
                url += 'localhost';
            url += '/' + this._config.database;
            return url;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Core.prototype, "cache", {
        /**
         * Gets the cache used to store objects retrieved from the database for performance reasons
         * @returns {cache}
         */
        get: function () {
            return this._cache;
        },
        set: function (value) {
            this._cache = value;
        },
        enumerable: true,
        configurable: true
    });
    /**
     * Registers a new plugin with this Iridium Core
     * @param {Iridium.Plugin} plugin The plugin to register with this Iridium Core
     * @returns {Iridium.Core}
     */
    Core.prototype.register = function (plugin) {
        this.plugins.push(plugin);
        return this;
    };
    /**
     * Connects to the database server specified in the provided configuration
     * @param {function(Error, Iridium.Core)} [callback] A callback to be triggered once the connection is established.
     * @returns {Promise}
     */
    Core.prototype.connect = function (callback) {
        var self = this;
        return Bluebird.bind(this).then(function () {
            if (self._connection)
                return self._connection;
            return mongoConnectAsyc(self.url);
        }).then(function (db) {
            self._connection = db;
            return self;
        }).nodeify(callback);
    };
    /**
     * Closes the active database connection
     * @type {Promise}
     */
    Core.prototype.close = function () {
        var self = this;
        return Bluebird.bind(this).then(function () {
            if (!self._connection)
                return this;
            var conn = self._connection;
            self._connection = null;
            conn.close();
            return this;
        });
    };
    /**
     * Provides an express middleware which can be used to set the req.db property
     * to the current Iridium instance.
     * @returns {Iridium.ExpressMiddleware}
     */
    Core.prototype.express = function () {
        return Express_1.default(this);
    };
    return Core;
})();
exports.default = Core;
 
//# sourceMappingURL=../lib/Core.js.map