Summary
The migration of a large legacy MySQL database from Laravel to NestJS using TypeORM poses significant challenges, including tables without primary keys, complex column defaults, and a large schema size. A reliable approach is needed to generate TypeORM entities while minimizing database schema changes and ensuring data integrity.
Root Cause
The root cause of the issues encountered is:
- typeorm-model-generator limitations, such as failing to generate entities for tables without primary keys
- Invalid TypeScript generation for certain column defaults, causing Prettier and TypeScript parse errors
- Schema size and complexity, making it difficult to manage and maintain entities
Why This Happens in Real Systems
This happens in real systems due to:
- Legacy database design, which may not follow modern best practices
- Incremental development, leading to a large and complex schema over time
- Technological debt, where database design and schema changes are postponed or neglected
Real-World Impact
The real-world impact of these issues includes:
- Data integrity risks, due to potential runtime corruption or silent bugs
- Maintenance challenges, resulting from duplicate entities and schema drift
- Migration delays, caused by the need to manually recreate entities or redesign the database schema
Example or Code
// Example of a generated entity with an invalid default value
@Entity()
export class ExampleEntity {
@Column({ type: 'varchar' })
exampleColumn: string;
constructor() {
this.exampleColumn = () => '{"key":"value"}'; // Invalid default value
}
}
How Senior Engineers Fix It
Senior engineers fix these issues by:
- Implementing custom entity generation scripts, to handle tables without primary keys and complex column defaults
- Using TypeORM’s built-in features, such as @Column options and @BeforeInsert hooks, to ensure data integrity and validate entity data
- Breaking down the schema, into smaller, more manageable modules, to reduce complexity and improve maintainability
Why Juniors Miss It
Juniors may miss these issues due to:
- Lack of experience, with large and complex legacy databases
- Insufficient knowledge, of TypeORM’s features and limitations
- Inadequate testing, which can lead to overlooking potential data integrity risks and maintenance challenges