sequelize-auto根据表生成表模型

周滨海
2023-12-01

1.同步表生成表模型
auto.js

var SequelizeAuto = require('sequelize-auto')

//各个数据库配置
let dbConfig = [
  {
    name: 'fangbao', auto: true, adapter: 'sails-mysql', host: '127.0.0.1', port: 3306, user: 'root', password: '123456', database: 'houseinsurancelinkage_test', modelPath: './dbStorage/fangbao/',
  },
  {
    name: 'linkage', host: '127.0.0.1', port: 3306, user: 'root', password: '123456', database: 'linkagesystem_test', modelPath: './dbStorage/linkage/',
  }
]

for (const data of dbConfig) {
  let auto = new SequelizeAuto(data.database, data.user, data.password, {
    host: data.host,
    dialect: 'mysql',
    directory: data.modelPath, // prevents the program from writing to disk
    port: data.port,
    // caseModel: 'c', // convert snake_case column names to camelCase field names: user_id -> userId
    caseFile: 'c', // file names created for each model use camelCase.js not snake_case.js
    singularize: true, // convert plural table names to singular model names
    additional: {
      // freezeTableName: true,
      //启动默认时间戳
      timestamps: true,
      // 自定义创建时间和修改时间字段
      updatedAt: 'gmt_modified',
      createdAt: 'gmt_create'
    }
});
auto.run(function (err) {
  if (err) throw err;
  console.log(auto.tables); // table list
  console.log(auto.foreignKeys); // foreign key list
});
}

2.执行 node auto.js

 类似资料: