加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
db.js 904 Bytes
一键复制 编辑 原始数据 按行查看 历史
橘子皮儿 提交于 2018-07-17 14:55 . first commit
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
//var config = require('../config').database;
//console.log("into db")
var db = null;
module.exports = app => {
if (!db) {
const config = app.libs.config.database;
//console.log(config)
const sequelize = new Sequelize(config.database, config.username, config.password, config);
const dir = path.join(__dirname, 'models');
db = {
sequelize, Sequelize
};
fs.readdirSync(path.join(dir)).filter(function (file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
}).forEach(function (file) {
//console.log(path.join(dir, file))
var model = sequelize.import(path.join(dir, file));
db[model.name] = model;
});
Object.keys(db).forEach(function (modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
}
return db;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化