npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

nestjs-reverse-engineering

v0.0.5

Published

A powerful TypeScript/NestJS library for database reverse engineering, entity generation, and CRUD operations

Readme

NestJS Reverse Engineering Library

A powerful, configurable TypeScript/NestJS library for database reverse engineering, entity generation, and CRUD operations. Transform your existing database into a fully-featured NestJS application with entities, controllers, services, and more.

🚀 Features

  • Database Reverse Engineering: Generate TypeORM entities from existing database schemas
  • CRUD Generation: Create controllers, services, DTOs, and repositories
  • Multiple Database Support: PostgreSQL, MySQL, and more via TypeORM
  • Configurable Output: Customize where files are generated
  • Table Filtering: Include/exclude specific tables
  • NestJS Integration: Direct module integration with your existing apps
  • CLI & Programmatic APIs: Use via command line or in your code
  • Entity Reuse: Uses existing entities when available
  • Module Auto-Import: Automatically updates your app.module.ts
  • SQL Generation: Export CREATE TABLE and INSERT statements
  • Data Export: Export data with optional masking for sensitive fields

📦 Installation

npm install nestjs-reverse-engineering
# or
yarn add nestjs-reverse-engineering

🛠️ Quick Start

1. CLI Usage

Initialize a configuration file:

npx nestjs-reverse-engineering init

Generate everything:

npx nestjs-reverse-engineering all

Or use individual commands:

# Generate entities only
npx nestjs-reverse-engineering entities

# Generate CRUD operations
npx nestjs-reverse-engineering crud

# Test database connection
npx nestjs-reverse-engineering test

2. NestJS Module Integration

import { Module } from '@nestjs/common';
import { ReverseEngineeringModule } from 'nestjs-reverse-engineering';

@Module({
  imports: [
    ReverseEngineeringModule.forRoot({
      database: {
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        username: 'your_username',
        password: 'your_password',
        database: 'your_database',
      },
      generation: {
        outputDir: './src',
        entitiesDir: './src/entities',
      },
    }),
  ],
})
export class AppModule {}

3. Programmatic Usage

import { ReverseEngineeringService } from 'nestjs-reverse-engineering';

const service = new ReverseEngineeringService({
  database: {
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    username: 'your_username',
    password: 'your_password',
    database: 'your_database',
  },
});

// Generate entities
await service.generateEntities();

// Generate CRUD operations
await service.generateCrud();

// Test connection
const isConnected = await service.testConnection();

⚙️ Configuration

Configuration File (re-config.json)

{
  "database": {
    "type": "postgres",
    "host": "localhost",
    "port": 5432,
    "username": "your_username",
    "password": "your_password",
    "database": "your_database",
    "schema": "public"
  },
  "generation": {
    "outputDir": "./src",
    "entitiesDir": "./src/entities",
    "sqlDir": "./sql",
    "dataDir": "./data"
  },
  "tables": {
    "includeTables": [],
    "excludeTables": ["migrations", "seeds"]
  },
  "features": {
    "generateEntities": true,
    "generateCrud": true,
    "generateSql": true,
    "exportData": true,
    "enableSwagger": true,
    "enableValidation": true,
    "enableAuth": false
  },
  "crud": {
    "framework": "nestjs",
    "generateTests": true,
    "generateAuth": false
  },
  "data": {
    "enableMasking": false,
    "batchSize": 1000
  }
}

TypeScript Configuration Interface

export interface ReverseEngineeringConfig {
  database: {
    type: 'postgres' | 'mysql' | 'sqlite' | 'mssql';
    host?: string;
    port?: number;
    username?: string;
    password?: string;
    database?: string;
    schema?: string;
    ssl?: boolean;
    synchronize?: boolean;
  };
  generation?: {
    outputDir?: string;          // Default: './src'
    entitiesDir?: string;        // Default: './src/entities'
    sqlDir?: string;             // Default: './sql'
    dataDir?: string;            // Default: './data'
  };
  tables?: {
    includeTables?: string[];    // Include only these tables
    excludeTables?: string[];    // Exclude these tables
  };
  features?: {
    generateEntities?: boolean;   // Default: true
    generateCrud?: boolean;       // Default: true
    generateSql?: boolean;        // Default: true
    exportData?: boolean;         // Default: true
    enableSwagger?: boolean;      // Default: true
    enableValidation?: boolean;   // Default: true
    enableAuth?: boolean;         // Default: false
  };
  crud?: {
    framework?: 'nestjs';         // Default: 'nestjs'
    generateTests?: boolean;      // Default: true
    generateAuth?: boolean;       // Default: false
  };
  data?: {
    enableMasking?: boolean;      // Default: false
    batchSize?: number;           // Default: 1000
  };
}

🖥️ CLI Commands

Global Options

-h, --host <host>              Database host
-p, --port <port>              Database port  
-u, --username <username>      Database username
-w, --password <password>      Database password
-d, --database <database>      Database name
-s, --schema <schema>          Database schema
--dialect <dialect>            Database dialect (postgres|mysql)
--ssl                          Use SSL connection
--config <path>                Configuration file path
--output-dir <dir>             Base output directory
--entities-dir <dir>           Entities directory
--sql-dir <dir>                SQL output directory
--data-dir <dir>               Data export directory

Commands

Initialize Configuration

npx nestjs-reverse-engineering init [options]

Test Database Connection

npx nestjs-reverse-engineering test

List Tables

npx nestjs-reverse-engineering tables

Generate Entities

npx nestjs-reverse-engineering entities [options]

Options:
  --include-tables <tables>    Comma-separated list of tables to include
  --exclude-tables <tables>    Comma-separated list of tables to exclude

Generate CRUD Operations

npx nestjs-reverse-engineering crud [options]

Options:
  --include-tables <tables>    Comma-separated list of tables to include
  --exclude-tables <tables>    Comma-separated list of tables to exclude
  --no-tests                   Skip test generation
  --no-swagger                 Skip Swagger documentation
  --enable-auth                Enable authentication guards

Generate SQL Scripts

npx nestjs-reverse-engineering sql [options]

Options:
  --include-tables <tables>    Comma-separated list of tables to include
  --exclude-tables <tables>    Comma-separated list of tables to exclude

Export Data

npx nestjs-reverse-engineering data [options]

Options:
  --include-tables <tables>    Comma-separated list of tables to include  
  --exclude-tables <tables>    Comma-separated list of tables to exclude
  --enable-masking             Enable data masking for sensitive fields
  --batch-size <size>          Batch size for data export

Generate Everything

npx nestjs-reverse-engineering all [options]

🏗️ Generated Structure

When you run the library, it generates files in your project's /src directory:

src/
├── entities/
│   ├── user.entity.ts
│   ├── post.entity.ts
│   └── index.ts
├── users/
│   ├── users.controller.ts
│   ├── users.service.ts
│   ├── users.module.ts
│   ├── dto/
│   │   ├── create-user.dto.ts
│   │   └── update-user.dto.ts
│   └── users.controller.spec.ts
├── posts/
│   ├── posts.controller.ts
│   ├── posts.service.ts
│   ├── posts.module.ts
│   ├── dto/
│   │   ├── create-post.dto.ts
│   │   └── update-post.dto.ts
│   └── posts.controller.spec.ts
└── app.module.ts (automatically updated)

🔧 Advanced Usage

Custom Entity Templates

import { ReverseEngineeringService } from 'nestjs-reverse-engineering';

const service = new ReverseEngineeringService(config);

// Generate with custom templates
await service.generateEntities({
  customTemplates: {
    entity: 'path/to/custom-entity.template',
    dto: 'path/to/custom-dto.template'
  }
});

Selective Generation

// Generate only specific tables
await service.generateCrud({
  includeTables: ['users', 'posts'],
  excludeTables: ['migrations']
});

With Custom Configuration

const customConfig = {
  database: { /* ... */ },
  generation: {
    outputDir: './src/modules',
    entitiesDir: './src/database/entities'
  },
  features: {
    generateAuth: true,
    enableSwagger: true
  }
};

const service = new ReverseEngineeringService(customConfig);

🔌 NestJS Module Features

Dynamic Configuration

import { ReverseEngineeringModule } from 'nestjs-reverse-engineering';

@Module({
  imports: [
    ReverseEngineeringModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        database: {
          type: configService.get('DB_TYPE'),
          host: configService.get('DB_HOST'),
          // ... other config
        },
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

Injectable Service

import { Injectable } from '@nestjs/common';
import { ReverseEngineeringService } from 'nestjs-reverse-engineering';

@Injectable()
export class MyService {
  constructor(
    private readonly reverseEngineering: ReverseEngineeringService,
  ) {}

  async regenerateEntities() {
    return this.reverseEngineering.generateEntities();
  }
}

🧪 Testing

The library includes comprehensive testing utilities:

import { Test } from '@nestjs/testing';
import { ReverseEngineeringModule } from 'nestjs-reverse-engineering';

describe('Generated Modules', () => {
  beforeEach(async () => {
    const module = await Test.createTestingModule({
      imports: [ReverseEngineeringModule.forRoot(config)],
    }).compile();
    
    // Your tests here
  });
});

🔒 Security & Best Practices

  • Environment Variables: Store database credentials in environment variables
  • Data Masking: Enable data masking for sensitive information during export
  • SSL Connections: Use SSL for production database connections
  • Table Filtering: Exclude sensitive or system tables from generation
# Example with environment variables
DB_HOST=localhost \
DB_USERNAME=user \
DB_PASSWORD=pass \
npx nestjs-reverse-engineering all

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🐛 Issues & Support

For issues, questions, or contributions, please visit our GitHub repository.

📚 Additional Resources