pnpm scaffold command auto-generates boilerplate code based on Entity definitions. It generates Model classes and test files in seconds, significantly reducing development time.
Basic Concept
Scaffold is an automation tool that converts Entities to code:- Entity-based: Understand types and structure from Entity definitions
- Type-safe: TypeScript types are auto-generated
- Consistent: All code follows the same pattern
- Customizable: Freely customize after generation
Commands
model - Generate Model Class
Generate a Model class for an Entity.src/application/user/user.model.ts
The real output contains full bodies for
findById/findOne/findMany/save/del; they are
elided above for brevity. When the Entity has a string primary key, id is generated as string.model_test - Generate Test File
Generate a test file for a Model.src/application/user/user.model.test.ts
View Scaffolding
Admin list and form components can also be generated from the CLI.The same generators are available in the Scaffolding tab of Sonamu UI.
Generated Code Structure
Model Class
Generated Model classes have the following structure:- ✅ TypeScript types
- ✅ Subset/Loader query wiring
- ✅ BaseModelClass inheritance
- ✅ Basic CRUD methods (
findById/findOne/findMany/save/del)
- Business logic methods
- Complex queries
- Data validation
- Relation loading
Test File
Generated test files include basic tests:- Add business logic tests
- Edge case tests
- Performance tests
- Integration tests
Development Workflow
1. Define Entity
src/application/product/product.entity.json
2. Migration
3. Generate Model
4. Add Business Logic
src/application/product/product.model.ts
5. Generate Tests
6. Write Tests
src/application/product/product.model.test.ts
7. Add API
src/application/product/product.model.ts
Customization
Adding Model Methods
Add business logic to generated Models:Extending Tests
Add more test cases:Batch Generation for Multiple Entities
Using Scripts
Batch Test Generation
Practical Tips
1. Complete Entity First
2. Incremental Extension
3. Follow Conventions
4. Git Management
Troubleshooting
Regenerating Model
Problem: Want to regenerate Model Solution:Syncing After Entity Change
Problem: Update Model after Entity change Solution:Next Steps
Model
Learn more about Model classes
Testing
Write tests