Registry / database / sequelize-auto

sequelize-auto

JSON →
library0.8.8jsnpmunverified

Sequelize-Auto is a utility that automates the generation of Sequelize ORM models directly from an existing database schema. It supports various SQL dialects including MySQL/MariaDB, PostgreSQL, SQLite, and MSSQL. The current stable version is 0.8.8, and it is primarily a command-line interface tool, though it also offers programmatic usage. Its main purpose is to reduce manual boilerplate by converting an existing database structure into ready-to-use Sequelize model definitions, including basic column definitions and data types. Users must install Sequelize and the specific database dialect driver separately, as these are no longer direct dependencies.

npm install sequelize-auto
INSTALL
IMPORT
SIG · SEQUELIZE-AUTO
S
sequelize-auto
databasejavascriptv0.8.8
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SequelizeAuto
const SequelizeAuto = require('sequelize-auto');
import SequelizeAuto from 'sequelize-auto';
For CommonJS environments (default for this package), this is the correct way to import the main class for programmatic use.
SequelizeAuto
import SequelizeAuto from 'sequelize-auto';
const SequelizeAuto = require('sequelize-auto');
For ECMAScript Modules (ESM) projects, `sequelize-auto` is typically imported as a default export, often requiring bundler configuration or specific `package.json` setup for dual CJS/ESM support.
AutoOptions
import type { AutoOptions } from 'sequelize-auto';
This type import provides definitions for the options object passed to the `SequelizeAuto` constructor when using it programmatically in TypeScript projects.

This quickstart demonstrates how to use `sequelize-auto` via its command-line interface to generate Sequelize models from an existing MySQL database table, including setup instructions and a conceptual example of using the generated model programmatically.

const { execSync } = require('child_process'); const path = require('path'); // --- Prerequisites --- // 1. Install sequelize and a dialect driver (e.g., mysql2): // npm install sequelize mysql2 // 2. Ensure a database and table exist, e.g., for MySQL: // CREATE DATABASE my_auto_db; // USE my_auto_db; // CREATE TABLE products ( // id INT PRIMARY KEY AUTO_INCREMENT, // name VARCHAR(255) NOT NULL, // price DECIMAL(10, 2) DEFAULT 0.00 // ); // INSERT INTO products (name, price) VALUES ('Laptop', 1200.00); // --- Configuration --- const outputDir = path.join(__dirname, 'generated_models'); const host = process.env.DB_HOST ?? 'localhost'; const user = process.env.DB_USER ?? 'root'; const password = process.env.DB_PASSWORD ?? 'password'; // !! Use secure methods for passwords in production const database = 'my_auto_db'; const dialect = 'mysql'; const tableName = 'products'; // Ensure the output directory exists try { execSync(`mkdir -p ${outputDir}`); console.log(`Ensured output directory: ${outputDir}`); } catch (e) { console.error('Failed to create output directory:', e.message); process.exit(1); } // --- Run sequelize-auto CLI --- const cliCommand = [ 'sequelize-auto', `-h ${host}`, `-d ${database}`, `-u ${user}`, `-x ${password}`, `--dialect ${dialect}`, `-o ${outputDir}`, `-t ${tableName}` ].join(' '); console.log(` Executing CLI command: ${cliCommand} `); try { const stdout = execSync(cliCommand, { encoding: 'utf8', stdio: 'pipe' }); console.log('Sequelize models generated successfully:'); console.log(stdout); console.log(` Check the '${outputDir}' directory for generated model files.`); // --- Example of programmatic usage (after models are generated) --- console.log('\n--- Demonstrating programmatic usage of generated models (conceptual) ---'); const { Sequelize, DataTypes } = require('sequelize'); const sequelize = new Sequelize(database, user, password, { host, dialect, logging: false // Suppress Sequelize SQL logging }); // Dynamically require the generated model // Note: The actual model file name might vary based on case options (e.g., 'product.js' or 'Product.js') const ProductModel = require(path.join(outputDir, tableName.charAt(0).toUpperCase() + tableName.slice(1))) (sequelize, DataTypes); async function fetchProduct() { try { await sequelize.authenticate(); console.log('Database connection successful.'); const product = await ProductModel.findOne({ where: { name: 'Laptop' } }); if (product) { console.log('Found product:', product.toJSON()); } else { console.log('Product not found.'); } } catch (error) { console.error('Error during database operation:', error); } finally { await sequelize.close(); console.log('Database connection closed.'); } } fetchProduct(); } catch (error) { console.error('\nFailed to generate Sequelize models:'); console.error(error.message); if (error.stderr) { console.error('Stderr:', error.stderr); } process.exit(1); }
sequelize-auto --version
Debug
Known issues
breakingAs of a prior major version, `sequelize-auto` no longer includes `sequelize` as a direct dependency. Users must manually install `sequelize` and the appropriate dialect driver (e.g., `mysql2`, `pg`, `sqlite3`, `tedious`) separately. Failing to do so will result in 'module not found' errors.
fix
Run `npm install sequelize <dialect-driver>` (e.g., `npm install sequelize mysql2`) in your project.
affects: >=0.5.0 (based on project history, though specific version change is not in excerpt)
gotchaThe peer dependency for `sequelize` is specified as `>3.30.0`. While `sequelize-auto` may function with modern `sequelize` versions (v6/v7), this broad and older range can lead to unexpected behavior or incompatibilities if breaking changes were introduced in `sequelize` versions not fully anticipated by `sequelize-auto`.
fix
Always test generated models thoroughly with your specific `sequelize` version. For critical applications, consider explicitly pinning to a `sequelize` version known to be compatible, such as `^5` or `^6`, and review `sequelize`'s breaking changes for newer versions.
affects: >=0.8.0
gotchaWhen using the `--pass` (or `-x`) option, if no password value is provided, `sequelize-auto` will interactively prompt for the password in the terminal. Directly providing passwords in command-line arguments can pose security risks (e.g., shell history).
fix
For production or automated scripts, prefer securing credentials using environment variables (e.g., `process.env.DB_PASSWORD`), configuration files (`-c`), or interactive prompts when appropriate, rather than hardcoding passwords in commands.
affects: >=0.8.0
gotchaThe `-c` (config) and `-a` (additional) options expect paths to JSON files for Sequelize options and model options, respectively. Incorrect JSON formatting or invalid Sequelize-specific options in these files can lead to generation failures or malformed models.
fix
Ensure that any JSON configuration files are syntactically correct and contain valid options as per the Sequelize documentation for constructor options (for `-c`) and `Model.init` options (for `-a`).
affects: >=0.8.0
gotchaGenerated model files, especially for TypeScript (`-l ts`), rely on specific Sequelize versions and TypeScript configurations. Older TypeScript versions (pre-4.x) might fail to compile generated TypeScript models due to syntax or feature usage.
fix
Use TypeScript 4.x or newer when generating TypeScript models. If using ES modules, ensure your `tsconfig.json` and Node.js environment are correctly configured for ESM support.
affects: >=0.8.0
Errors
Common errors & fixes
Error: Cannot find module 'sequelize'
The `sequelize` package is not installed or not resolvable in the current project's `node_modules`.
fix
Install `sequelize` as a project dependency: `npm install sequelize` or `yarn add sequelize`.
Error: Please install the '<dialect-driver>' package manually
The specific database dialect driver (e.g., `mysql2`, `pg`, `sqlite3`, `tedious`) corresponding to the `--dialect` option is missing.
fix
Install the required driver package: `npm install <dialect-driver-package>` (e.g., `npm install mysql2` for MySQL/MariaDB).
Unhandled rejection SequelizeConnectionError: Access denied for user 'user'@'host' (or similar connection error)
Incorrect database connection parameters (host, user, password, port, database name) or the database server is inaccessible/not running.
fix
Verify the `-h`, `-d`, `-u`, `-x`, `-p` options. Check your database server status, credentials, firewall rules, and network connectivity.
Error: Output directory does not exist: /path/to/models
The directory specified by the `-o` or `--output` option for generated models does not exist.
fix
Create the output directory manually before running `sequelize-auto`: `mkdir -p /path/to/models` (or `New-Item -Path /path/to/models -ItemType Directory` on Windows).
Upgrade
Version history
0.8.8latest on npm
Audit
Dependencies
sequelizerequiredCore ORM library required for generated models and sequelize-auto's internal operations.
mysql2optionalMySQL/MariaDB dialect driver. Required if connecting to MySQL or MariaDB databases.
pgoptionalPostgreSQL dialect driver. Required if connecting to PostgreSQL databases.
pg-hstoreoptionalPostgreSQL HSTORE type support. Often used in conjunction with 'pg' for PostgreSQL.
sqlite3optionalSQLite dialect driver. Required if connecting to SQLite databases.
tediousoptionalMSSQL dialect driver. Required if connecting to Microsoft SQL Server databases.
Agent activity
12 hits · last 30 days
node
10
Meta
1
Resources
sequelize-auto — npm install sequelize-auto · libregistry