67b8bff0ecf7083dc5a08a147c783bf10c3a70d0
[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 if (config.timezone && config.timezone == 'xcally-motion-timezone') {
18   config.timezone = '';
19 }
20
21 var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
22   host: config.db.host,
23   dialect: 'mysql',
24   timezone: config.timezone || moment().format("Z"),
25   logging: false,
26   define: {
27     charset: 'utf8',
28     collate: 'utf8_general_ci'
29   }
30 });
31
32 var db = {};
33
34 fs
35   .readdirSync(__dirname)
36   .filter(function(file) {
37     return (fs.lstatSync(path.join(__dirname, file)).isFile()) &&
38       (file.indexOf(".") !== 0) && (file.indexOf('.spec.') === -1) && (file !== "index.js");
39   })
40   .forEach(function(file) {
41     var model = sequelize["import"](path.join(__dirname, file));
42     db[model.name] = model;
43   });
44
45 // Invoke associations on each of the models
46 Object.keys(db).forEach(function(modelName) {
47   if (db[modelName].options.hasOwnProperty('associate')) {
48     db[modelName].options.associate(db);
49   }
50 });
51
52 db.history = {};
53
54 var sequelize_history = new Sequelize(config.db_history.database, config.db_history.username, config.db_history.password, {
55   host: config.db_history.host,
56   dialect: 'mysql',
57   timezone: config.timezone || moment().format("Z"),
58   logging: false,
59   define: {
60     charset: 'utf8',
61     collate: 'utf8_general_ci'
62   }
63 });
64
65 fs
66   .readdirSync(path.join(__dirname, 'history'))
67   .filter(function(file) {
68     return (fs.lstatSync(path.join(__dirname, 'history', file)).isFile()) &&
69       (file.indexOf(".") !== 0) && (file.indexOf('.spec.') === -1) && (file !== "index.js");
70   })
71   .forEach(function(file) {
72     var model = sequelize_history["import"](path.join(__dirname, 'history', file));
73     db.history[model.name] = model;
74   });
75
76 // Invoke associations on each of the models
77 Object.keys(db.history).forEach(function(modelName) {
78   if (db.history[modelName].options.hasOwnProperty('associate')) {
79     db.history[modelName].options.associate(db.history);
80   }
81 });
82
83 db.sequelize = sequelize;
84 db.sequelize_history = sequelize_history;
85 db.Sequelize = Sequelize;
86
87 module.exports = db;