This guide helps migrate the codebase to the new improved organization structure.
src/
├── auth/
├── courses/
├── users/
├── common/
├── migrations/
└── [other features mixed at root level]
src/
├── common/ # Shared infrastructure
├── config/ # Configuration
├── auth/ # Authentication
├── features/ # All feature modules
│ ├── courses/
│ ├── users/
│ ├── enrollments/
│ └── [other features]
├── database/ # Database layer
└── test/ # Testing utilities
-
Create new directory structure
mkdir -p apps/backend/src/features mkdir -p apps/backend/src/database mkdir -p apps/backend/src/config
-
Create feature directories
mkdir -p apps/backend/src/features/{courses,users,enrollments,progress,certificates} -
Organize common utilities
mkdir -p apps/backend/src/common/{validation,utils,decorators,filters,interceptors,pipes,sanitizers,errors,logger}
For each feature module:
-
Create module structure
mkdir -p apps/backend/src/features/feature/{controllers,services,entities,dto} -
Move files
# Move controllers mv apps/backend/src/feature.controller.ts apps/backend/src/features/feature/controllers/ # Move services mv apps/backend/src/feature.service.ts apps/backend/src/features/feature/services/ # Move entities mv apps/backend/src/feature.entity.ts apps/backend/src/features/feature/entities/ # Move DTOs mv apps/backend/src/feature.dto.ts apps/backend/src/features/feature/dto/
-
Move module file
mv apps/backend/src/feature.module.ts apps/backend/src/features/feature/
-
Update module imports
// Before import { FeatureController } from './feature.controller'; import { FeatureService } from './feature.service'; // After import { FeatureController } from './controllers/feature.controller'; import { FeatureService } from './services/feature.service';
-
Update app.module imports
// Before import { FeatureModule } from './feature/feature.module'; // After import { FeatureModule } from './features/feature/feature.module';
-
Update cross-module imports
// Before import { OtherService } from '../other/other.service'; // After import { OtherService } from '../other/services/other.service';
Ensure modules properly export services:
// feature.module.ts
@Module({
imports: [TypeOrmModule.forFeature([Feature])],
controllers: [FeatureController],
providers: [FeatureService],
exports: [FeatureService], // Export for other modules
})
export class FeatureModule {}-
Run unit tests
npm run test -
Run integration tests
npm run test:integration
-
Run e2e tests
npm run test:e2e
-
Check for import errors
npm run build
- Update README files
- Update API documentation
- Update developer guides
- Update architecture diagrams
Create scripts/migrate-structure.sh:
#!/bin/bash
# Create new directories
mkdir -p src/features
mkdir -p src/database
mkdir -p src/config
# Move auth
mkdir -p src/auth/strategies src/auth/guards src/auth/decorators
mv src/auth/*.ts src/auth/
# Move features
for feature in courses users enrollments progress certificates credentials notifications analytics forums quizzes surveys leaderboard cohorts recommendations search moderation access-control coupons payouts kyc webhooks import-export batch reminders email cdn cache rate-limit api-usage audit health gateway metrics secrets stellar; do
if [ -d "src/$feature" ]; then
mkdir -p src/features/$feature/{controllers,services,entities,dto}
mv src/$feature/* src/features/$feature/ 2>/dev/null || true
rmdir src/$feature 2>/dev/null || true
fi
done
# Move migrations
mkdir -p src/database/migrations
mv src/migrations/* src/database/migrations/ 2>/dev/null || true
rmdir src/migrations 2>/dev/null || true
# Move config
mkdir -p src/config
mv src/config.ts src/config/ 2>/dev/null || true
echo "Migration complete!"If issues occur:
-
Revert git changes
git reset --hard HEAD
-
Restore from backup
# If you have a backup cp -r backup/src/* src/
- All files moved to new locations
- All imports updated
- All modules export correctly
- Unit tests pass
- Integration tests pass
- E2E tests pass
- Build succeeds
- No TypeScript errors
- Documentation updated
- Team notified of changes
Solution: Check import paths and ensure modules are exported
// Ensure service is exported from module
@Module({
providers: [FeatureService],
exports: [FeatureService], // Add this
})
export class FeatureModule {}Solution: Reorganize imports or use lazy loading
// Use forwardRef for circular dependencies
@Module({
imports: [forwardRef(() => OtherModule)],
})
export class FeatureModule {}Solution: Update test imports and mocks
// Update test imports
import { FeatureService } from '../services/feature.service';- Week 1: Prepare structure, create directories
- Week 2: Move core modules (auth, common)
- Week 3: Move feature modules (courses, users)
- Week 4: Move remaining modules, update imports
- Week 5: Testing and verification
- Week 6: Documentation and team training
- Announce changes to team
- Provide migration guide to developers
- Schedule training session on new structure
- Create support channel for questions
- Document lessons learned
- Monitor for issues in production
- Gather feedback from team
- Update CI/CD if needed
- Update IDE configurations (ESLint, Prettier)
- Archive old documentation