Sequelize TypeScript Generator

12.0.1 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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();

view raw JSON →