Sequelize Automate Model Generator
raw JSON →sequelize-automate is a utility designed to automatically generate Sequelize ORM model definitions directly from an existing database schema. It supports various code styles including JavaScript, TypeScript, Egg.js, and Midway.js for the generated models. The package is currently at version 1.2.2 and appears to be actively maintained, though it does not specify a fixed release cadence. Its key differentiator is its straightforward command-line interface (CLI) that simplifies scaffolding models from an established database, reducing the manual boilerplate required when integrating Sequelize into existing projects. It supports popular SQL dialects such as MySQL, PostgreSQL, MariaDB, SQLite, and Microsoft SQL Server, requiring users to install the corresponding database driver separately. This tool is particularly useful for accelerating development in brownfield projects or when rapidly prototyping a Sequelize application against an existing data source.
Common errors
error Error: Please install mysql2 package manually. ↓
npm install mysql2 (or npm install -g mysql2 if using the global CLI) to install the necessary driver. Replace mysql2 with pg, mariadb, sqlite3, or tedious for other dialects. error Error: connect ECONNREFUSED <host>:<port> ↓
sequelize-automate. Double-check the --host, --port, --user, and --password parameters. Check firewall rules. error Error: Can't get definitions from database. Reason: Access denied for user '...'@'localhost' (using password: YES) ↓
--user and --password arguments are correct and that the user has sufficient privileges to read the database schema. Warnings
breaking Starting with `sequelize-automate` v1.x, `sequelize` itself and database drivers (e.g., `mysql2`, `pg`) are no longer direct dependencies. Users must manually install `sequelize` and the appropriate dialect binding in their project. ↓
gotcha When using `sequelize-automate` programmatically, ensure you correctly instantiate the `Automate` class with all required database connection parameters. Incorrect or missing parameters will lead to connection failures. ↓
gotcha The `--type` option dictates the generated code style (e.g., `js`, `ts`, `egg`, `midway`). If generating TypeScript models (`--type ts`), you will need TypeScript 4.x or later to compile the generated files. ↓
gotcha Generated models from `sequelize-automate` provide basic schema definitions. Complex associations (e.g., `hasMany`, `belongsToMany`) and custom methods are not automatically inferred or generated and may require manual addition post-generation. ↓
Install
npm install sequelize-automate yarn add sequelize-automate pnpm add sequelize-automate Imports
- Automate
import Automate from 'sequelize-automate'; // ESM const Automate = require('sequelize-automate'); // CJS - AutomateInstance.run wrong
Automate.run();correctconst automate = new Automate(database, user, pass, options); const generatedCode = await automate.run(); - AutomateInstance.getDefinitions wrong
Automate.getDefinitions();correctconst automate = new Automate(database, user, pass, options); const definitions = await automate.getDefinitions();
Quickstart
import { exec } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Database connection details from environment variables or defaults
const DB_HOST = process.env.DB_HOST ?? 'localhost';
const DB_NAME = process.env.DB_NAME ?? 'your_database_name';
const DB_USER = process.env.DB_USER ?? 'root';
const DB_PASSWORD = process.env.DB_PASSWORD ?? '';
const DB_PORT = process.env.DB_PORT ?? '3306'; // e.g., '5432' for Postgres
const DB_DIALECT = process.env.DB_DIALECT ?? 'mysql'; // e.g., 'postgres', 'sqlite', 'mssql'
const OUTPUT_DIR = path.join(__dirname, 'generated_models');
// Ensure output directory exists (for a real project, consider `fs.mkdirSync`)
console.log(`Models will be generated in: ${OUTPUT_DIR}`);
// CLI command to generate TypeScript models
const command = `npx sequelize-automate \
--type ts \
--host ${DB_HOST} \
--database ${DB_NAME} \
--user ${DB_USER} \
--password ${DB_PASSWORD} \
--port ${DB_PORT} \
--dialect ${DB_DIALECT} \
--output ${OUTPUT_DIR} \
--camel`;
console.log(`Executing command:\n${command}`);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error generating models: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
console.log(`Models generated successfully:\n${stdout}`);
console.log(`
Check the '${OUTPUT_DIR}' directory for your new Sequelize models.`);
});