A powerful TypeScript/NestJS library for database reverse engineering, entity generation, and CRUD operations
npm install nestjs-reverse-engineeringA 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.
- 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
``bash`
npm install nestjs-reverse-engineeringor
yarn add nestjs-reverse-engineering
Initialize a configuration file:
`bash`
npx nestjs-reverse-engineering init
Generate everything:
`bash`
npx nestjs-reverse-engineering all
Or use individual commands:
`bashGenerate entities only
npx nestjs-reverse-engineering entities
$3
`typescript
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
`typescript
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
$3
`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
}
}
`$3
`typescript
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
$3
`bash
-h, --host Database host
-p, --port Database port
-u, --username Database username
-w, --password Database password
-d, --database Database name
-s, --schema Database schema
--dialect Database dialect (postgres|mysql)
--ssl Use SSL connection
--config Configuration file path
--output-dir Base output directory
--entities-dir Entities directory
--sql-dir SQL output directory
--data-dir Data export directory
`$3
#### Initialize Configuration
`bash
npx nestjs-reverse-engineering init [options]
`#### Test Database Connection
`bash
npx nestjs-reverse-engineering test
`#### List Tables
`bash
npx nestjs-reverse-engineering tables
`#### Generate Entities
`bash
npx nestjs-reverse-engineering entities [options]Options:
--include-tables Comma-separated list of tables to include
--exclude-tables Comma-separated list of tables to exclude
`#### Generate CRUD Operations
`bash
npx nestjs-reverse-engineering crud [options]Options:
--include-tables Comma-separated list of tables to include
--exclude-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
`bash
npx nestjs-reverse-engineering sql [options]Options:
--include-tables Comma-separated list of tables to include
--exclude-tables Comma-separated list of tables to exclude
`#### Export Data
`bash
npx nestjs-reverse-engineering data [options]Options:
--include-tables Comma-separated list of tables to include
--exclude-tables Comma-separated list of tables to exclude
--enable-masking Enable data masking for sensitive fields
--batch-size Batch size for data export
`#### Generate Everything
`bash
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
$3
`typescript
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'
}
});
`$3
`typescript
// Generate only specific tables
await service.generateCrud({
includeTables: ['users', 'posts'],
excludeTables: ['migrations']
});
`$3
`typescript
const customConfig = {
database: { / ... / },
generation: {
outputDir: './src/modules',
entitiesDir: './src/database/entities'
},
features: {
generateAuth: true,
enableSwagger: true
}
};const service = new ReverseEngineeringService(customConfig);
`๐ NestJS Module Features
$3
`typescript
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 {}
`$3
`typescript
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:
`typescript
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
`bash
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`)This project is licensed under the MIT License - see the LICENSE file for details.
For issues, questions, or contributions, please visit our GitHub repository.