The first time I used sequelize.js as the ORM tool for database connection in my project, WHEN I was writing my first Model, I realized that this kind of thing was fixed and could be generated automatically. Fortunately, I found a sequelize-Automate library that can automatically generate Model code by reading database table definitions. However, WHEN the table definition is updated and needs to be changed by the Model, I often repeat the build command and copy the code into the project. A study, the following optimization version was born ~~

sequelize-automate-gm

Automatically generates and updates Model code for the famous ORM framework, Sequelizejs. This project is based on Sequelize-Automate. The new and optimized contents are as follows:

  • New shell interactions make it easier to use.
  • Automatic incremental updates that map library table changes directly to Model code without affecting your own changes in the Model.
  • Ts Model generation writes the structure definition directly inside the Model class and does not generate the *.d.ts definition file.
  • Fixed some TS ESLint bugs.
  • Support ES6, commonJS two modular code.

Important note: Autogenerated models still need to be reviewed to avoid negative impact on your code due to unknown issues with the tool.

The installation

Global installation

npm install -g sequelize-automate-gm
Copy the code

In-project installation

npm install sequelize-automate-gm
Copy the code

use

Step 1: Configuration files

Module. exports = {"dbOptions": {// sequelize database config "database": "db_test", // database name" username": {// module. Exports = {"dbOptions": {// sequelize database config ": {// database name" username": "Guess ", // user "password": "guess", // password" dialect": "mysql", // database dialect" host": "localhost", // database service address "port": 3306, // Database service port number "define": {" corrupt ": true, "freezeTableName": true,}}, "options": {// Generate file configuration information "type": // Generate code type, support ts, js" dir": "./test/demo/model", // generate code file output location "moduleType": "Commonjs ", // generate code in a modular way, support commonJS, ES6 "sequelizeName": 'Sequelize', // Sequelize Specifies the name of the variable introduced into the Sequelize module. The default value is Sequelize "sequelizeModulePath": 'sequelize', // sequelize Introduces the module of the sequelize module. The default sequelize is "sequelizeInsName": 'sequelizeClient', // Sequelize instance variable name, default sequelizeClient "sequelizeInsModulePath": '.. Js ', // module path of the sequelize instance. No default value. This parameter is mandatory. }}Copy the code

SequelizeName, sequelizeModulePath, sequelizeInsName, and sequelizeInsModulePath are configured for projects that encapsulate Sequelize. When generating a Model, you need to name variables and specify module paths according to your needs. SequelizeInsModulePath is mandatory because I have assumed that your project must encapsulate the instantiation of Sequelize.

Step 2: Execute the command

sagm -c /path/to/config
Copy the code

sagm = Sequelize Automate Generate Model

Running effect

Interactive page

Select the Model that generates part of the table or all of the table

Select the table to generate, the space check

Choose between incremental updates (reading existing files and doing incremental updates) or full replacement (replacing existing ones completely with newly generated code)

The generated code

// javascript const Sequelize = require('Sequelize'); const sequelizeClient = require('.. /lib/sequelize.js'); const { DataTypes, Model } = Sequelize; class Student extends Model {} const attributes = { id: { type: DataTypes.INTEGER, allowNull: false, defaultValue: null, primaryKey: true, autoIncrement: true, comment: '', field: 'id' }, name: { type: DataTypes.STRING(45), allowNull: true, defaultValue: null, primaryKey: false, autoIncrement: false, comment: '', field: 'name' }, age: { type: DataTypes.INTEGER, allowNull: true, defaultValue: null, primaryKey: false, autoIncrement: false, comment: '', field: 'age' }, class: { type: DataTypes.STRING(45), allowNull: true, defaultValue: null, primaryKey: false, autoIncrement: false, comment: '', field: 'class' } }; const options = { modelName: 'student', sequelize: sequelizeClient, indexes: [] }; Student.init(attributes, options); module.exports = Student;Copy the code
// typescript const Sequelize = require('Sequelize'); const sequelizeClient = require('.. /lib/sequelize.js'); const { DataTypes, Model } = Sequelize; class Student extends Model { id! : number; name? : string; age? : number; class? : string; } const attributes = { id: { type: DataTypes.INTEGER, allowNull: false, defaultValue: null, primaryKey: true, autoIncrement: true, comment: '', field: 'id' }, name: { type: DataTypes.STRING(45), allowNull: true, defaultValue: null, primaryKey: false, autoIncrement: false, comment: '', field: 'name' }, age: { type: DataTypes.INTEGER, allowNull: true, defaultValue: null, primaryKey: false, autoIncrement: false, comment: '', field: 'age' }, class: { type: DataTypes.STRING(45), allowNull: true, defaultValue: null, primaryKey: false, autoIncrement: false, comment: '', field: 'class' } }; const options = { modelName: 'student', sequelize: sequelizeClient, indexes: [] }; Student.init(attributes, options); module.exports = Student;Copy the code