Skip to main content

@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

Use it:

More Examples

Context in HTTP:
Context in model pipeline:

Runtime And Lifecycle Notes

  • loadModel(model, input) rejects if the first argument was not created by defineModel().
  • Model input must be an object.
  • Services are loaded in the order of the services tuple.
  • Pipeline middleware follows Koa-style await next().
  • The terminal model result is stored in ctx.state.result and returned by loadModel().
  • 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.

HTTP API + Model + TypeORM

Complete Example

Controller:
Model:
Boot file:
Package config:

Package-Local AI Guide

This package also ships AI.md in npm so agents can read accurate examples after installation.