const Sequelize = require('sequelize');
const settings = require("../../../config/settings")
const fs = require("fs")
const path = require("path");
class DbFactory {
  constructor() {
    const dbConfig = settings.database();
    this.db = new Sequelize(dbConfig.dbname,
      dbConfig.user,
      dbConfig.password,
      dbConfig.config);

    this.db.Sequelize = Sequelize;
    this.db.Op = Sequelize.Op;

    this.initModels();
    this.initRelations();
  }
  async initModels() {
    var self = this;
    var modelpath = path.normalize(path.join(__dirname, '..')) + "/models/";
    var models = fs.readdirSync(modelpath);
    models.forEach(function (m) {
      self.db.import(modelpath + m);
    });
    console.log("init models....");
  }
  async initRelations() {
  }
  //async getCon(){,用于使用替换table模型内字段数据使用
  getCon() {
    var that = this;
    // await this.db.authenticate().then(()=>{
    //   console.log('Connection has been established successfully.');
    // }).catch(err => {
    //   console.error('Unable to connect to the database:', err);
    //   throw err;
    // });
    //同步模型
    if (settings.env == "dev") {}
    return this.db;
  }

  getSettleDBCon() {
    var that = this;
    if (settings.env == "dev") {}
    return this.settledb;
  }

}
module.exports = DbFactory;
