backend-patterns

grmkris's avatarfrom grmkris

Backend entity patterns - services with DI, Drizzle schemas (3-file pattern), ORPC routers, TypeIDs, neverthrow. Use when creating services, schemas, routers, or new entities.

0stars🔀0forks📁View on GitHub🕐Updated Dec 30, 2025

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.
namebackend-patterns
descriptionBackend 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

Workflow: Adding a New Entity

  1. Add TypeID prefix to packages/shared/src/typeid.schema.ts
  2. Create schema files in packages/db/src/schema/{domain}/
  3. Create service in packages/api/src/services/
  4. Add to context in packages/api/src/context.ts
  5. Create router in packages/api/src/routers/
  6. Export router in packages/api/src/routers/routers.ts
  7. Run bun run db:generate if schema changed