Skip to main content
The pnpm sonamu 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.
If you omit the Entity in a TTY, an interactive prompt asks for it:
scaffold model fails when the Model file already exists. To regenerate it, see Regenerating Model.
You cannot scaffold a Model for the Entities where better-auth stores credentials (Session, Account, Verification, TwoFactor, Passkey, Jwks, ApiKey, SsoProvider). The generated findById, findMany, and save have no guards, so anyone could read or overwrite session tokens and password hashes without signing in. If you need this data, use the better-auth API, or write only the methods you need with an admin guard. The refusal applies when both the Entity ID and table match the better-auth definition (for example, Session and sessions), and also covers Model generation and preview in Sonamu UI.
Generated files: Generated code:
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.
Generated files: Generated code:
src/application/user/user.model.test.ts
The generated test starts as describe.skip. Remove .skip once you have written real assertions.

View Scaffolding

Admin list and form components can also be generated from the CLI.
Generated files:
The same generators are available in the Scaffolding tab of Sonamu UI.

Generated Code Structure

Model Class

Generated Model classes have the following structure:
Auto-generated features:
  • ✅ TypeScript types
  • ✅ Subset/Loader query wiring
  • ✅ BaseModelClass inheritance
  • ✅ Basic CRUD methods (findById/findOne/findMany/save/del)
You need to add:
  • Business logic methods
  • Complex queries
  • Data validation
  • Relation loading

Test File

Generated test files include basic tests:
Extensible:
  • 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 scaffold batch

Batch Test Generation

scaffold batch is a dry-run unless you pass --execute. In a TTY it asks for confirmation; add --confirm for non-interactive runs. Existing files are skipped unless you add --overwrite. Use scaffold status or scaffold preview with the same --entity/--template selectors to check which files exist and what would be written.

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