Built motion from commit 95b01fa.|0.0.70
[motion.git] / server / models / index.js
1 "use strict";
2
3 var fs = require("fs");
4 var path = require("path");
5 var config = require('../config/environment');
6 var Sequelize = require("sequelize");
7 var moment = require('moment');
8
9 //Used to disable all warnings for Promise!
10 Sequelize.Promise.config({
11   // Enables all warnings except forgotten return statements.
12   warnings: {
13     wForgottenReturn: false
14   }
15 });
16
17 var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
18   host: config.db.host,
19   dialect: 'mysql',
20   timezone: moment().format("Z"),
21   logging: false
22 });
23
24 var db = {};
25
26 fs
27   .readdirSync(__dirname)
28   .filter(function(file) {
29     return (fs.lstatSync(path.join(__dirname, file)).isFile()) &&
30       (file.indexOf(".") !== 0) && (file.indexOf('.spec.') === -1) && (file !== "index.js");
31   })
32   .forEach(function(file) {
33     var model = sequelize["import"](path.join(__dirname, file));
34     db[model.name] = model;
35   });
36
37 // Invoke associations on each of the models
38 Object.keys(db).forEach(function(modelName) {
39   if (db[modelName].options.hasOwnProperty('associate')) {
40     db[modelName].options.associate(db);
41   }
42 });
43
44 db.history = {};
45
46 var sequelize_history = new Sequelize(config.db_history.database, config.db_history.username, config.db_history.password, {
47   host: config.db_history.host,
48   dialect: 'mysql',
49   timezone: moment().format("Z"),
50   logging: false
51 });
52
53 fs
54   .readdirSync(path.join(__dirname, 'history'))
55   .filter(function(file) {
56     return (fs.lstatSync(path.join(__dirname, 'history', file)).isFile()) &&
57       (file.indexOf(".") !== 0) && (file.indexOf('.spec.') === -1) && (file !== "index.js");
58   })
59   .forEach(function(file) {
60     var model = sequelize_history["import"](path.join(__dirname, 'history', file));
61     db.history[model.name] = model;
62   });
63
64 // Invoke associations on each of the models
65 Object.keys(db.history).forEach(function(modelName) {
66   if (db.history[modelName].options.hasOwnProperty('associate')) {
67     db.history[modelName].options.associate(db.history);
68   }
69 });
70
71 db.sequelize = sequelize;
72 db.sequelize_history = sequelize_history;
73 db.Sequelize = Sequelize;
74
75 module.exports = db;