/// <reference path="../../typings/tsd.d.ts" />
var FS = require('fs');
var Path = require('path');
var Sequelize = require('sequelize');
var DFiles = require('./Files');
var Dollyjs;
(function (Dollyjs) {
/**
* @class Database
* Database Configuration
* Configure the database with setup method then return the DB object
* - This setup support only mongo db
*/
var Database = (function () {
/**
* @constructs Database
*/
function Database(dollyPref) {
Database.modulePath = (DFiles.Files.pathFiles()).databaseFolder;
this.env = process.env.NODE_ENV || dollyPref.stage;
this.databaseConfigure();
this.sequelize = new Sequelize(this.configDatabase.database, this.configDatabase.username, this.configDatabase.password, this.configDatabase);
this.basename = Path.basename(module.filename);
this.db = {};
}
Object.defineProperty(Database.prototype, "basename", {
/**
* @returns {any}
* @public
*/
get: function () {
return this._basename;
},
/**
* @param value {any}
* @public
*/
set: function (value) {
this._basename = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Database.prototype, "db", {
/**
* @returns {any}
* @public
*/
get: function () {
return this._db;
},
/**
* @param value
* @public
*/
set: function (value) {
this._db = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Database.prototype, "configDatabase", {
/**
* @returns {any}
* @public
*/
get: function () {
return this._configDatabase;
},
/**
* @param value
*/
set: function (value) {
this._configDatabase = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Database.prototype, "sequelize", {
/**
* @returns {any}
* @public
*/
get: function () {
return this._sequelize;
},
/**
* @param value
* @public
*/
set: function (value) {
this._sequelize = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Database.prototype, "env", {
/**
* @returns {number}
* @public
*/
get: function () {
return this._env;
},
/**
* @param value
* @public
*/
set: function (value) {
this._env = value;
},
enumerable: true,
configurable: true
});
/**
* @private Config the database
*/
Database.prototype.databaseConfigure = function () {
var databasePathJson = (DFiles.Files.pathFiles()).databaseConfigFile;
var databaseConfigFile = require(databasePathJson);
this.configDatabase = databaseConfigFile[this.env];
};
/**
* @public Database connection
*/
Database.prototype.connectDatabse = function () {
this.loadModules();
this.db.sequelize = this.sequelize;
this.db.Sequelize = Sequelize;
Database.models = this.db;
};
/**
* @private Load Models
*/
Database.prototype.loadModules = function () {
var _this = this;
if (!FS.existsSync(Database.modulePath)) {
return;
}
var loadFiles = FS.readdirSync(Database.modulePath);
var loadFilesFilter = loadFiles.filter(function (file) {
return (file.indexOf('.') != 0) && (file != _this._basename);
});
loadFilesFilter.forEach(function (file) {
if (file.slice(-3) != '.js') {
return;
}
var model = _this.sequelize['import'](Path.join(Database.modulePath, file));
_this.db[model.name] = model;
});
Object.keys(this.db).forEach(function (modelName) {
if ('associate' in _this.db[modelName]) {
_this.db[modelName].associate(_this.db);
}
});
};
/**
* @static Module path
* @type {string}
*/
Database.modulePath = __dirname + '/../../app/database';
return Database;
})();
Dollyjs.Database = Database;
})(Dollyjs || (Dollyjs = {}));
module.exports = Dollyjs;
//# sourceMappingURL=Database.js.map