Sequelize Automate Model Generator

raw JSON →
1.2.2 verified Thu Apr 23 auth: no javascript

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.

error Error: Please install mysql2 package manually.
cause The required database driver for MySQL/MariaDB (mysql2) is not installed in the project or globally.
fix
Run 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>
cause The application could not establish a connection to the database. This typically means the database server is not running, the host/port is incorrect, or a firewall is blocking the connection.
fix
Verify that your database server is running and accessible from the machine running 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)
cause The provided database credentials (username and password) are incorrect or lack the necessary permissions to access the specified database.
fix
Ensure the --user and --password arguments are correct and that the user has sufficient privileges to read the database schema.
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.
fix Ensure you explicitly run `npm install sequelize` and `npm install <your-database-driver>` (e.g., `npm install mysql2`) in your 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.
fix Initialize the `Automate` class like `new Automate(database, user, password, { host, port, dialect, ...options })` ensuring all necessary connection details are provided.
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.
fix Specify the correct `--type` option for your project's needs. If using `--type ts`, ensure your project's `tsconfig.json` is configured correctly and you have TypeScript 4.x+ installed.
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.
fix After generation, review the models and manually add any missing associations or custom logic within your application's Sequelize setup.
npm install sequelize-automate
yarn add sequelize-automate
pnpm add sequelize-automate

This quickstart demonstrates how to use `sequelize-automate` via its command-line interface (CLI) to generate TypeScript-based Sequelize models from an existing database schema. It utilizes environment variables for secure database connection details and outputs the generated files to a specified directory.

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.`);
});