@hile/model
Put reusable business logic behind typed models with services and pipeline middleware.Choose This Package When
Use When
Use@hile/model for reusable business logic and @hile/context for request/work-unit context that should flow through async calls, micro messages, and queue jobs.
Do Not Use When
- Do not put business logic only in controllers when it will be reused by jobs, pages, or message handlers.
- Do not add fixed business fields to
@hile/context; each app owns its context shape.
Install
Imports
Copy-Paste Example
More Examples
Runtime And Lifecycle Notes
loadModel(model, input)rejects if the first argument was not created bydefineModel().- Model input must be an object.
- Services are loaded in the order of the
servicestuple. - Pipeline middleware follows Koa-style
await next(). - The terminal model result is stored in
ctx.state.resultand returned byloadModel(). runWithContext()merges with parent context by default; pass{ merge: false }to replace.getContext()returns a frozen shallow snapshot.requireContext(keys)throws when selected keys are missing.
Anti-Patterns
- Passing primitives to
loadModel(). - Mutating context snapshots.
- Logging whole context objects by default.
- Assuming context propagation changes business payloads; it should stay in metadata or async storage.
Verification Checklist
- Models export
defineModel(...)results. - Controllers and pages call
loadModel(model, objectInput). - Pipeline middleware writes derived state to
ctx.state. - Context keys are app-defined and JSON-serializable when crossing process boundaries.
Related Recipes
HTTP API + Model + TypeORM
Complete Example
Controller:Package-Local AI Guide
This package also shipsAI.md in npm so agents can read accurate examples after installation.