backend-claude-code 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/.claude/settings.json +42 -0
  2. package/.github/ISSUE_TEMPLATE/bug_report.md +35 -0
  3. package/.github/ISSUE_TEMPLATE/feature_request.md +33 -0
  4. package/.github/PULL_REQUEST_TEMPLATE.md +32 -0
  5. package/.mcp.json +19 -0
  6. package/CLAUDE.md +126 -0
  7. package/README.md +142 -0
  8. package/agents/code-reviewer.md +84 -0
  9. package/agents/database-reviewer.md +91 -0
  10. package/agents/java-build-resolver.md +127 -0
  11. package/agents/java-performance-reviewer.md +262 -0
  12. package/agents/planner.md +99 -0
  13. package/agents/security-reviewer.md +119 -0
  14. package/agents/tdd-guide.md +189 -0
  15. package/bin/cli.js +144 -0
  16. package/commands/db-migrate.md +134 -0
  17. package/commands/dev-build.md +72 -0
  18. package/commands/dev-coverage.md +73 -0
  19. package/commands/dev-fix.md +75 -0
  20. package/commands/dev-plan.md +501 -0
  21. package/commands/dev-review.md +144 -0
  22. package/commands/dev-run.md +385 -0
  23. package/commands/dev-test.md +89 -0
  24. package/commands/dev-verify.md +95 -0
  25. package/commands/dev.md +45 -0
  26. package/commands/git-commit.md +112 -0
  27. package/commands/git-issue.md +74 -0
  28. package/commands/git-pr.md +184 -0
  29. package/commands/git-push.md +28 -0
  30. package/package.json +24 -0
  31. package/rules/architecture.md +33 -0
  32. package/rules/coding-style.md +113 -0
  33. package/rules/controller-patterns.md +63 -0
  34. package/rules/dto-patterns.md +76 -0
  35. package/rules/entity-patterns.md +70 -0
  36. package/rules/error-handling.md +56 -0
  37. package/rules/hooks.md +73 -0
  38. package/rules/repository-patterns.md +75 -0
  39. package/rules/security.md +101 -0
  40. package/rules/service-patterns.md +70 -0
  41. package/rules/testing.md +174 -0
  42. package/skills/api-design/SKILL.md +523 -0
  43. package/skills/architecture-decision-records/SKILL.md +179 -0
  44. package/skills/database-migrations/SKILL.md +429 -0
  45. package/skills/hexagonal-architecture/SKILL.md +276 -0
  46. package/skills/java-coding-standards/SKILL.md +236 -0
  47. package/skills/jpa-patterns/SKILL.md +218 -0
  48. package/skills/postgres-patterns/SKILL.md +147 -0
  49. package/skills/springboot-patterns/SKILL.md +255 -0
  50. package/skills/springboot-security/SKILL.md +241 -0
  51. package/skills/springboot-tdd/SKILL.md +236 -0
@@ -0,0 +1,429 @@
1
+ ---
2
+ name: database-migrations
3
+ description: Database migration best practices for schema changes, data migrations, rollbacks, and zero-downtime deployments across PostgreSQL, MySQL, and common ORMs (Prisma, Drizzle, Kysely, Django, TypeORM, golang-migrate).
4
+ origin: ECC
5
+ ---
6
+
7
+ # Database Migration Patterns
8
+
9
+ Safe, reversible database schema changes for production systems.
10
+
11
+ ## When to Activate
12
+
13
+ - Creating or altering database tables
14
+ - Adding/removing columns or indexes
15
+ - Running data migrations (backfill, transform)
16
+ - Planning zero-downtime schema changes
17
+ - Setting up migration tooling for a new project
18
+
19
+ ## Core Principles
20
+
21
+ 1. **Every change is a migration** — never alter production databases manually
22
+ 2. **Migrations are forward-only in production** — rollbacks use new forward migrations
23
+ 3. **Schema and data migrations are separate** — never mix DDL and DML in one migration
24
+ 4. **Test migrations against production-sized data** — a migration that works on 100 rows may lock on 10M
25
+ 5. **Migrations are immutable once deployed** — never edit a migration that has run in production
26
+
27
+ ## Migration Safety Checklist
28
+
29
+ Before applying any migration:
30
+
31
+ - [ ] Migration has both UP and DOWN (or is explicitly marked irreversible)
32
+ - [ ] No full table locks on large tables (use concurrent operations)
33
+ - [ ] New columns have defaults or are nullable (never add NOT NULL without default)
34
+ - [ ] Indexes created concurrently (not inline with CREATE TABLE for existing tables)
35
+ - [ ] Data backfill is a separate migration from schema change
36
+ - [ ] Tested against a copy of production data
37
+ - [ ] Rollback plan documented
38
+
39
+ ## PostgreSQL Patterns
40
+
41
+ ### Adding a Column Safely
42
+
43
+ ```sql
44
+ -- GOOD: Nullable column, no lock
45
+ ALTER TABLE users ADD COLUMN avatar_url TEXT;
46
+
47
+ -- GOOD: Column with default (Postgres 11+ is instant, no rewrite)
48
+ ALTER TABLE users ADD COLUMN is_active BOOLEAN NOT NULL DEFAULT true;
49
+
50
+ -- BAD: NOT NULL without default on existing table (requires full rewrite)
51
+ ALTER TABLE users ADD COLUMN role TEXT NOT NULL;
52
+ -- This locks the table and rewrites every row
53
+ ```
54
+
55
+ ### Adding an Index Without Downtime
56
+
57
+ ```sql
58
+ -- BAD: Blocks writes on large tables
59
+ CREATE INDEX idx_users_email ON users (email);
60
+
61
+ -- GOOD: Non-blocking, allows concurrent writes
62
+ CREATE INDEX CONCURRENTLY idx_users_email ON users (email);
63
+
64
+ -- Note: CONCURRENTLY cannot run inside a transaction block
65
+ -- Most migration tools need special handling for this
66
+ ```
67
+
68
+ ### Renaming a Column (Zero-Downtime)
69
+
70
+ Never rename directly in production. Use the expand-contract pattern:
71
+
72
+ ```sql
73
+ -- Step 1: Add new column (migration 001)
74
+ ALTER TABLE users ADD COLUMN display_name TEXT;
75
+
76
+ -- Step 2: Backfill data (migration 002, data migration)
77
+ UPDATE users SET display_name = username WHERE display_name IS NULL;
78
+
79
+ -- Step 3: Update application code to read/write both columns
80
+ -- Deploy application changes
81
+
82
+ -- Step 4: Stop writing to old column, drop it (migration 003)
83
+ ALTER TABLE users DROP COLUMN username;
84
+ ```
85
+
86
+ ### Removing a Column Safely
87
+
88
+ ```sql
89
+ -- Step 1: Remove all application references to the column
90
+ -- Step 2: Deploy application without the column reference
91
+ -- Step 3: Drop column in next migration
92
+ ALTER TABLE orders DROP COLUMN legacy_status;
93
+
94
+ -- For Django: use SeparateDatabaseAndState to remove from model
95
+ -- without generating DROP COLUMN (then drop in next migration)
96
+ ```
97
+
98
+ ### Large Data Migrations
99
+
100
+ ```sql
101
+ -- BAD: Updates all rows in one transaction (locks table)
102
+ UPDATE users SET normalized_email = LOWER(email);
103
+
104
+ -- GOOD: Batch update with progress
105
+ DO $$
106
+ DECLARE
107
+ batch_size INT := 10000;
108
+ rows_updated INT;
109
+ BEGIN
110
+ LOOP
111
+ UPDATE users
112
+ SET normalized_email = LOWER(email)
113
+ WHERE id IN (
114
+ SELECT id FROM users
115
+ WHERE normalized_email IS NULL
116
+ LIMIT batch_size
117
+ FOR UPDATE SKIP LOCKED
118
+ );
119
+ GET DIAGNOSTICS rows_updated = ROW_COUNT;
120
+ RAISE NOTICE 'Updated % rows', rows_updated;
121
+ EXIT WHEN rows_updated = 0;
122
+ COMMIT;
123
+ END LOOP;
124
+ END $$;
125
+ ```
126
+
127
+ ## Prisma (TypeScript/Node.js)
128
+
129
+ ### Workflow
130
+
131
+ ```bash
132
+ # Create migration from schema changes
133
+ npx prisma migrate dev --name add_user_avatar
134
+
135
+ # Apply pending migrations in production
136
+ npx prisma migrate deploy
137
+
138
+ # Reset database (dev only)
139
+ npx prisma migrate reset
140
+
141
+ # Generate client after schema changes
142
+ npx prisma generate
143
+ ```
144
+
145
+ ### Schema Example
146
+
147
+ ```prisma
148
+ model User {
149
+ id String @id @default(cuid())
150
+ email String @unique
151
+ name String?
152
+ avatarUrl String? @map("avatar_url")
153
+ createdAt DateTime @default(now()) @map("created_at")
154
+ updatedAt DateTime @updatedAt @map("updated_at")
155
+ orders Order[]
156
+
157
+ @@map("users")
158
+ @@index([email])
159
+ }
160
+ ```
161
+
162
+ ### Custom SQL Migration
163
+
164
+ For operations Prisma cannot express (concurrent indexes, data backfills):
165
+
166
+ ```bash
167
+ # Create empty migration, then edit the SQL manually
168
+ npx prisma migrate dev --create-only --name add_email_index
169
+ ```
170
+
171
+ ```sql
172
+ -- migrations/20240115_add_email_index/migration.sql
173
+ -- Prisma cannot generate CONCURRENTLY, so we write it manually
174
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email ON users (email);
175
+ ```
176
+
177
+ ## Drizzle (TypeScript/Node.js)
178
+
179
+ ### Workflow
180
+
181
+ ```bash
182
+ # Generate migration from schema changes
183
+ npx drizzle-kit generate
184
+
185
+ # Apply migrations
186
+ npx drizzle-kit migrate
187
+
188
+ # Push schema directly (dev only, no migration file)
189
+ npx drizzle-kit push
190
+ ```
191
+
192
+ ### Schema Example
193
+
194
+ ```typescript
195
+ import { pgTable, text, timestamp, uuid, boolean } from "drizzle-orm/pg-core";
196
+
197
+ export const users = pgTable("users", {
198
+ id: uuid("id").primaryKey().defaultRandom(),
199
+ email: text("email").notNull().unique(),
200
+ name: text("name"),
201
+ isActive: boolean("is_active").notNull().default(true),
202
+ createdAt: timestamp("created_at").notNull().defaultNow(),
203
+ updatedAt: timestamp("updated_at").notNull().defaultNow(),
204
+ });
205
+ ```
206
+
207
+ ## Kysely (TypeScript/Node.js)
208
+
209
+ ### Workflow (kysely-ctl)
210
+
211
+ ```bash
212
+ # Initialize config file (kysely.config.ts)
213
+ kysely init
214
+
215
+ # Create a new migration file
216
+ kysely migrate make add_user_avatar
217
+
218
+ # Apply all pending migrations
219
+ kysely migrate latest
220
+
221
+ # Rollback last migration
222
+ kysely migrate down
223
+
224
+ # Show migration status
225
+ kysely migrate list
226
+ ```
227
+
228
+ ### Migration File
229
+
230
+ ```typescript
231
+ // migrations/2024_01_15_001_create_user_profile.ts
232
+ import { type Kysely, sql } from 'kysely'
233
+
234
+ // IMPORTANT: Always use Kysely<any>, not your typed DB interface.
235
+ // Migrations are frozen in time and must not depend on current schema types.
236
+ export async function up(db: Kysely<any>): Promise<void> {
237
+ await db.schema
238
+ .createTable('user_profile')
239
+ .addColumn('id', 'serial', (col) => col.primaryKey())
240
+ .addColumn('email', 'varchar(255)', (col) => col.notNull().unique())
241
+ .addColumn('avatar_url', 'text')
242
+ .addColumn('created_at', 'timestamp', (col) =>
243
+ col.defaultTo(sql`now()`).notNull()
244
+ )
245
+ .execute()
246
+
247
+ await db.schema
248
+ .createIndex('idx_user_profile_avatar')
249
+ .on('user_profile')
250
+ .column('avatar_url')
251
+ .execute()
252
+ }
253
+
254
+ export async function down(db: Kysely<any>): Promise<void> {
255
+ await db.schema.dropTable('user_profile').execute()
256
+ }
257
+ ```
258
+
259
+ ### Programmatic Migrator
260
+
261
+ ```typescript
262
+ import { Migrator, FileMigrationProvider } from 'kysely'
263
+ import { promises as fs } from 'fs'
264
+ import * as path from 'path'
265
+ // ESM only — CJS can use __dirname directly
266
+ import { fileURLToPath } from 'url'
267
+ const migrationFolder = path.join(
268
+ path.dirname(fileURLToPath(import.meta.url)),
269
+ './migrations',
270
+ )
271
+
272
+ // `db` is your Kysely<any> database instance
273
+ const migrator = new Migrator({
274
+ db,
275
+ provider: new FileMigrationProvider({
276
+ fs,
277
+ path,
278
+ migrationFolder,
279
+ }),
280
+ // WARNING: Only enable in development. Disables timestamp-ordering
281
+ // validation, which can cause schema drift between environments.
282
+ // allowUnorderedMigrations: true,
283
+ })
284
+
285
+ const { error, results } = await migrator.migrateToLatest()
286
+
287
+ results?.forEach((it) => {
288
+ if (it.status === 'Success') {
289
+ console.log(`migration "${it.migrationName}" executed successfully`)
290
+ } else if (it.status === 'Error') {
291
+ console.error(`failed to execute migration "${it.migrationName}"`)
292
+ }
293
+ })
294
+
295
+ if (error) {
296
+ console.error('migration failed', error)
297
+ process.exit(1)
298
+ }
299
+ ```
300
+
301
+ ## Django (Python)
302
+
303
+ ### Workflow
304
+
305
+ ```bash
306
+ # Generate migration from model changes
307
+ python manage.py makemigrations
308
+
309
+ # Apply migrations
310
+ python manage.py migrate
311
+
312
+ # Show migration status
313
+ python manage.py showmigrations
314
+
315
+ # Generate empty migration for custom SQL
316
+ python manage.py makemigrations --empty app_name -n description
317
+ ```
318
+
319
+ ### Data Migration
320
+
321
+ ```python
322
+ from django.db import migrations
323
+
324
+ def backfill_display_names(apps, schema_editor):
325
+ User = apps.get_model("accounts", "User")
326
+ batch_size = 5000
327
+ users = User.objects.filter(display_name="")
328
+ while users.exists():
329
+ batch = list(users[:batch_size])
330
+ for user in batch:
331
+ user.display_name = user.username
332
+ User.objects.bulk_update(batch, ["display_name"], batch_size=batch_size)
333
+
334
+ def reverse_backfill(apps, schema_editor):
335
+ pass # Data migration, no reverse needed
336
+
337
+ class Migration(migrations.Migration):
338
+ dependencies = [("accounts", "0015_add_display_name")]
339
+
340
+ operations = [
341
+ migrations.RunPython(backfill_display_names, reverse_backfill),
342
+ ]
343
+ ```
344
+
345
+ ### SeparateDatabaseAndState
346
+
347
+ Remove a column from the Django model without dropping it from the database immediately:
348
+
349
+ ```python
350
+ class Migration(migrations.Migration):
351
+ operations = [
352
+ migrations.SeparateDatabaseAndState(
353
+ state_operations=[
354
+ migrations.RemoveField(model_name="user", name="legacy_field"),
355
+ ],
356
+ database_operations=[], # Don't touch the DB yet
357
+ ),
358
+ ]
359
+ ```
360
+
361
+ ## golang-migrate (Go)
362
+
363
+ ### Workflow
364
+
365
+ ```bash
366
+ # Create migration pair
367
+ migrate create -ext sql -dir migrations -seq add_user_avatar
368
+
369
+ # Apply all pending migrations
370
+ migrate -path migrations -database "$DATABASE_URL" up
371
+
372
+ # Rollback last migration
373
+ migrate -path migrations -database "$DATABASE_URL" down 1
374
+
375
+ # Force version (fix dirty state)
376
+ migrate -path migrations -database "$DATABASE_URL" force VERSION
377
+ ```
378
+
379
+ ### Migration Files
380
+
381
+ ```sql
382
+ -- migrations/000003_add_user_avatar.up.sql
383
+ ALTER TABLE users ADD COLUMN avatar_url TEXT;
384
+ CREATE INDEX CONCURRENTLY idx_users_avatar ON users (avatar_url) WHERE avatar_url IS NOT NULL;
385
+
386
+ -- migrations/000003_add_user_avatar.down.sql
387
+ DROP INDEX IF EXISTS idx_users_avatar;
388
+ ALTER TABLE users DROP COLUMN IF EXISTS avatar_url;
389
+ ```
390
+
391
+ ## Zero-Downtime Migration Strategy
392
+
393
+ For critical production changes, follow the expand-contract pattern:
394
+
395
+ ```
396
+ Phase 1: EXPAND
397
+ - Add new column/table (nullable or with default)
398
+ - Deploy: app writes to BOTH old and new
399
+ - Backfill existing data
400
+
401
+ Phase 2: MIGRATE
402
+ - Deploy: app reads from NEW, writes to BOTH
403
+ - Verify data consistency
404
+
405
+ Phase 3: CONTRACT
406
+ - Deploy: app only uses NEW
407
+ - Drop old column/table in separate migration
408
+ ```
409
+
410
+ ### Timeline Example
411
+
412
+ ```
413
+ Day 1: Migration adds new_status column (nullable)
414
+ Day 1: Deploy app v2 — writes to both status and new_status
415
+ Day 2: Run backfill migration for existing rows
416
+ Day 3: Deploy app v3 — reads from new_status only
417
+ Day 7: Migration drops old status column
418
+ ```
419
+
420
+ ## Anti-Patterns
421
+
422
+ | Anti-Pattern | Why It Fails | Better Approach |
423
+ |-------------|-------------|-----------------|
424
+ | Manual SQL in production | No audit trail, unrepeatable | Always use migration files |
425
+ | Editing deployed migrations | Causes drift between environments | Create new migration instead |
426
+ | NOT NULL without default | Locks table, rewrites all rows | Add nullable, backfill, then add constraint |
427
+ | Inline index on large table | Blocks writes during build | CREATE INDEX CONCURRENTLY |
428
+ | Schema + data in one migration | Hard to rollback, long transactions | Separate migrations |
429
+ | Dropping column before removing code | Application errors on missing column | Remove code first, drop column next deploy |
@@ -0,0 +1,276 @@
1
+ ---
2
+ name: hexagonal-architecture
3
+ description: Design, implement, and refactor Ports & Adapters systems with clear domain boundaries, dependency inversion, and testable use-case orchestration across TypeScript, Java, Kotlin, and Go services.
4
+ origin: ECC
5
+ ---
6
+
7
+ # Hexagonal Architecture
8
+
9
+ Hexagonal architecture (Ports and Adapters) keeps business logic independent from frameworks, transport, and persistence details. The core app depends on abstract ports, and adapters implement those ports at the edges.
10
+
11
+ ## When to Use
12
+
13
+ - Building new features where long-term maintainability and testability matter.
14
+ - Refactoring layered or framework-heavy code where domain logic is mixed with I/O concerns.
15
+ - Supporting multiple interfaces for the same use case (HTTP, CLI, queue workers, cron jobs).
16
+ - Replacing infrastructure (database, external APIs, message bus) without rewriting business rules.
17
+
18
+ Use this skill when the request involves boundaries, domain-centric design, refactoring tightly coupled services, or decoupling application logic from specific libraries.
19
+
20
+ ## Core Concepts
21
+
22
+ - **Domain model**: Business rules and entities/value objects. No framework imports.
23
+ - **Use cases (application layer)**: Orchestrate domain behavior and workflow steps.
24
+ - **Inbound ports**: Contracts describing what the application can do (commands/queries/use-case interfaces).
25
+ - **Outbound ports**: Contracts for dependencies the application needs (repositories, gateways, event publishers, clock, UUID, etc.).
26
+ - **Adapters**: Infrastructure and delivery implementations of ports (HTTP controllers, DB repositories, queue consumers, SDK wrappers).
27
+ - **Composition root**: Single wiring location where concrete adapters are bound to use cases.
28
+
29
+ Outbound port interfaces usually live in the application layer (or in domain only when the abstraction is truly domain-level), while infrastructure adapters implement them.
30
+
31
+ Dependency direction is always inward:
32
+
33
+ - Adapters -> application/domain
34
+ - Application -> port interfaces (inbound/outbound contracts)
35
+ - Domain -> domain-only abstractions (no framework or infrastructure dependencies)
36
+ - Domain -> nothing external
37
+
38
+ ## How It Works
39
+
40
+ ### Step 1: Model a use case boundary
41
+
42
+ Define a single use case with a clear input and output DTO. Keep transport details (Express `req`, GraphQL `context`, job payload wrappers) outside this boundary.
43
+
44
+ ### Step 2: Define outbound ports first
45
+
46
+ Identify every side effect as a port:
47
+
48
+ - persistence (`UserRepositoryPort`)
49
+ - external calls (`BillingGatewayPort`)
50
+ - cross-cutting (`LoggerPort`, `ClockPort`)
51
+
52
+ Ports should model capabilities, not technologies.
53
+
54
+ ### Step 3: Implement the use case with pure orchestration
55
+
56
+ Use case class/function receives ports via constructor/arguments. It validates application-level invariants, coordinates domain rules, and returns plain data structures.
57
+
58
+ ### Step 4: Build adapters at the edge
59
+
60
+ - Inbound adapter converts protocol input to use-case input.
61
+ - Outbound adapter maps app contracts to concrete APIs/ORM/query builders.
62
+ - Mapping stays in adapters, not inside use cases.
63
+
64
+ ### Step 5: Wire everything in a composition root
65
+
66
+ Instantiate adapters, then inject them into use cases. Keep this wiring centralized to avoid hidden service-locator behavior.
67
+
68
+ ### Step 6: Test per boundary
69
+
70
+ - Unit test use cases with fake ports.
71
+ - Integration test adapters with real infra dependencies.
72
+ - E2E test user-facing flows through inbound adapters.
73
+
74
+ ## Architecture Diagram
75
+
76
+ ```mermaid
77
+ flowchart LR
78
+ Client["Client (HTTP/CLI/Worker)"] --> InboundAdapter["Inbound Adapter"]
79
+ InboundAdapter -->|"calls"| UseCase["UseCase (Application Layer)"]
80
+ UseCase -->|"uses"| OutboundPort["OutboundPort (Interface)"]
81
+ OutboundAdapter["Outbound Adapter"] -->|"implements"| OutboundPort
82
+ OutboundAdapter --> ExternalSystem["DB/API/Queue"]
83
+ UseCase --> DomainModel["DomainModel"]
84
+ ```
85
+
86
+ ## Suggested Module Layout
87
+
88
+ Use feature-first organization with explicit boundaries:
89
+
90
+ ```text
91
+ src/
92
+ features/
93
+ orders/
94
+ domain/
95
+ Order.ts
96
+ OrderPolicy.ts
97
+ application/
98
+ ports/
99
+ inbound/
100
+ CreateOrder.ts
101
+ outbound/
102
+ OrderRepositoryPort.ts
103
+ PaymentGatewayPort.ts
104
+ use-cases/
105
+ CreateOrderUseCase.ts
106
+ adapters/
107
+ inbound/
108
+ http/
109
+ createOrderRoute.ts
110
+ outbound/
111
+ postgres/
112
+ PostgresOrderRepository.ts
113
+ stripe/
114
+ StripePaymentGateway.ts
115
+ composition/
116
+ ordersContainer.ts
117
+ ```
118
+
119
+ ## TypeScript Example
120
+
121
+ ### Port definitions
122
+
123
+ ```typescript
124
+ export interface OrderRepositoryPort {
125
+ save(order: Order): Promise<void>;
126
+ findById(orderId: string): Promise<Order | null>;
127
+ }
128
+
129
+ export interface PaymentGatewayPort {
130
+ authorize(input: { orderId: string; amountCents: number }): Promise<{ authorizationId: string }>;
131
+ }
132
+ ```
133
+
134
+ ### Use case
135
+
136
+ ```typescript
137
+ type CreateOrderInput = {
138
+ orderId: string;
139
+ amountCents: number;
140
+ };
141
+
142
+ type CreateOrderOutput = {
143
+ orderId: string;
144
+ authorizationId: string;
145
+ };
146
+
147
+ export class CreateOrderUseCase {
148
+ constructor(
149
+ private readonly orderRepository: OrderRepositoryPort,
150
+ private readonly paymentGateway: PaymentGatewayPort
151
+ ) {}
152
+
153
+ async execute(input: CreateOrderInput): Promise<CreateOrderOutput> {
154
+ const order = Order.create({ id: input.orderId, amountCents: input.amountCents });
155
+
156
+ const auth = await this.paymentGateway.authorize({
157
+ orderId: order.id,
158
+ amountCents: order.amountCents,
159
+ });
160
+
161
+ // markAuthorized returns a new Order instance; it does not mutate in place.
162
+ const authorizedOrder = order.markAuthorized(auth.authorizationId);
163
+ await this.orderRepository.save(authorizedOrder);
164
+
165
+ return {
166
+ orderId: order.id,
167
+ authorizationId: auth.authorizationId,
168
+ };
169
+ }
170
+ }
171
+ ```
172
+
173
+ ### Outbound adapter
174
+
175
+ ```typescript
176
+ export class PostgresOrderRepository implements OrderRepositoryPort {
177
+ constructor(private readonly db: SqlClient) {}
178
+
179
+ async save(order: Order): Promise<void> {
180
+ await this.db.query(
181
+ "insert into orders (id, amount_cents, status, authorization_id) values ($1, $2, $3, $4)",
182
+ [order.id, order.amountCents, order.status, order.authorizationId]
183
+ );
184
+ }
185
+
186
+ async findById(orderId: string): Promise<Order | null> {
187
+ const row = await this.db.oneOrNone("select * from orders where id = $1", [orderId]);
188
+ return row ? Order.rehydrate(row) : null;
189
+ }
190
+ }
191
+ ```
192
+
193
+ ### Composition root
194
+
195
+ ```typescript
196
+ export const buildCreateOrderUseCase = (deps: { db: SqlClient; stripe: StripeClient }) => {
197
+ const orderRepository = new PostgresOrderRepository(deps.db);
198
+ const paymentGateway = new StripePaymentGateway(deps.stripe);
199
+
200
+ return new CreateOrderUseCase(orderRepository, paymentGateway);
201
+ };
202
+ ```
203
+
204
+ ## Multi-Language Mapping
205
+
206
+ Use the same boundary rules across ecosystems; only syntax and wiring style change.
207
+
208
+ - **TypeScript/JavaScript**
209
+ - Ports: `application/ports/*` as interfaces/types.
210
+ - Use cases: classes/functions with constructor/argument injection.
211
+ - Adapters: `adapters/inbound/*`, `adapters/outbound/*`.
212
+ - Composition: explicit factory/container module (no hidden globals).
213
+ - **Java**
214
+ - Packages: `domain`, `application.port.in`, `application.port.out`, `application.usecase`, `adapter.in`, `adapter.out`.
215
+ - Ports: interfaces in `application.port.*`.
216
+ - Use cases: plain classes (Spring `@Service` is optional, not required).
217
+ - Composition: Spring config or manual wiring class; keep wiring out of domain/use-case classes.
218
+ - **Kotlin**
219
+ - Modules/packages mirror the Java split (`domain`, `application.port`, `application.usecase`, `adapter`).
220
+ - Ports: Kotlin interfaces.
221
+ - Use cases: classes with constructor injection (Koin/Dagger/Spring/manual).
222
+ - Composition: module definitions or dedicated composition functions; avoid service locator patterns.
223
+ - **Go**
224
+ - Packages: `internal/<feature>/domain`, `application`, `ports`, `adapters/inbound`, `adapters/outbound`.
225
+ - Ports: small interfaces owned by the consuming application package.
226
+ - Use cases: structs with interface fields plus explicit `New...` constructors.
227
+ - Composition: wire in `cmd/<app>/main.go` (or dedicated wiring package), keep constructors explicit.
228
+
229
+ ## Anti-Patterns to Avoid
230
+
231
+ - Domain entities importing ORM models, web framework types, or SDK clients.
232
+ - Use cases reading directly from `req`, `res`, or queue metadata.
233
+ - Returning database rows directly from use cases without domain/application mapping.
234
+ - Letting adapters call each other directly instead of flowing through use-case ports.
235
+ - Spreading dependency wiring across many files with hidden global singletons.
236
+
237
+ ## Migration Playbook
238
+
239
+ 1. Pick one vertical slice (single endpoint/job) with frequent change pain.
240
+ 2. Extract a use-case boundary with explicit input/output types.
241
+ 3. Introduce outbound ports around existing infrastructure calls.
242
+ 4. Move orchestration logic from controllers/services into the use case.
243
+ 5. Keep old adapters, but make them delegate to the new use case.
244
+ 6. Add tests around the new boundary (unit + adapter integration).
245
+ 7. Repeat slice-by-slice; avoid full rewrites.
246
+
247
+ ### Refactoring Existing Systems
248
+
249
+ - **Strangler approach**: keep current endpoints, route one use case at a time through new ports/adapters.
250
+ - **No big-bang rewrites**: migrate per feature slice and preserve behavior with characterization tests.
251
+ - **Facade first**: wrap legacy services behind outbound ports before replacing internals.
252
+ - **Composition freeze**: centralize wiring early so new dependencies do not leak into domain/use-case layers.
253
+ - **Slice selection rule**: prioritize high-churn, low-blast-radius flows first.
254
+ - **Rollback path**: keep a reversible toggle or route switch per migrated slice until production behavior is verified.
255
+
256
+ ## Testing Guidance (Same Hexagonal Boundaries)
257
+
258
+ - **Domain tests**: test entities/value objects as pure business rules (no mocks, no framework setup).
259
+ - **Use-case unit tests**: test orchestration with fakes/stubs for outbound ports; assert business outcomes and port interactions.
260
+ - **Outbound adapter contract tests**: define shared contract suites at port level and run them against each adapter implementation.
261
+ - **Inbound adapter tests**: verify protocol mapping (HTTP/CLI/queue payload to use-case input and output/error mapping back to protocol).
262
+ - **Adapter integration tests**: run against real infrastructure (DB/API/queue) for serialization, schema/query behavior, retries, and timeouts.
263
+ - **End-to-end tests**: cover critical user journeys through inbound adapter -> use case -> outbound adapter.
264
+ - **Refactor safety**: add characterization tests before extraction; keep them until new boundary behavior is stable and equivalent.
265
+
266
+ ## Best Practices Checklist
267
+
268
+ - Domain and use-case layers import only internal types and ports.
269
+ - Every external dependency is represented by an outbound port.
270
+ - Validation occurs at boundaries (inbound adapter + use-case invariants).
271
+ - Use immutable transformations (return new values/entities instead of mutating shared state).
272
+ - Errors are translated across boundaries (infra errors -> application/domain errors).
273
+ - Composition root is explicit and easy to audit.
274
+ - Use cases are testable with simple in-memory fakes for ports.
275
+ - Refactoring starts from one vertical slice with behavior-preserving tests.
276
+ - Language/framework specifics stay in adapters, never in domain rules.