English | ä¸ć–‡
A powerful NestJS package that automatically generates CRUD operations, entities, DTOs, services, controllers, and modules based on your database table structure using TypeORM.
- 🚀 Automatic CRUD Generation: Generate complete CRUD operations from database tables
- 🗄️ Multi-Database Support: MySQL, PostgreSQL, SQL Server, Oracle, MariaDB, SQLite
- 📝 Smart Templates: Uses Handlebars templates for customizable code generation
- 🎯 NestJS Integration: Seamlessly integrates with NestJS applications
- đź”§ Flexible Configuration: Customizable naming conventions and code styles
- 📦 CLI Support: Command-line interface for easy integration
- 🎨 Code Formatting: Automatic Prettier formatting for generated code
npm install typeorm-crud-generatorimport { Module } from '@nestjs/common';
import { TypeormCrudGeneratorModule } from 'typeorm-crud-generator';
@Module({
imports: [TypeormCrudGeneratorModule],
// ... other imports
})
export class AppModule {}import { Injectable } from '@nestjs/common';
import { GenerateService, IConnectionOptions, IGenerationOptions } from 'typeorm-crud-generator';
@Injectable()
export class MyService {
constructor(private readonly generateService: GenerateService) {}
async generateCrud() {
const connectionOptions: IConnectionOptions = {
databaseType: 'mysql',
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
databaseNames: ['mydb'],
schemaNames: [''],
ssl: false,
skipTables: [],
onlyTables: [],
};
const generationOptions: Partial<IGenerationOptions> = {
resultsPath: './generated',
convertCaseEntity: 'pascal',
convertCaseProperty: 'camel',
convertCaseFile: 'param',
};
await this.generateService.generateCrud(connectionOptions, generationOptions);
}
}# Global installation
npm install -g typeorm-crud-generator
# Use short command
tg --help
# Fully interactive mode
tg
# Command line mode
tg --type mysql --host localhost --port 3306 --username root --password password --database mydb --output ./generated# Fully interactive mode
npx typeorm-crud-generator
# Command line mode
npx typeorm-crud-generator \
--type mysql \
--host localhost \
--port 3306 \
--username root \
--password password \
--database mydb \
--output ./generated
# Multiple databases support
npx typeorm-crud-generator \
--type mysql \
--database "db1,db2,db3" \
--skip-tables "migrations,logs" \
--only-tables "users,products"# Add script to package.json
{
"scripts": {
"generate": "node dist/cli.js"
}
}
# Use script
npm run generate
# With parameters
npm run generate -- --type mysql --database mydbinterface IConnectionOptions {
databaseType: 'mysql' | 'postgres' | 'mssql' | 'oracle' | 'mariadb' | 'sqlite';
host: string;
port: number;
user: string;
password: string;
databaseNames: string[]; // Support multiple databases
schemaNames: string[];
ssl: boolean;
skipTables: string[]; // Tables to skip
onlyTables: string[]; // Only process these tables
instanceName?: string;
//If OnlyTables is set and there are associated tables that are not in OnlyTables, then this field needs to be set to false, and the generated entity will not contain any associated relationships. If they all exist, set to true, and an entity with associated relationships will be generated. default is false
includeRelatedTables?: boolean
}interface IGenerationOptions {
// Basic configuration
resultsPath: string; // Output directory
pluralizeNames: boolean; // Whether to pluralize entity names
noConfigs: boolean; // Whether to skip config file generation
// Naming conversion strategy
convertCaseFile: 'pascal' | 'param' | 'camel' | 'none'; // File naming
convertCaseEntity: 'pascal' | 'camel' | 'none'; // Entity naming
convertCaseProperty: 'pascal' | 'camel' | 'snake' | 'none'; // Property naming
convertEol: 'LF' | 'CRLF'; // Line ending style
// Code generation options
propertyVisibility: 'public' | 'protected' | 'private' | 'none'; // Member visibility
lazy: boolean; // Whether to use lazy loading for relations
activeRecord: boolean; // Whether to inherit BaseEntity
generateConstructor: boolean; // Whether to generate constructor based on Partial<T>
customNamingStrategyPath: string; // Custom naming strategy path
relationIds: boolean; // Whether to generate RelationId fields
strictMode: 'none' | '?' | '!'; // Strict mode flag (affects nullable/required type annotations)
skipSchema: boolean; // Whether to skip schema
indexFile: boolean; // Whether to generate index file
exportType: 'named' | 'default'; // Export type
// Permission related (controller route permission decorators)
addPermissionIdentifier?: boolean; // Enable permission decorators on each controller route
permissionIdentifier?: string; // Permission decorator identifier (default @permission)
perMissionIdentifierPrefix?: string; // Permission prefix, usually for system domain
// Swagger switch
addSwaggerIdentifier?: boolean; // Only output Swagger imports and decorators when true
}resultsPath: Output directory for generated resultspluralizeNames: Whether to pluralize entity namesnoConfigs: Whether to skip config file generation
convertCaseFile: File naming case conversion strategyconvertCaseEntity: Entity class name case conversion strategyconvertCaseProperty: Property name case conversion strategyconvertEol: Line ending style (LF/CRLF)
propertyVisibility: Member visibility (public/protected/private/none)lazy: Whether to use lazy loading for relations (Promise)activeRecord: Whether to inherit BaseEntitygenerateConstructor: Whether to generate constructor based on PartialcustomNamingStrategyPath: Custom naming strategy pathrelationIds: Whether to generate RelationId fieldsstrictMode: Strict mode flag (none/?/!), affects nullable/required type annotationsskipSchema: Whether to skip schemaindexFile: Whether to generate index fileexportType: Export type (named/default)
addPermissionIdentifier: Enable permission decorators on each controller routepermissionIdentifier: Permission decorator identifier (default @permission), outputs as:@permission('prefix:entityCamel:operation')perMissionIdentifierPrefix: Permission prefix, usually for system domain (e.g., 'system')
Operation mapping:
add= Create endpoint (@Post)remove= Delete endpoint (@Delete)list= Query list (@Get /paginate & @Get /)query= Query details (@Get :id)edit= Edit/Update (@Patch :id or /upsert)
addSwaggerIdentifier: Only output Swagger imports and decorators when true; when false, completely independent of @nestjs/swagger- When false:
- Controller doesn't output ApiTags/ApiOperation/ApiResponse etc.
- Create/Pagination DTO doesn't output ApiProperty/ApiPropertyOptional
- Entity doesn't output ApiProperty
- Update DTO will import PartialType from @nestjs/mapped-types instead of swagger version
- When false:
For each database table, the generator creates:
- Entity: TypeORM entity class with proper decorators
- DTOs: Create, Update, and Pagination DTOs
- Service: CRUD service with TypeORM operations
- Controller: REST API controller with CRUD endpoints
- Module: NestJS module configuration
Main service for CRUD generation, providing two core methods:
generateCrud(connectionOptions, generationOptions): Generate CRUD files to specified directorygenerateAndArchiveCrud(connectionOptions, generationOptions): Generate CRUD files and package into ZIP archive
import { GenerateService, IConnectionOptions, IGenerationOptions } from 'typeorm-crud-generator';
@Injectable()
export class MyService {
constructor(private readonly generateService: GenerateService) {}
async generateFiles() {
const connectionOptions: IConnectionOptions = {
databaseType: 'mysql',
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
databaseNames: ['mydb'],
schemaNames: [''],
ssl: false,
skipTables: [],
onlyTables: [],
includeRelatedTables:true
};
const generationOptions: Partial<IGenerationOptions> = {
resultsPath: './generated',
convertCaseEntity: 'pascal',
convertCaseProperty: 'camel',
};
// Generate files to directory
await this.generateService.generateCrud(connectionOptions, generationOptions);
// Or generate files and package into ZIP
await this.generateService.generateAndArchiveCrud(connectionOptions, generationOptions);
}
}import { Module } from '@nestjs/common';
import { TypeormCrudGeneratorModule, GenerateService } from 'typeorm-crud-generator';
@Module({
imports: [TypeormCrudGeneratorModule],
providers: [MyService],
})
export class AppModule {}
@Injectable()
export class MyService {
constructor(private readonly generateService: GenerateService) {}
async generateFromDatabase() {
await this.generateService.generateCrud({
databaseType: 'postgres',
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'password',
databaseNames: ['mydb'],
schemaNames: [''],
ssl: false,
skipTables: [],
onlyTables: [],
includeRelatedTables:true
}, {
resultsPath: './src/generated',
convertCaseEntity: 'pascal',
convertCaseProperty: 'camel',
});
}
}This project is inspired by and builds upon ideas from typeorm-model-generator.
- Repository: typeorm-model-generator
- License: MIT (see their repository for details)
You can customize the generated code by modifying the Handlebars templates in the templates/ directory.
- Initial release
- Support for multiple database types
- CRUD generation with customizable templates
- CLI support
- NestJS module integration