Registry / database / sequelize-typescript-generator

sequelize-typescript-generator

JSON →
library12.0.1jsnpmunverified

sequelize-typescript-generator is a utility that automates the creation of TypeScript models for the sequelize-typescript ORM, directly from an existing relational database schema. Currently at version 12.0.1, the library provides both a command-line interface (CLI) and a programmatic API for model generation. It supports a wide range of SQL databases including PostgreSQL, MySQL, MariaDB, SQL Server, and SQLite, and is tested against various versions of these databases. Its primary differentiation lies in its ability to parse an existing database schema, infer table structures, column types, and common associations (one-to-one, one-to-many, many-to-many), and then generate type-safe sequelize-typescript models, significantly reducing manual boilerplate and ensuring model-to-database consistency. While no explicit release cadence is documented, its consistent major version updates suggest active development, aiming to keep pace with sequelize and sequelize-typescript advancements. The library also offers features like strict mode generation, case transformation for column names, and options for including index annotations, making it a robust tool for projects using a database-first approach with TypeScript and Sequelize.

npm install sequelize-typescript-generator
INSTALL
IMPORT
SIG · SEQUELIZE-TYPESCRI
S
sequelize-typescript-generator
databasejavascriptv12.0.1
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.

generate
import { generate } from 'sequelize-typescript-generator';
const generate = require('sequelize-typescript-generator');
This is the main function for programmatic model generation. The library primarily uses ES Modules.
GeneratorOptions
import type { GeneratorOptions } from 'sequelize-typescript-generator';
Type definition for configuring the `generate` function programmatically.

Demonstrates programmatic generation of Sequelize TypeScript models from a PostgreSQL database, showing essential configuration and environment variable usage for credentials.

import { generate } from 'sequelize-typescript-generator'; import path from 'path'; import fs from 'fs'; async function runGenerator() { const outputDir = path.resolve(__dirname, 'generated-models'); if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } try { await generate({ dialect: 'postgres', host: process.env.DB_HOST ?? 'localhost', port: parseInt(process.env.DB_PORT ?? '5432', 10), database: process.env.DB_NAME ?? 'your_database_name', username: process.env.DB_USER ?? 'postgres', password: process.env.DB_PASSWORD ?? '', outDir: outputDir, schema: process.env.DB_SCHEMA ?? 'public', // Optional, for Postgres // tables: ['users', 'products'], // Optional: specify tables to generate // skipTables: ['audit_logs'], // Optional: specify tables to skip // indices: true, // Optional: include index annotations // strict: true, // Optional: enable strict mode // case: 'camel', // Optional: 'camel' | 'snake' | 'pascal' // associationsFile: path.resolve(__dirname, 'associations.json'), // Optional: define custom associations }); console.log('Sequelize TypeScript models generated successfully!'); } catch (error) { console.error('Error generating models:', error); process.exit(1); } } runGenerator();
sequelize-typescript-generator --version
Debug
Known issues
breakingThis library declares TypeScript version `^5.0.4` as a peer dependency. Using older or incompatible TypeScript versions might lead to compilation errors or unexpected type inference issues, especially with advanced features or strict mode.
fix
Ensure your project's `typescript` version meets the peer dependency requirement by running `npm install typescript@^5.0.4` or updating your `package.json`.
affects: >=12.0.0
gotchaThe generator requires the appropriate database driver (e.g., `pg` for PostgreSQL, `mysql2` for MySQL) to be installed in your project. Failure to install the correct driver for your chosen dialect will result in runtime errors.
fix
Install the database driver for your dialect, e.g., `npm install -S pg pg-hstore` for PostgreSQL, `npm install -S mysql2` for MySQL, etc.
affects: >=1.0.0
gotchaWhen using `sequelize-typescript-generator` globally (`npm install -g`), automatic linting of generated models is not supported out-of-the-box. The `eslint` library does not support global plugins for this functionality.
fix
To enable linting with a global installation, ensure `typescript`, `eslint`, and `@typescript-eslint/parser` are installed locally in your project: `npm install -S typescript eslint @typescript-eslint/parser`.
affects: >=1.0.0
gotchaGenerated models are a snapshot of your database schema at the time of generation. Any subsequent changes to your database schema (e.g., adding/removing columns, altering types, new tables) will *not* automatically update the generated models. You must re-run the generator to reflect these changes.
fix
Integrate the generator into your CI/CD pipeline or development workflow to rerun it whenever database schema migrations occur, ensuring models remain synchronized with the database.
affects: >=1.0.0
gotchaWhile the generator attempts to infer common associations, complex relationships or non-standard naming conventions might not be fully captured. In such cases, you might need to manually define associations via the `--associations-file` option or manually adjust the generated models.
fix
Review generated models carefully for complex associations. Use the `--associations-file` option (for CLI) or `associationsFile` option (programmatic) to provide explicit association definitions for challenging cases, or manually add them post-generation.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'pg'\nRequire stack:\n- .../node_modules/sequelize-typescript-generator/dist/cli.js
The required database driver for the specified dialect is not installed in the project's `node_modules`.
fix
Install the appropriate database driver for your chosen dialect. For PostgreSQL, run `npm install pg pg-hstore`. For MySQL, `npm install mysql2`, etc.
Error: Command 'stg' not found.
The `stg` command (from a global install) is not in the system's PATH, or `npx` (for local install) is not available to execute it.
fix
If using locally, ensure `npx` is installed globally (`npm install -g npx`) or execute with `node ./node_modules/.bin/stg`. If using globally, ensure `npm install -g sequelize-typescript-generator` completed successfully and your global npm bin directory is in your system's PATH.
SequelizeConnectionError: connect ECONNREFUSED <DB_HOST>:<DB_PORT>
The generator failed to establish a connection to the database, likely due to incorrect host, port, credentials, or the database server being unavailable/inaccessible.
fix
Verify the database server is running, reachable from where the generator is executed, and that the provided `host`, `port`, `username`, `password`, and `database` credentials are correct and have appropriate network access.
TypeError: (0 , sequelize_typescript_generator_1.generate) is not a function
Attempting to use a CommonJS `require()` statement or an incorrect import syntax for an ES module-first library, or a bundling issue.
fix
Ensure you are using ES module imports: `import { generate } from 'sequelize-typescript-generator';`. If using CommonJS in a hybrid project, you might need `const { generate } = await import('sequelize-typescript-generator');` or configure your build system for proper ESM resolution.
Upgrade
Version history
12.0.1latest on npm
Audit
Dependencies
typescriptrequiredPeer dependency required for model type generation and linting features.
Agent activity
28 hits · last 30 days
node
24
OpenAI (training)
2
Meta
1
Resources