App conventions
Nest apps
Rules for NestJS apps, currently apps/api.
Layout
src/features/<names>/: one feature each, named in the plural. Files:<name>.module.ts,.controller.ts,.service.ts,dto/,response/,libs/,index.ts.src/common/<module>/: code that two or more features use. Import it through its barrel; a module over five files splits into role folders with their own barrels.- Filenames are kebab-case with a Nest role suffix:
.service.ts,.guard.ts,.dto.ts. - Reads from ClickHouse live in
@openperf/server, together with their response schemas. The controller calls them directly or through the feature's service; a service owns the checks around a read, such as access to the parent record and the not-found error.
Requests and responses
- Schemas are zod:
dto/<name>.dto.tsexports<name>Dto,response/<name>.response.tsexports<name>Response. Request types come fromz.infer, response types fromz.input. - Declare both sides on the handler:
@Query({ schema })or@Body({ schema }),@ApiResult(schema), and@ApiErrors()on the controller. - Body and response schemas get
.meta({ id }); query schemas don't. Response schemas have no.transform(); time-based defaults belong in the request. - Services return database rows; the response schema drops columns it doesn't list. A
Datecolumn usesdateTimeSchemafromcommon/schema, which sends an ISO string. Leave secret columns out of the select, asomit(getTableColumns(table), ["secret"])does. - Errors carry a stable code from
buildApiError;GlobalExceptionFilterbuilds the response.
Access
- Every route declares
@Access("read" | "write" | "authenticated")or@Public(true), on the controller and per handler where it differs.src/app.module.test.tschecks every route. - API tokens reach only
@AllowApiToken(true)routes, and read tokens can't write. Read the caller with@Actor().
Code
- Only entry points call
readEnvironment(). Providers injectENVIRONMENT, functions take values as parameters, tests usecreateTestEnvironment(). - Slow or retried work goes to
worker-jobsthroughJobQueueService. Pass the transaction when the job must commit with a row change. AuditService.record(event)reads the actor from the current request. Pass a context only where no request carries it, such as an inbound webhook.- Never
import typea class used for dependency injection. - Route handlers and migrations are exempt from verb-first names. Prefer Bun APIs.