Skip to content

Automatically generate bare sequelize models from your database.

License

Notifications You must be signed in to change notification settings

nodejh/sequelize-automate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sequelize-Automate

NPM version npm download build status Test coverage David deps Known Vulnerabilities

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 project

$ 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 for the database.  [string] [default: "localhost"]
  --database, -d  Database name.                      [string] [default: "test"]
  --user, -u      Username for database.              [string] [default: "root"]
  --password, -p  Password for database.              [string] [default: "root"]
  --port, -P      Port number for database. e.g. MySQL/MariaDB: 3306, Postgres:
                  5432, MSSQL: 1433                                     [number]
  --dialect, -e   The dialect/engine that you're using: mysql, sqlite, postgres,
                  mssql
            [choices: "mysql", "sqlite", "postgres", "mssql"] [default: "mysql"]
  --output, -o    What directory to place the models.
                                                    [string] [default: "models"]
  --camel, -C     Use camel case to name models       [boolean] [default: false]
  --config, -c    Sequelize automate config file, see README.md         [string]
  --emptyDir, -r  Remove all files in `dir` and `typesDir` directories before
                  generate models.                    [boolean] [default: false]
  --match, -m     Match tables using given RegExp.    [string] [default: null]

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
} = require('sequelize');

module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.INTEGER(11).UNSIGNED,
      allowNull: false,
      defaultValue: null,
      primaryKey: true,
      autoIncrement: true,
      comment: "primary ket",
      field: "id"
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      defaultValue: null,
      primaryKey: false,
      autoIncrement: false,
      comment: "user name",
      field: "name",
      unique: "uk_name"
    },
    email: {
      type: DataTypes.STRING(255),
      allowNull: false,
      defaultValue: null,
      primaryKey: false,
      autoIncrement: false,
      comment: "user email",
      field: "email"
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: null,
      primaryKey: false,
      autoIncrement: false,
      comment: "created datetime",
      field: "created_at"
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: null,
      primaryKey: false,
      autoIncrement: false,
      comment: "updated datetime",
      field: "updated_at"
    }
  };
  const options = {
    tableName: "user",
    comment: "",
    indexes: []
  };
  const UserModel = sequelize.define("userModel", attributes, options);
  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:

{
  "dbOptions": {
    "database": "test",
    "username": "root",
    "password": "root",
    "dialect": "mysql",
    "host": "localhost",
    "port": 3306,
    "logging": false
  },
  "options": {
    "type": "js",
    "dir": "models"
  }
}

Or a .js file: sequelize-automate -c "./sequelize-automate.config.js"

module.exports = {
  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": {
  "automate": "sequelize-automate -c sequelize-automate.config.json"
}

Then you can use npm run automate to generate models.

Programmatic API

const Automate = require('sequelize-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 options
const 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.
  match: null // RegExp to match table name
}

const automate = new Automate(dbOptions, options);

(async function main() {
  // // get table definitions
  // const definitions = await automate.getDefinitions();
  // console.log(definitions);

  // or generate codes
  const code = await automate.run();
  console.log(code);
})()

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

example

TypeScript

$ sequelize-automate -t ts

example

Egg.js

$ sequelize-automate -t egg

example

Midway.js

$ sequelize-automate -t midway

example

If you want to generate codes for other frameworks, please let me know.

LICENSE

MIT

About

Automatically generate bare sequelize models from your database.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published