{"id":12000,"library":"sequelize-typescript-generator","title":"Sequelize TypeScript Generator","description":"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.","status":"active","version":"12.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/spinlud/sequelize-typescript-generator","tags":["javascript","sequelize","sequelize-typescript","sequelize-typescript-generator","sequelize-typescript-auto","knex","bookshelf","typescript"],"install":[{"cmd":"npm install sequelize-typescript-generator","lang":"bash","label":"npm"},{"cmd":"yarn add sequelize-typescript-generator","lang":"bash","label":"yarn"},{"cmd":"pnpm add sequelize-typescript-generator","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for model type generation and linting features.","package":"typescript","optional":false}],"imports":[{"note":"This is the main function for programmatic model generation. The library primarily uses ES Modules.","wrong":"const generate = require('sequelize-typescript-generator');","symbol":"generate","correct":"import { generate } from 'sequelize-typescript-generator';"},{"note":"Type definition for configuring the `generate` function programmatically.","symbol":"GeneratorOptions","correct":"import type { GeneratorOptions } from 'sequelize-typescript-generator';"}],"quickstart":{"code":"import { generate } from 'sequelize-typescript-generator';\nimport path from 'path';\nimport fs from 'fs';\n\nasync function runGenerator() {\n  const outputDir = path.resolve(__dirname, 'generated-models');\n  if (!fs.existsSync(outputDir)) {\n    fs.mkdirSync(outputDir, { recursive: true });\n  }\n\n  try {\n    await generate({\n      dialect: 'postgres',\n      host: process.env.DB_HOST ?? 'localhost',\n      port: parseInt(process.env.DB_PORT ?? '5432', 10),\n      database: process.env.DB_NAME ?? 'your_database_name',\n      username: process.env.DB_USER ?? 'postgres',\n      password: process.env.DB_PASSWORD ?? '',\n      outDir: outputDir,\n      schema: process.env.DB_SCHEMA ?? 'public', // Optional, for Postgres\n      // tables: ['users', 'products'], // Optional: specify tables to generate\n      // skipTables: ['audit_logs'], // Optional: specify tables to skip\n      // indices: true, // Optional: include index annotations\n      // strict: true, // Optional: enable strict mode\n      // case: 'camel', // Optional: 'camel' | 'snake' | 'pascal'\n      // associationsFile: path.resolve(__dirname, 'associations.json'), // Optional: define custom associations\n    });\n    console.log('Sequelize TypeScript models generated successfully!');\n  } catch (error) {\n    console.error('Error generating models:', error);\n    process.exit(1);\n  }\n}\n\nrunGenerator();\n","lang":"typescript","description":"Demonstrates programmatic generation of Sequelize TypeScript models from a PostgreSQL database, showing essential configuration and environment variable usage for credentials."},"warnings":[{"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`.","message":"This 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.","severity":"breaking","affected_versions":">=12.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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`.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"Generated 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"While 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Install the appropriate database driver for your chosen dialect. For PostgreSQL, run `npm install pg pg-hstore`. For MySQL, `npm install mysql2`, etc.","cause":"The required database driver for the specified dialect is not installed in the project's `node_modules`.","error":"Error: Cannot find module 'pg'\\nRequire stack:\\n- .../node_modules/sequelize-typescript-generator/dist/cli.js"},{"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.","cause":"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.","error":"Error: Command 'stg' not found."},{"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.","cause":"The generator failed to establish a connection to the database, likely due to incorrect host, port, credentials, or the database server being unavailable/inaccessible.","error":"SequelizeConnectionError: connect ECONNREFUSED <DB_HOST>:<DB_PORT>"},{"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.","cause":"Attempting to use a CommonJS `require()` statement or an incorrect import syntax for an ES module-first library, or a bundling issue.","error":"TypeError: (0 , sequelize_typescript_generator_1.generate) is not a function"}],"ecosystem":"npm"}