Backend Models

Design domain entities, DTOs, and mappers with static factory methods.

1|Updated Dec 4, 2024
One-click install
npx skills add https://github.com/imkdw/imkdw-dev --skill backend-models-imkdw
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Backend Models
Source: https://github.com/imkdw/imkdw-dev/tree/main/.claude/skills/backend-models
Command: npx skills add https://github.com/imkdw/imkdw-dev --skill backend-models-imkdw

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Poorly designed data models can lead to tightly coupled code, difficult maintenance, and a lack of clarity in business logic, hindering scalability and developer productivity. This Skill promotes robust domain-driven design patterns for backend models.

Core Features & Use Cases

  • Domain Entities: Implement rich domain models with private constructors and static create() factory methods, ensuring valid state upon creation.
  • DTO Management: Create clear request and response Data Transfer Objects (DTOs) with validation decorators and static from() methods for easy data transformation.
  • Mapper Integration: Seamlessly convert between Prisma entities and domain objects using dedicated mapper classes, maintaining separation of concerns.
  • Value Objects: Utilize value objects (e.g., ArticleContent) for complex attributes, enhancing type safety and encapsulating domain logic.
  • Use Case: When defining a new Product domain entity, ensure it has a private constructor, a static create() factory method, and uses value objects for complex attributes like Price and Dimensions, adhering to DDD principles.

Quick Start

Help me define a new Category domain entity with an id and name property, ensuring it follows the static factory method pattern and uses a private constructor.

Frequently Asked Questions about Backend Models

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I design domain entities with TypeScript following domain-driven design patterns?

Domain entities use private constructors and static create() factory methods to ensure valid state. Implement them in shared/domain/ with class-validator decorators, value objects for complex attributes, and adherence to @imkdw-dev/types interfaces and @imkdw-dev/consts validation limits for type safety and encapsulation.

What's the best way to structure DTOs and mappers between domain and persistence layers?

Create request/response DTOs in features/{feature}/dto/ with static from() methods for transformation. Use dedicated mapper classes in shared/mapper/ to convert between Prisma entities and domain objects, maintaining separation of concerns and clear data flow boundaries.

When should I use value objects in my backend models?

Value objects like ArticleContent encapsulate domain logic for complex attributes, enhancing type safety and business rule enforcement. Use them in domain entities to represent non-primitive concepts tied to your domain, keeping related behavior and validation together.

Can I implement factory methods and validation decorators together in TypeScript domain models?

Yes, combine static create() factory methods with class-validator decorators on DTO properties to validate input during instantiation, then use mappers to transform validated DTOs into domain entities following DDD conventions.

How do DTOs differ from domain entities in backend architecture?

DTOs (data transfer objects) are lightweight, validation-focused structures for API requests and responses; domain entities are rich models with business logic, private state, and factory methods. Mappers convert between them to prevent coupling external contracts to domain design.

What constraints should I follow when defining entity constructors?

Use private constructors in domain entities to prevent invalid instantiation, expose a static create() method for controlled object creation, and validate preconditions inside create() before delegating to the constructor, ensuring domain invariants hold.