backend-patterns
Backend entity patterns - services with DI, Drizzle schemas (3-file pattern), ORPC routers, TypeIDs, neverthrow. Use when creating services, schemas, routers, or new entities.
When & Why to Use This Skill
This Claude skill streamlines backend development by enforcing consistent architectural patterns and best practices. It provides a standardized framework for building services with Dependency Injection (DI), Drizzle ORM schemas using a 3-file pattern, and type-safe ORPC routers. By integrating tools like neverthrow for functional error handling and TypeIDs for prefixed identifiers, it ensures high maintainability, type safety, and scalability in modern TypeScript monorepos.
Use Cases
- Scenario 1: Scaffolding new database entities using a structured 3-file pattern (DB schema, Zod validation, and relations) to ensure Drizzle ORM consistency.
- Scenario 2: Implementing robust backend services using factory functions and dependency injection to improve testability and modularity.
- Scenario 3: Building type-safe API layers with ORPC routers that seamlessly map service results to standardized error responses using the neverthrow pattern.
- Scenario 4: Maintaining architectural integrity in a monorepo by following predefined conventions for shared packages, TypeID prefixes, and centralized logging.
| name | backend-patterns |
|---|---|
| description | Backend entity patterns - services with DI, Drizzle schemas (3-file pattern), ORPC routers, TypeIDs, neverthrow. Use when creating services, schemas, routers, or new entities. |
Backend Patterns
Use this skill when building backend features. Follow these patterns for consistency.
Project Setup
Replace these placeholders for your project:
@project/→ your package scope (e.g.,@myapp/)packages/paths follow standard monorepo layout
Expected packages:
@project/db- Drizzle database client, DB_SCHEMA export@project/shared- TypeIDs, constants, shared schemas@project/logger- Pino logger wrapper
Quick Reference
| Entity Type | Pattern | Files to Create/Modify |
|---|---|---|
| Service | Factory function with deps, neverthrow | packages/api/src/services/xxx-service.ts, context.ts |
| Schema | 3-file pattern | .db.ts, .zod.ts, .relations.ts in packages/db/src/schema/ |
| Router | ORPC procedures, Result→Error mapping | packages/api/src/routers/xxx-router.ts, routers.ts |
| TypeID | Prefixed ID | packages/shared/src/typeid.schema.ts |
Key Conventions
- db.query preferred over db.select for simple lookups (see service.md)
- neverthrow for service error handling,
.match()in routers (see router.md) - Zod enums with
as const+z.enum()pattern (see schema.md)
Pattern Details
- For service creation, see service.md
- For schema creation, see schema.md
- For router creation, see router.md
Workflow: Adding a New Entity
- Add TypeID prefix to
packages/shared/src/typeid.schema.ts - Create schema files in
packages/db/src/schema/{domain}/ - Create service in
packages/api/src/services/ - Add to context in
packages/api/src/context.ts - Create router in
packages/api/src/routers/ - Export router in
packages/api/src/routers/routers.ts - Run
bun run db:generateif schema changed