all files / dist/satellites/ models.js

87.37% Statements 83/95
67.86% Branches 19/28
91.67% Functions 22/24
87.18% Lines 68/78
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                          18×                                                                                                                                                                                                                                           18×                                                                                                                                                                                                                       18×                                                            
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _utils = require('../utils');
 
var _utils2 = _interopRequireDefault(_utils);
 
var _mongoose = require('mongoose');
 
var _mongoose2 = _interopRequireDefault(_mongoose);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/**
 * Manage the models.
 */
 
var Models = function () {
 
  /**
   * Create a new Models call instance.
   *
   * @param api   API reference.
   */
 
 
  /**
   * Connection status.
   *
   * @type {boolean}
   */
 
 
  /**
   * Reference for the API object.
   *
   * @type {null}
   */
 
  function Models(api) {
    _classCallCheck(this, Models);
 
    this.api = null;
    this.mongoose = null;
    this.connected = false;
    this.models = new Map();
 
    this.api = api;
  }
 
  /**
   * Open connection to MongoDB server.
   *
   * @param callback  Callback function.
   */
 
 
  /**
   * Hash with all registered models.
   *
   * @type {Map}
   */
 
 
  /**
   * Mongoose object.
   *
   * @type {null}
   */
 
 
  _createClass(Models, [{
    key: 'openConnection',
    value: function openConnection(callback) {
      var self = this;
 
      // if the connection has already open return and execute the callback
      Iif (self.status()) {
        callback(new Error('Connection is already open'));
        return;
      }
 
      // hack: this fix a strange bug on the test environment
      if (self.api.env === 'test' && _mongoose2.default.connections[0]._hasOpened === true) {
        // save the mongoose instance
        self.mongoose = _mongoose2.default;
 
        // mark mongoose was connected
        self.connected = true;
 
        // execute the callback function and return
        callback();
        return;
      }
 
      var connectCallback = function connectCallback() {
        // save mongoose object
        self.mongoose = _mongoose2.default;
 
        // open the new connection
        self.mongoose.connect(self.api.config.models.connectionString, function (error) {
          Iif (error) {
            self.api.log('MongoDB Error: ' + err, 'emerg');
            return;
          }
 
          self.api.log('connected to MongoDB', 'debug');
          self.connected = true;
          callback();
        });
 
        // define handler for disconnected event
        self.mongoose.connection.on('disconnected', function () {
          self.connected = false;
          self.api.log('MongoDB Connection Closed', 'debug');
        });
      };
 
      // check if we are use a mock version of the package
      Eif (self.api.config.models.pkg === 'mockgoose') {
        // require mockgoose
        var mockgoose = require('mockgoose');
 
        // wrap mongoose with mockgoose
        mockgoose(_mongoose2.default).then(connectCallback);
 
        // log an warning
        self.api.log('running with mockgoose', 'warning');
      } else {
        connectCallback();
      }
    }
 
    /**
     * Close connection.
     *
     * @param callback  Callback function.
     */
 
  }, {
    key: 'closeConnection',
    value: function closeConnection(callback) {
      var self = this;
 
      // if there is not connection open return now
      Iif (!self.status()) {
        callback(new Error('There is no connection open'));
        return;
      }
 
      self.mongoose.connection.close(callback);
    }
 
    /**
     * Return the connection status.
     *
     * @returns {boolean}
     */
 
  }, {
    key: 'status',
    value: function status() {
      return this.connected;
    }
 
    /**
     * Add a new model.
     *
     * If the model already exists it will be replaced.
     *
     * @param name    Model name
     * @param schema  Model schema.
     */
 
  }, {
    key: 'add',
    value: function add(name, schema) {
      // if the model already exists that can't be overwrite
      Iif (this.models.has(name)) {
        return;
      }
 
      // save the new model instance
      this.models.set(name, this.mongoose.model(name, schema));
    }
 
    /**
     * Get a model object from the repository.
     *
     * @param modelName   model name to get.
     * @returns {V}       model object.
     */
 
  }, {
    key: 'get',
    value: function get(modelName) {
      return this.models.get(modelName);
    }
 
    /**
     * Remove a model from the repository.
     *
     * @param modelName   model name to be deleted.
     */
 
  }, {
    key: 'remove',
    value: function remove(modelName) {
      this.models.delete(modelName);
    }
  }]);
 
  return Models;
}();
 
/**
 * Initializer for the models features.
 */
 
 
var _class = function () {
  function _class() {
    _classCallCheck(this, _class);
 
    this.loadPriority = 100;
    this.startPriority = 100;
    this.stopPriority = 400;
  }
 
  /**
   * Initializer load priority.
   *
   * @type {number}
   */
 
 
  /**
   * Initializer start priority.
   *
   * @type {number}
   */
 
 
  /**
   * Initializer stop priority.
   *
   * @type {number}
   */
 
 
  _createClass(_class, [{
    key: 'load',
 
 
    /**
     * Initializer loading function.
     *
     * @param api   API reference.
     * @param next  Callback function.
     */
    value: function load(api, next) {
      // expose models class on the engine
      api.models = new Models(api);
 
      // finish the initializer loading
      next();
    }
 
    /**
     * Initializer start function.
     *
     * @param api   API reference.
     * @param next  Callback function.
     */
 
  }, {
    key: 'start',
    value: function start(api, next) {
      // cleanup mongoose cache
      _mongoose2.default.models = {};
      _mongoose2.default.modelSchemas = {};
 
      // open connection
      api.models.openConnection(function () {
        // read models files from the modules
        api.modules.modulesPaths.forEach(function (modulePath) {
          _utils2.default.recursiveDirectoryGlob(modulePath + '/models').forEach(function (moduleFile) {
            // get file basename
            var basename = _path2.default.basename(moduleFile, '.js');
 
            // load the model
            api.models.add(basename, require(moduleFile).default);
 
            // log a message
            api.log('model loaded: ' + basename, 'debug');
          });
        });
 
        // finish the initializer start
        next();
      });
    }
 
    /**
     * Initializer stop function.
     *
     * @param api   API reference.
     * @param next  Callback function.
     */
 
  }, {
    key: 'stop',
    value: function stop(api, next) {
      // close connection
      api.models.closeConnection(next);
    }
  }]);
 
  return _class;
}();
 
exports.default = _class;
//# sourceMappingURL=models.js.map