• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically generate bare sequelize models from your database.

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

More Repositories

1

hugo-theme-mini

A fast, minimalist and responsive hugo theme for bloggers.
HTML
727
star
2

nodejh.github.io

Blog
HTML
267
star
3

qqbot

QQ 聊天机器人
JavaScript
21
star
4

vue2-tutorials

Vue2 tutorials
JavaScript
14
star
5

serverless-class

JavaScript
10
star
6

egg-plugin-graphql

egg graphql plugin
JavaScript
10
star
7

electron-quick-start

JavaScript
8
star
8

teaching_evaluation

一键评教
JavaScript
8
star
9

urp_scu

好看又好用的非官方教务系统
JavaScript
8
star
10

hexo-documentation

6
star
11

scugpa

四川大学绩点计算器
JavaScript
5
star
12

express-example

Express 示例网站
JavaScript
2
star
13

scu_api

四川大学校园 API
JavaScript
2
star
14

typecho-theme-bing

a typecho them with bing images
PHP
2
star
15

huobi-sdk

SDK of huobi
JavaScript
2
star
16

typecho-theme-minimal

极简的 typecho 主题
CSS
2
star
17

docker-go-server-ping

一个简单的 Go 服务
Go
2
star
18

koa-init

Initial koa project
JavaScript
1
star
19

demo-react-virtualized

some demos of react-virtualized
JavaScript
1
star
20

examples

examples
Java
1
star
21

disk_web

network disk website
JavaScript
1
star
22

library_system

图书馆管理系统
HTML
1
star
23

SearchingAlgorithm

SearchingAlgorithm
JavaScript
1
star
24

node-cli-translate

📓 A simple command line interface translate tool by node.js
JavaScript
1
star
25

http-errors-status-code

http-errors status code
JavaScript
1
star
26

friendstrip

友你伴我旅游APP
Java
1
star
27

xiaoxiami

Xiami music client
JavaScript
1
star
28

nodejs-google-translate

google translation
JavaScript
1
star
29

sublime-fix-bug

解决 Sublime Text 3在 Ubuntu 下不能输入中文的问题
1
star
30

react-draft-wysiwyg

A Wysiwyg editor built using ReactJS and DraftJS libraries.
JavaScript
1
star