|
| 1 | +# Agents.md |
| 2 | + |
| 3 | +This file provides guidance to coding agents when working with code in this repository. |
| 4 | + |
| 5 | +## Essential Commands |
| 6 | + |
| 7 | +**Development:** |
| 8 | +- `pnpm run dev` - Start API server with hot reload on port 5000 |
| 9 | +- `pnpm run dev:background` - Start background processor |
| 10 | +- `pnpm run dev:temporal-worker` - Start Temporal worker |
| 11 | +- `pnpm run dev:temporal-server` - Start Temporal server for local development |
| 12 | + |
| 13 | +**Database:** |
| 14 | +- `pnpm run db:migrate:latest` - Apply latest migrations |
| 15 | +- `pnpm run db:migrate:reset` - Drop schema and rerun migrations |
| 16 | +- `pnpm run db:seed:import` - Import seed data for local development |
| 17 | +- `pnpm run db:migrate:make` - Generate new migration based on entity changes |
| 18 | +- `pnpm run db:migrate:create` - Create empty migration file |
| 19 | + |
| 20 | +**Building & Testing:** |
| 21 | +- `pnpm run build` - Compile TypeScript to build directory |
| 22 | +- `pnpm run lint` - Run ESLint (max 0 warnings) |
| 23 | +- `pnpm run test` - Run full test suite with database reset |
| 24 | +- `pnpm run cli` - Run CLI commands (e.g., `pnpm run cli api`) |
| 25 | + |
| 26 | +**Individual Test Execution:** |
| 27 | +- Remember to run only individual tests when possible for faster feedback |
| 28 | +- `NODE_ENV=test npx jest __tests__/specific-test.ts --testEnvironment=node --runInBand` |
| 29 | +- Use `--testEnvironment=node --runInBand` flags for database-dependent tests |
| 30 | + |
| 31 | +## High-Level Architecture |
| 32 | + |
| 33 | +**Core Framework Stack:** |
| 34 | +- **Fastify** - Web framework with plugins for CORS, helmet, cookies, rate limiting |
| 35 | +- **Mercurius** - GraphQL server with caching, upload support, and subscriptions |
| 36 | +- **TypeORM** - Database ORM with entity-based modeling and migrations |
| 37 | +- **PostgreSQL** - Primary database with master/slave replication setup. Favor read replica when you're ok with eventually consistent data. |
| 38 | +- **Redis** - Caching and pub/sub via ioRedisPool |
| 39 | +- **Temporal** - Workflow orchestration for background jobs |
| 40 | +- **ClickHouse** - Analytics and metrics storage |
| 41 | + |
| 42 | +**Application Entry Points:** |
| 43 | +- `src/index.ts` - Main Fastify server setup with GraphQL, auth, and middleware |
| 44 | +- `bin/cli.ts` - CLI dispatcher supporting api, background, temporal, cron modes |
| 45 | +- `src/background.ts` - Pub/Sub message handlers and background processing |
| 46 | +- `src/cron.ts` - Scheduled task execution |
| 47 | + |
| 48 | +**GraphQL Schema Organization:** |
| 49 | +- `src/graphql.ts` - Combines all schema modules with transformers and directives |
| 50 | +- `src/schema/` - GraphQL resolvers organized by domain (posts, users, feeds, etc.) |
| 51 | +- `src/directive/` - Custom GraphQL directives for auth, rate limiting, URL processing |
| 52 | +- **Docs**: See `src/graphorm/AGENTS.md` for comprehensive guide on using GraphORM to solve N+1 queries. GraphORM is the default and preferred method for all GraphQL query responses. Use GraphORM instead of TypeORM repositories for GraphQL resolvers to prevent N+1 queries and enforce best practices. |
| 53 | + |
| 54 | +**Data Layer:** |
| 55 | +- `src/entity/` - TypeORM entities defining database schema |
| 56 | +- `src/migration/` - Database migrations for schema evolution |
| 57 | +- `src/data-source.ts` - Database connection with replication configuration |
| 58 | + |
| 59 | +**Core Services:** |
| 60 | +- `src/Context.ts` - Request context with user, permissions, and data loaders |
| 61 | +- `src/auth.ts` - Authentication middleware and user context resolution |
| 62 | +- `src/dataLoaderService.ts` - Efficient batch loading for related entities |
| 63 | +- `src/workers/` - Use workers for async, non-critical operations (notifications, reputation, external syncs). Prefer `TypedWorker` for type safety. Architecture uses Google Pub/Sub + CDC (Debezium) for reactive processing. See `src/workers/AGENTS.md` for more. |
| 64 | +- `src/integrations/` - External service integrations (Slack, SendGrid, etc.) |
| 65 | +- `src/cron/` - Scheduled cron jobs for maintenance and periodic tasks. One file per cron, registered in `index.ts`, deployed via `.infra/crons.ts` Pulumi config. Each cron exports a `Cron` object with `name` and `handler(DataSource, logger, pubsub)`. Run locally with `pnpm run cli cron <name>`. See `src/cron/AGENTS.md` for more. |
| 66 | + |
| 67 | +**Type Safety & Validation:** |
| 68 | +- We favor type safety throughout the codebase. Use TypeScript interfaces and types for compile-time type checking. |
| 69 | +- **Zod schemas** are preferred for runtime validation, especially for input validation, API boundaries, and data parsing. Zod provides both type inference and runtime validation, making it ideal for verifying user input, API payloads, and external data sources. |
| 70 | +- When possible, prefer Zod schemas over manual validation as they provide type safety, better error messages, and can be inferred to TypeScript types. |
| 71 | + |
| 72 | +**Business Domains:** |
| 73 | +- **Content**: Posts, comments, bookmarks, feeds, sources |
| 74 | +- **Users**: Authentication, preferences, profiles, user experience |
| 75 | +- **Organizations**: Squad management, member roles, campaigns |
| 76 | +- **Notifications**: Push notifications, email digests, alerts |
| 77 | +- **Monetization**: Paddle subscription management, premium features |
| 78 | +- **Squads**: Squad management, member roles, campaigns |
| 79 | + |
| 80 | +**Testing Strategy:** |
| 81 | +- Jest with supertest for integration testing |
| 82 | +- Database reset before each test run via pretest hook |
| 83 | +- Fixtures in `__tests__/fixture/` for test data |
| 84 | +- Mercurius integration testing for GraphQL endpoints |
| 85 | + |
| 86 | +**Infrastructure Concerns:** |
| 87 | +- OpenTelemetry for distributed tracing and metrics |
| 88 | +- GrowthBook for feature flags and A/B testing |
| 89 | +- OneSignal for push notifications |
| 90 | +- Temporal workflows for async job processing |
| 91 | +- Rate limiting and caching at multiple layers |
0 commit comments