{"id":16582667,"url":"https://github.com/nodejh/sequelize-automate","last_synced_at":"2025-04-05T04:14:39.745Z","repository":{"id":36473194,"uuid":"227136161","full_name":"nodejh/sequelize-automate","owner":"nodejh","description":"Automatically generate bare sequelize models from your database.","archived":false,"fork":false,"pushed_at":"2022-04-09T06:07:44.000Z","size":1347,"stargazers_count":114,"open_issues_count":38,"forks_count":21,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-22T11:48:04.728Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nodejh.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-10T14:07:28.000Z","updated_at":"2024-10-23T09:16:36.000Z","dependencies_parsed_at":"2022-08-08T15:01:30.773Z","dependency_job_id":null,"html_url":"https://github.com/nodejh/sequelize-automate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejh%2Fsequelize-automate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejh%2Fsequelize-automate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejh%2Fsequelize-automate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejh%2Fsequelize-automate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nodejh","download_url":"https://codeload.github.com/nodejh/sequelize-automate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246763805,"owners_count":20829795,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T22:33:42.977Z","updated_at":"2025-04-05T04:14:39.720Z","avatar_url":"https://github.com/nodejh.png","language":"JavaScript","readme":"# Sequelize-Automate\n\n[![NPM version][npm-image]][npm-url]\n[![npm download][download-image]][download-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n\u003c!-- [![Greenkeeper badge](https://badges.greenkeeper.io/nodejh/sequelize-automate.svg)](https://greenkeeper.io/) --\u003e\n\u003c!-- [![Build Status](http://img.shields.io/travis/nodejh/sequelize-automate/master.svg)](https://travis-ci.org/nodejh/sequelize-automate) --\u003e\n\u003c!-- [![Dependency Status](https://david-dm.org/nodejh/sequelize-automate.svg)](https://david-dm.org/nodejh/sequelize-automate) --\u003e\n\n\n\u003c!-- [![Test Coverage](https://codeclimate.com/github/nodejh/sequelize-automate/badges/coverage.svg)](https://codeclimate.com/github/nodejh/sequelize-automate/coverage) --\u003e\n\n[npm-image]: https://img.shields.io/npm/v/sequelize-automate.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/sequelize-automate\n[travis-image]: https://img.shields.io/travis/nodejh/sequelize-automate.svg?style=flat-square\n[travis-url]: https://travis-ci.org/nodejh/sequelize-automate\n[codecov-image]: https://img.shields.io/codecov/c/github/nodejh/sequelize-automate.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/nodejh/sequelize-automate?branch=master\n[david-image]: https://img.shields.io/david/nodejh/sequelize-automate.svg?style=flat-square\n[david-url]: https://david-dm.org/nodejh/sequelize-automate\n[snyk-image]: https://snyk.io/test/npm/sequelize-automate/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/sequelize-automate\n[download-image]: https://img.shields.io/npm/dm/sequelize-automate.svg?style=flat-square\n[download-url]: https://npmjs.org/package/sequelize-automate\n\n\nAutomatically generate models for [SequelizeJS](https://github.com/sequelize/sequelize). Support javascript, typescript, egg.js and midway.\n\n## Installing\n\n### global\n\n```shell script\n$ npm install -g sequelize-automate\n```\n\nYou'll also have to manually install the driver for your database of choice:\n\n```shell script\n# One of the following:\n$ npm install -g pg pg-hstore # Postgres\n$ npm install -g mysql2\n$ npm install -g mariadb\n$ npm install -g sqlite3\n$ npm install -g tedious # Microsoft SQL Server\n```\n\n### in project\n\n```shell script\n$ npm install sequelize-automate --save\n```\n\nYou'll also have to manually install the driver for your database of choice:\n\n```shell script\n# One of the following:\n$ npm install --save pg pg-hstore # Postgres\n$ npm install --save mysql2\n$ npm install --save mariadb\n$ npm install --save sqlite3\n$ npm install --save tedious # Microsoft SQL Server\n```\n\n\n## Usage\n\n### Command Line\n\n```shell script\nUsage: sequelize-automate -t [type] -h \u003chost\u003e -d \u003cdatabase\u003e -u \u003cuser\u003e -p [password] -P [port]  -e [dialect] -o [/path/to/models] -c [/path/to/config]\n\nOptions:\n  --version       Show version number                                  [boolean]\n  --help          Show help                                            [boolean]\n  --type, -t      Which code style want to generate.\n                           [choices: \"js\", \"ts\", \"egg\", \"midway\", \"@ali/midway\"]\n  --host, -h      IP/Hostname for the database.  [string] [default: \"localhost\"]\n  --database, -d  Database name.                      [string] [default: \"test\"]\n  --user, -u      Username for database.              [string] [default: \"root\"]\n  --password, -p  Password for database.              [string] [default: \"root\"]\n  --port, -P      Port number for database. e.g. MySQL/MariaDB: 3306, Postgres:\n                  5432, MSSQL: 1433                                     [number]\n  --dialect, -e   The dialect/engine that you're using: mysql, sqlite, postgres,\n                  mssql\n            [choices: \"mysql\", \"sqlite\", \"postgres\", \"mssql\"] [default: \"mysql\"]\n  --output, -o    What directory to place the models.\n                                                    [string] [default: \"models\"]\n  --camel, -C     Use camel case to name models       [boolean] [default: false]\n  --config, -c    Sequelize automate config file, see README.md         [string]\n  --emptyDir, -r  Remove all files in `dir` and `typesDir` directories before\n                  generate models.                    [boolean] [default: false]\n  --match, -m     Match tables using given RegExp.    [string] [default: null]\n```\n\n#### Example\n\n```shell script\n$ sequelize-automate -t js -h localhost -d test -u root -p root -P 3306  -e mysql -o models\n```\n\nProduces a file/files such as ./models/user.js which looks like:\n\n```javascript\nconst {\n  DataTypes\n} = require('sequelize');\n\nmodule.exports = sequelize =\u003e {\n  const attributes = {\n    id: {\n      type: DataTypes.INTEGER(11).UNSIGNED,\n      allowNull: false,\n      defaultValue: null,\n      primaryKey: true,\n      autoIncrement: true,\n      comment: \"primary ket\",\n      field: \"id\"\n    },\n    name: {\n      type: DataTypes.STRING(100),\n      allowNull: false,\n      defaultValue: null,\n      primaryKey: false,\n      autoIncrement: false,\n      comment: \"user name\",\n      field: \"name\",\n      unique: \"uk_name\"\n    },\n    email: {\n      type: DataTypes.STRING(255),\n      allowNull: false,\n      defaultValue: null,\n      primaryKey: false,\n      autoIncrement: false,\n      comment: \"user email\",\n      field: \"email\"\n    },\n    createdAt: {\n      type: DataTypes.DATE,\n      allowNull: false,\n      defaultValue: null,\n      primaryKey: false,\n      autoIncrement: false,\n      comment: \"created datetime\",\n      field: \"created_at\"\n    },\n    updatedAt: {\n      type: DataTypes.DATE,\n      allowNull: false,\n      defaultValue: null,\n      primaryKey: false,\n      autoIncrement: false,\n      comment: \"updated datetime\",\n      field: \"updated_at\"\n    }\n  };\n  const options = {\n    tableName: \"user\",\n    comment: \"\",\n    indexes: []\n  };\n  const UserModel = sequelize.define(\"userModel\", attributes, options);\n  return UserModel;\n};\n```\n\nWhich makes it easy for you to simply [Sequelize.import](https://sequelize.org/master/manual/models-definition.html#import) it.\n\n#### Configuration options\n\nYou can use `-c, --config` option to specify a configuration file.\n\n```shell script\n$ sequelize-automate -c \"./sequelize-automate.config.json\"\n```\n\nFor now, you must create a file called `sequelize-automate.config.json` with the following content:\n\n```json\n{\n  \"dbOptions\": {\n    \"database\": \"test\",\n    \"username\": \"root\",\n    \"password\": \"root\",\n    \"dialect\": \"mysql\",\n    \"host\": \"localhost\",\n    \"port\": 3306,\n    \"logging\": false\n  },\n  \"options\": {\n    \"type\": \"js\",\n    \"dir\": \"models\"\n  }\n}\n```\n\nOr a `.js` file: `sequelize-automate -c \"./sequelize-automate.config.js\"`\n\n```javascript\nmodule.exports = {\n  dbOptions: {\n    database: \"test\",\n    username: \"root\",\n    password: \"root\",\n    dialect: \"mysql\",\n    host: \"localhost\",\n    port: 3306,\n    logging: false\n  },\n  options: {\n    type: \"js\",\n    dir: \"models\"\n }\n}\n```\n\n#### In project\n\nAlso, you can use `sequelize-automate` in project.\n\nFirst add a configuration file `sequelize-automate.config.json` as above and add `automate` script to `package.json`:\n\n```json\n\"script\": {\n  \"automate\": \"sequelize-automate -c sequelize-automate.config.json\"\n}\n```\n\nThen you can use `npm run automate` to generate models.\n\n\n## Programmatic API\n\n```javascript\nconst Automate = require('sequelize-automate');\n\n// Database options, is the same with sequelize constructor options.\nconst dbOptions = {\n  database: 'test',\n  username: 'root',\n  password: 'root',\n  dialect: 'mysql',\n  host: '127.0.0.1',\n  port: 3306,\n  define: {\n    underscored: false,\n    freezeTableName: false,\n    charset: 'utf8mb4',\n    timezone: '+00:00',\n    dialectOptions: {\n      collate: 'utf8_general_ci',\n    },\n    timestamps: false,\n  },\n};\n\n// Automate options\nconst options = {\n  type: 'js', // Which code style want to generate, supported: js/ts/egg/midway. Default is `js`.\n  camelCase: false, // Model name camel case. Default is false.\n  fileNameCamelCase: true, // Model file name camel case. Default is false.\n  dir: 'models', // What directory to place the models. Default is `models`.\n  typesDir: 'models', // What directory to place the models' definitions (for typescript), default is the same with dir.\n  emptyDir: false, // Remove all files in `dir` and `typesDir` directories before generate models.\n  tables: null, // Use these tables, Example: ['user'], default is null.\n  skipTables: null, // Skip these tables. Example: ['user'], default is null.\n  tsNoCheck: false, // Whether add @ts-nocheck to model files, default is false.\n  match: null // RegExp to match table name\n}\n\nconst automate = new Automate(dbOptions, options);\n\n(async function main() {\n  // // get table definitions\n  // const definitions = await automate.getDefinitions();\n  // console.log(definitions);\n\n  // or generate codes\n  const code = await automate.run();\n  console.log(code);\n})()\n```\n\nDatabase 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](https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor).\n\n### Methods\n\n- `automate.getDefinitions()`: Get all model definitions. `sequelize-automate` will use these definitions to generate different codes.\n- `automate.run()`: Generate model codes.\n\n\n## Type\n\nYou can generate different (node.js framework's) codes use `type` option.\n\n### JavaScript\n\n```shell script\n$ sequelize-automate -t js\n```\n\n[example](https://github.com/nodejh/sequelize-automate/tree/master/src/generate/template/javascript)\n\n### TypeScript\n\n```shell script\n$ sequelize-automate -t ts\n```\n\n[example](https://github.com/nodejh/sequelize-automate/tree/master/src/generate/template/typescript)\n\n### Egg.js\n\n```shell script\n$ sequelize-automate -t egg\n```\n\n[example](https://github.com/nodejh/sequelize-automate/tree/master/src/generate/template/egg)\n\n\n### Midway.js\n\n```shell script\n$ sequelize-automate -t midway\n```\n\n[example](https://github.com/nodejh/sequelize-automate/tree/master/src/generate/template/midway)\n\nIf you want to generate codes for other frameworks, please let me know.\n\n## LICENSE\n\n[MIT](https://github.com/nodejh/sequelize-automate/blob/master/LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodejh%2Fsequelize-automate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnodejh%2Fsequelize-automate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodejh%2Fsequelize-automate/lists"}