|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +MikroORM Getting Started guide — a blog API built with Fastify, MikroORM v7 (SQLite driver), and TypeScript. ESM-only (`"type": "module"` in package.json). |
| 8 | + |
| 9 | +## Commands |
| 10 | + |
| 11 | +- **Build:** `npm run build` (runs `tsc`) |
| 12 | +- **Bundle:** `npm run bundle` (Vite SSR build → `dist/`) |
| 13 | +- **Dev server:** `npm run start` (runs `tsx src/server.ts` on port 3001) |
| 14 | +- **Run all tests:** `npm test` (runs `vitest`) |
| 15 | +- **Run a single test:** `npx vitest run test/user.test.ts` |
| 16 | +- **MikroORM CLI:** `npx mikro-orm` (config at `src/mikro-orm.config.ts`) |
| 17 | +- **Create migration:** `npx mikro-orm migration:create` |
| 18 | + |
| 19 | +## Architecture |
| 20 | + |
| 21 | +### Entity Definition Pattern (v7 `defineEntity` API) |
| 22 | + |
| 23 | +Entities use MikroORM v7's `defineEntity()` + `p.*` property builder pattern, **not** decorators: |
| 24 | + |
| 25 | +```ts |
| 26 | +export const ArticleSchema = defineEntity({ |
| 27 | + name: 'Article', |
| 28 | + extends: BaseSchema, |
| 29 | + properties: { |
| 30 | + title: p.string().index(), |
| 31 | + author: () => p.manyToOne(UserSchema).ref(), // arrow function for circular refs |
| 32 | + }, |
| 33 | +}); |
| 34 | +export type IArticle = InferEntity<typeof ArticleSchema>; |
| 35 | +``` |
| 36 | + |
| 37 | +Key conventions: |
| 38 | +- Schema objects are exported (e.g., `ArticleSchema`) and registered in `mikro-orm.config.ts` |
| 39 | +- Types are inferred with `InferEntity<typeof Schema>` (e.g., `IArticle`, `ITag`, `IComment`) |
| 40 | +- Only `User` has a custom class (via `setClass`) for `verifyPassword()` method |
| 41 | +- `BaseSchema` (in `src/modules/common/base.entity.ts`) provides `id`, `createdAt`, `updatedAt` |
| 42 | +- Relations use arrow-function property definitions to handle circular imports |
| 43 | +- Entity imports use `@mikro-orm/core`; driver-specific imports (`@mikro-orm/sqlite`) are used only in `db.ts` and custom repositories |
| 44 | + |
| 45 | +### Module Structure |
| 46 | + |
| 47 | +Code is organized in `src/modules/{module}/`: |
| 48 | +- **user/** — `User` entity (with custom class), `UserRepository`, routes (sign-up, sign-in, profile) |
| 49 | +- **article/** — `Article`, `Comment`, `Tag` schemas + `ArticleListing` virtual entity, `ArticleRepository`, routes |
| 50 | +- **common/** — `BaseSchema`, `AuthError`, helper functions, `SoftDeleteSubscriber` |
| 51 | + |
| 52 | +### ORM Service Layer (`src/db.ts`) |
| 53 | + |
| 54 | +`initORM()` synchronously initializes MikroORM (via `new MikroORM()`) and returns a cached `Services` object with typed repository accessors (`db.user`, `db.article`, `db.comment`, `db.tag`). Both app code and tests call this — tests pass overrides (`:memory:` DB, debug off). |
| 55 | + |
| 56 | +### App Bootstrap (`src/app.ts`) |
| 57 | + |
| 58 | +`bootstrap()` creates the Fastify app with: |
| 59 | +1. JWT plugin for auth |
| 60 | +2. `RequestContext` hook (per-request MikroORM identity map) |
| 61 | +3. Auth hook (decodes JWT, loads `request.user`) |
| 62 | +4. Error handler mapping `NotFoundError` → 404, `AuthError` → 401 |
| 63 | +5. Route registration with `/user` and `/article` prefixes |
| 64 | + |
| 65 | +### Testing |
| 66 | + |
| 67 | +Tests use in-memory SQLite (`:memory:`) with schema created fresh + `TestSeeder` for fixtures. Each test file uses a different port for parallel execution. Tests use Fastify's `app.inject()` for HTTP assertions without a real server. |
| 68 | + |
| 69 | +### Notable Patterns |
| 70 | + |
| 71 | +- **Soft delete:** `SoftDeleteSubscriber` intercepts DELETE change sets and converts them to UPDATE with `deletedAt` timestamp. `Comment` entity has a `softDelete` filter enabled by default. |
| 72 | +- **Virtual entity:** `ArticleListing` uses an expression callback (returns QueryBuilder) instead of a database table. |
| 73 | +- **Lazy properties:** `User.password` and `Article.text` are lazy-loaded (not fetched unless explicitly populated). |
| 74 | +- **Ref wrapper:** Foreign keys use `.ref()` for type-safe references without loading the full entity. |
| 75 | +- **Zod validation:** Route handlers use Zod schemas for request body validation. |
0 commit comments