Sequelize-Automate
Automatically generate models for SequelizeJS. Support javascript, typescript, egg.js and midway.
Installing
global
$ npm install -g sequelize-automate
You'll also have to manually install the driver for your database of choice:
# One of the following: $ npm install -g pg pg-hstore # Postgres $ npm install -g mysql2$ npm install -g mariadb$ npm install -g sqlite3$ npm install -g tedious # Microsoft SQL Server
in poroject
$ npm install sequelize-automate --save
You'll also have to manually install the driver for your database of choice:
# One of the following: $ npm install --save pg pg-hstore # Postgres $ npm install --save mysql2$ npm install --save mariadb$ npm install --save sqlite3$ npm install --save tedious # Microsoft SQL Server
Usage
Command Line
Usage: sequelize-automate -t [type] -h <host> -d <database> -u <user> -p [password] -P [port] -e [dialect] -o [/path/to/models] -c [/path/to/config] Options: --version Show version number [boolean] --help Show help [boolean] --type, -t Which code style want to generate. [choices: "js", "ts", "egg", "midway", "@ali/midway"] --host, -h IP/Hostname
Example
$ sequelize-automate -t js -h localhost -d test -u root -p root -P 3306 -e mysql -o models
Produces a file/files such as ./models/user.js which looks like:
const DataTypes } = ; module { const attributes = id: type: DataTypesUNSIGNED allowNull: false defaultValue: null primaryKey: true autoIncrement: true comment: "primary ket" field: "id" name: type: DataTypes allowNull: false defaultValue: null primaryKey: false autoIncrement: false comment: "user name" field: "name" unique: "uk_name" email: type: DataTypes allowNull: false defaultValue: null primaryKey: false autoIncrement: false comment: "user email" field: "email" createdAt: type: DataTypesDATE allowNull: false defaultValue: null primaryKey: false autoIncrement: false comment: "created datetime" field: "created_at" updatedAt: type: DataTypesDATE allowNull: false defaultValue: null primaryKey: false autoIncrement: false comment: "updated datetime" field: "updated_at" ; const options = tableName: "user" comment: "" indexes: ; const UserModel = sequelize; return UserModel;};
Which makes it easy for you to simply Sequelize.import it.
Configuration options
You can use -c, --config
option to specify a configuration file.
$ sequelize-automate -c "./sequelize-automate.config.json"
For now, you must create a file called sequelize-automate.config.json
with the following content:
Or a .js
file: sequelize-automate -c "./sequelize-automate.config.js"
moduleexports = dbOptions: database: "test" username: "root" password: "root" dialect: "mysql" host: "localhost" port: 3306 logging: false options: type: "js" dir: "models"
In project
Also, you can use sequelize-automate
in project.
First add a configuration file sequelize-automate.config.json
as above and add automate
script to package.json
:
"script":
Then you can use npm run automate
to generate models.
Programmatic API
const Automate = ; // Database options, is the same with sequelize constructor options.const dbOptions = database: 'test' username: 'root' password: 'root' dialect: 'mysql' host: '127.0.0.1' port: 3306 define: underscored: false freezeTableName: false charset: 'utf8mb4' timezone: '+00:00' dialectOptions: collate: 'utf8_general_ci' timestamps: false ; // Automate optionsconst options = type: 'js' // Which code style want to generate, supported: js/ts/egg/midway. Default is `js`. camelCase: false // Model name camel case. Default is false. fileNameCamelCase: true // Model file name camel case. Default is false. dir: 'models' // What directory to place the models. Default is `models`. typesDir: 'models' // What directory to place the models' definitions (for typescript), default is the same with dir. emptyDir: false // Remove all files in `dir` and `typesDir` directories before generate models. tables: null // Use these tables, Example: ['user'], default is null. skipTables: null // Skip these tables. Example: ['user'], default is null. tsNoCheck: false // Whether add @ts-nocheck to model files, default is false. const automate = dbOptions options; { // // get table definitions // const definitions = await automate.getDefinitions(); // console.log(definitions); // or generate codes const code = await automate; console;}
Database options dbOptions
is the same with sequelize constructor options, you can find all options here: https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor.
Methods
automate.getDefinitions()
: Get all model definitions.sequelize-automate
will use these definitions to generate different codes.automate.run()
: Generate model codes.
Type
You can generate different (node.js framework's) codes use type
option.
JavaScript
$ sequelize-automate -t js
TypeScript
$ sequelize-automate -t ts
Egg.js
$ sequelize-automate -t egg
Midway.js
$ sequelize-automate -t midway
If you want to generate codes for other frameworks, please let me know.