Automatically generate bare sequelize models from your database.
Automatically generate bare sequelize models from your database.
Automatically generate models for SequelizeJS via the command line.
npm install sequelize-autoYou will need to install sequelize; it's no longer installed by sequelize-auto.
You will need to install the correct dialect binding before using sequelize-auto.
| Dialect | Install |
|---|---|
| MySQL/MariaDB | npm install sequelize mysql2 |
| Postgres | npm install sequelize pg pg-hstore |
| Sqlite | npm install sequelize sqlite3 |
| MSSQL | npm install sequelize tedious |
sequelize-auto -h <host> -d <database> -u <user> -x [password] -p [port] --dialect [dialect] -c [/path/to/config] -o [/path/to/models] -t [tableName]…On Windows, provide the path to sequelize-auto:
node_modules\.bin\sequelize-auto [args]
sequelize-auto -o "./models" -d sequelize_auto_test -h localhost -u my_username -p 5432 -x my_password -e postgresProduces a file/files such as ./models/User.js which looks like:
…Sequelize-auto also generates an initialization file, ./models/init-models.js, which contains the code to load each model definition into Sequelize:
var DataTypes = require("sequelize").DataTypes;
var _User = require("./User");
var _Product = require("./Product");
function initModels(sequelize) {
var User = _User(sequelize, DataTypes);
var Product = _Product(sequelize, DataTypes);
return {
User,
Product,
};
}
module.exports = { initModels };This makes it easy to import all your models into Sequelize by calling initModels(sequelize).
var initModels = require("./models/init-models");
...
var models = initModels(sequelize);
models.User.findAll({ where: { username: "tony" }}).then(...);Alternatively, you can Sequelize.import each model (for Sequelize versions < 6), or require each file and call the returned function:
var User = require('path/to/user')(sequelize, DataTypes);You can use the -l es6 option to create the model definition files as ES6 classes, or -l esm option to create ES6 modules. Then you would require or import the classes and call the init(sequelize, DataTypes) method on each class.
Add -l ts to cli options or lang: 'ts' to programmatic options. This will generate a TypeScript class in each model file, and an init-model.ts file
to import and initialize all the classes.
Note that you need TypeScript 4.x to compile the generated files.
The TypeScript model classes are created as described in the Sequelize manual
Example model class, order.ts:
…Example init-models.ts:
…Model usage in a TypeScript program:
…For the -c, --config option, various JSON/configuration parameters are defined by Sequelize's options flag within the constructor. See the Sequelize docs for more info.
const SequelizeAuto = require('sequelize-auto');
const auto = new SequelizeAuto('database', 'user', 'pass');
auto.run().then(data => {
console.log(data.tables); // table and field list
console.log(data.foreignKeys); // table foreign key list
console.log(data.indexes); // table indexes
console.log(data.hasTriggerTables); // tables that have triggers
console.log(data.relations); // relationships between models
console.log(data.text) // text of generated models
});With options:
…Or you can create the sequelize instance first, using a connection string,
and then pass it to SequelizeAuto:
const SequelizeAuto = require('sequelize-auto');
const Sequelize = require('sequelize');
// const sequelize = new Sequelize('sqlite::memory:');
const sequelize = new Sequelize('postgres://user:[email protected]:5432/dbname');
const options = { caseFile: 'l', caseModel: 'p', caseProp: 'c' };
const auto = new SequelizeAuto(sequelize, null, null, options);
auto.run();To set up:
Create an empty database called sequelize_auto_test on your database server (sqlite excepted)
Create a .env file from sample.env and set your username/password/port etc. The env is read by test/config.js
Build the TypeScript from the src directory into the lib directory:
npm run build
Then run one of the test commands below:
# mysql only
npm run test-mysql
# postgres only
npm run test-postgres
# mssql only
npm run test-mssql
# sqlite only
npm run test-sqliteAlso see the sample directory which has an example including database scripts, export script, and a sample app.
No open issues yet, or sync has not completed.