Coding Standard: Node (NestJS)
Node/NestJS is used where the dominant shape is event/pipeline orchestration rather than domain CRUD — see ADR-00010 for why. This doc is the concrete rulebook a Node service is actually held to; Architecture-00007 covers the same shape across all three backend languages side by side.
Services on this standard
tenant-migration, tenant-migration-orchestrator, plus firebase-function (Firebase Cloud Functions runtime, non-HTTP triggers).
Folder layout — NestJS module structure
<feature>/
<feature>.entity.ts ← pure business types
<feature>.validation.ts ← validation — always its own file
<feature>.repository.ts ← every outbound I/O call — Mongo, Pub/Sub publish, third-party HTTP
<feature>.service.ts ← business orchestration
<feature>.controller.ts ← REST controller
message/* ← Pub/Sub envelope + publish/receive helpers
migration/* ← fan-out helpers (runInNewTrace)
Follow @templates/node-mongo-microservice/ when scaffolding a new Node service — read its own CLAUDE.md first. tenant-migration/tenant-migration-orchestrator predate this template and are its source pattern, not scaffolded from it.
Error handling
Use core/errors.js’s typed errors — never throw a native Error. Typed-error coverage is currently inconsistent across the library (a known gap); still prefer the typed constructor over a native throw in every new call site.
The domain object (DTO) parameter pattern
One domain object per service-method call, never several positional scalars — same rule as Go and Rust, applied at every layer boundary.
Validation — its own file, always
<feature>.validation.ts is separate from <feature>.entity.ts — never inline.
Request context — runWithRequestContext / runInNewTrace
This is Node’s equivalent of Go’s otel.WithSpan[T], backed by AsyncLocalStorage (core/request-store.js) rather than OTel span attributes alone. Wrap every service/repository/controller method body in it, span name convention "Type.Method":
return runWithRequestContext('Type.Method', async () => {
// getRequestContext() here sees tenant_id/user_id from the root scope automatically —
// no need to capture it before this call and re-pass it as a third argument.
});
A nested call does not need to re-read and re-pass getRequestContext()’s result as its own initialStore.
runInNewTrace(name, fn, initialStore) — same signature, but always mints a fresh root trace_id, even from inside an already-active span. Use only at a genuine fan-out point where one unit of work spawns many independent downstream units (e.g. migration/publish-migration-func.js’s publishMigrationItem) — don’t reach for this as a default.
Root scope is opened once per request by RequestContextInterceptor (NestJS HTTP) or by the Pub/Sub receiver’s withMessageReceiver callback (worker services, seeded from the envelope’s tenant_id/correlation_id/traceparent). There is no codegen equivalent of Go’s gowrap — every wrap is hand-written.
Deviation to know about: RequestContextInterceptor is normally applied globally via app.useGlobalInterceptors(...), but it internally uses firstValueFrom(next.handle()), which resolves only the first emission of an Observable — applied globally this would truncate a long-lived SSE route to one emission. tenant-migration instead applies @UseInterceptors(RequestContextInterceptor) per-handler, excluding its stream/health routes — a documented, deliberate deviation, not a bug.
Auth extraction
node/auth.middleware.js’s authMiddleware reads X-Tenant-ID/X-User-Id/X-Role, responds 401 via a typed AppError if any is missing, and assigns req.tenantId/req.userId/req.role. There is no Node-side JWT decoding — that’s Nginx + user-secure-engine’s job. authMiddleware’s span-attribute calls no longer no-op — pair it with node/request-context.interceptor.js; if a module’s own CLAUDE.md still describes the old no-op behavior, treat this as stale.
Fail-fast required-env-var boot check
Validate all required env vars at process boot and refuse to start if any are missing, rather than failing lazily on first use — the pattern already followed by tenant-migration/tenant-migration-orchestrator.
No over-engineering
No extra abstractions, error handling, or features beyond what the task requires.
Comments
No comments unless the WHY is genuinely non-obvious.
Known parity gaps against Go/Rust (don’t fabricate parity)
- No TypeScript /
.d.ts— zero compile-time checking for any consumer call intonode-lang-core-lib. node/core-logger.jsandfirebase/core-logger.jsare copy-pasted, not shared — a fix to one does not propagate to the other.index.js’s barrel export omitsnode/*andfirebase/*— alwaysrequire()those by full subpath.- No
gowrap-equivalent tracing-decorator codegen — everyrunWithRequestContextcall is added by hand. - No Mongo auto-close hook, no ping-based readiness check (deliberate divergence, not an oversight).
- No server-side HTTP request-duration metric yet (Node’s gap vs. Go/Rust’s
http.server.request.duration). file:../node-lang-core-libconsumption needs a one-timenpm installin that directory plusNODE_OPTIONS=--preserve-symlinkson the consumer — neither enforced automatically.