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-generatorVerified import paths — ran on the pinned version, not inferred.
Demonstrates programmatic generation of Sequelize TypeScript models from a PostgreSQL database, showing essential configuration and environment variable usage for credentials.
Ensure your project's `typescript` version meets the peer dependency requirement by running `npm install typescript@^5.0.4` or updating your `package.json`.
Install the database driver for your dialect, e.g., `npm install -S pg pg-hstore` for PostgreSQL, `npm install -S mysql2` for MySQL, etc.
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`.
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.
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.
Install the appropriate database driver for your chosen dialect. For PostgreSQL, run `npm install pg pg-hstore`. For MySQL, `npm install mysql2`, etc.
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.
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.
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.