nestjs-reverse-engineering
Version:
A powerful TypeScript/NestJS library for database reverse engineering, entity generation, and CRUD operations
452 lines (373 loc) โข 11.5 kB
Markdown
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.
- **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-engineering
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:
```bash
npx nestjs-reverse-engineering entities
npx nestjs-reverse-engineering crud
npx nestjs-reverse-engineering test
```
```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 {}
```
```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();
```
```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
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
};
}
```
```bash
-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
```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 <tables> Comma-separated list of tables to include
--exclude-tables <tables> Comma-separated list of tables to exclude
```
#### Generate CRUD Operations
```bash
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
```bash
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
```bash
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
```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
### Custom Entity Templates
```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'
}
});
```
```typescript
// Generate only specific tables
await service.generateCrud({
includeTables: ['users', 'posts'],
excludeTables: ['migrations']
});
```
```typescript
const customConfig = {
database: { /* ... */ },
generation: {
outputDir: './src/modules',
entitiesDir: './src/database/entities'
},
features: {
generateAuth: true,
enableSwagger: true
}
};
const service = new ReverseEngineeringService(customConfig);
```
```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 {}
```
```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();
}
}
```
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
});
});
```
- **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
DB_HOST=localhost \
DB_USERNAME=user \
DB_PASSWORD=pass \
npx nestjs-reverse-engineering all
```
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](LICENSE) file for details.
## ๐ Issues & Support
For issues, questions, or contributions, please visit our [GitHub repository](https://github.com/SuryaDivami/reverse-engineering/issues).
## ๐ Additional Resources
- [NestJS Documentation](https://docs.nestjs.com/)
- [TypeORM Documentation](https://typeorm.io/)