2016-04-20 10:03:55 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// external modules
|
|
|
|
var fs = require("fs");
|
|
|
|
var path = require("path");
|
|
|
|
var Sequelize = require("sequelize");
|
|
|
|
|
|
|
|
// core
|
|
|
|
var config = require('../config.js');
|
|
|
|
var logger = require("../logger.js");
|
|
|
|
|
|
|
|
var dbconfig = config.db;
|
|
|
|
dbconfig.logging = config.debug ? logger.info : false;
|
2016-12-12 02:18:12 +00:00
|
|
|
|
2016-12-12 03:12:59 +00:00
|
|
|
var sequelize = null;
|
2016-12-22 05:23:17 +00:00
|
|
|
|
2016-12-22 11:42:00 +00:00
|
|
|
// Heroku specific
|
2016-12-22 13:20:43 +00:00
|
|
|
if (config.dburl)
|
|
|
|
sequelize = new Sequelize(config.dburl, dbconfig);
|
2016-12-12 03:12:59 +00:00
|
|
|
else
|
2016-12-22 05:23:17 +00:00
|
|
|
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig);
|
2016-04-20 10:03:55 +00:00
|
|
|
|
2017-01-02 03:05:05 +00:00
|
|
|
// [Postgres] Handling NULL bytes
|
|
|
|
// https://github.com/sequelize/sequelize/issues/6485
|
|
|
|
function stripNullByte(value) {
|
|
|
|
return value ? value.replace(/\u0000/g, "") : value;
|
|
|
|
}
|
|
|
|
sequelize.stripNullByte = stripNullByte;
|
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
var db = {};
|
|
|
|
|
|
|
|
fs
|
|
|
|
.readdirSync(__dirname)
|
|
|
|
.filter(function (file) {
|
|
|
|
return (file.indexOf(".") !== 0) && (file !== "index.js");
|
|
|
|
})
|
|
|
|
.forEach(function (file) {
|
|
|
|
var model = sequelize.import(path.join(__dirname, file));
|
|
|
|
db[model.name] = model;
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(db).forEach(function (modelName) {
|
|
|
|
if ("associate" in db[modelName]) {
|
|
|
|
db[modelName].associate(db);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
db.sequelize = sequelize;
|
|
|
|
db.Sequelize = Sequelize;
|
|
|
|
|
2016-12-12 02:18:12 +00:00
|
|
|
module.exports = db;
|