Anarchitecture Bricks Docs

Repository documentation hub for packages, guides, and generated references.

Nest Guide

Intent

This is the Nest implementation cookbook for Anarchitecture Bricks. It focuses on backend module composition and entry-point usage, while shared cross-stack contracts stay canonical in:

Architecture

Easy Mode with Composition Modules

Use facade composition modules for the default host-app path:

import { Module } from '@nestjs/common';
import { FormsModule } from '@anarchitects/forms-nest';
import { AuthModule, provideAuthRuntimeGuards } from '@anarchitects/auth-nest';

@Module({
  imports: [FormsModule.forRoot({}), FormsModule.forRootFromConfig(), AuthModule.forRoot({}), AuthModule.forRootFromConfig()],
  providers: [...provideAuthRuntimeGuards()],
})
export class AppModule {}

Easy mode is preferred when teams want predictable integration and minimal Nest wiring. Activate auth centrally from the root module providers instead of binding guards on individual controllers.

Advanced Mode with Layer Entry Points

Use layered entry points when you need explicit control over domain composition:

Advanced mode is for replacing adapters, customizing policy wiring, or composing only selected layers while preserving facade compatibility.

TS Contract Integration

Contract ownership is TS-first:

Nest libraries (@anarchitects/forms-nest, @anarchitects/auth-nest) map these contracts in presentation/application layers and keep schemas synchronized for OpenAPI generation. Ownership and compatibility policy are defined in TS Contracts Guide.

Auth CASL Guidance

For @anarchitects/auth-nest, document and preserve the two-layer authorization model:

This split is required for ownership-sensitive rules. Route metadata alone cannot prove that a user may update a specific post; the resource must be loaded before CASL can make that final decision.

Typical composition:

import { Controller, Patch } from '@nestjs/common';
import { AuthorizeResource, Policies } from '@anarchitects/auth-declarations';
import { AuthorizedResource, AuthModule, provideAuthRuntimeGuards } from '@anarchitects/auth-nest';

@Controller('posts')
export class PostsController {
  @Patch(':postId')
  @Policies({ action: 'update', subject: 'Post' })
  @AuthorizeResource({ action: 'update', subject: 'Post', idParam: 'postId' })
  updatePost(@AuthorizedResource() post: Post) {
    return post;
  }
}

@Module({
  imports: [AuthModule.forRoot({})],
  providers: [...provideAuthRuntimeGuards()],
})
export class AppModule {}

Malformed persisted permission payloads are treated as trust-boundary failures and must fail closed instead of being partially trusted.

Library Entry Point Cookbook

Schema Evolution and Compatibility

Contract Verification Workflow

Common Pitfalls