Co-Pilot / 辅助式
更新于 a month ago

rich-domain

Ttarcisioandrade
0.0k
tarcisioandrade/rich-domain
82
Agent 评分

💡 摘要

一个用于领域驱动设计的TypeScript单体库,提供可扩展应用开发的工具。

🎯 适合人群

软件架构师后端开发人员使用React的前端开发人员DevOps工程师技术项目经理

🤖 AI 吐槽:看起来很能打,但别让配置把人劝退。

安全分析中风险

风险:Medium。建议检查:是否执行 shell/命令行指令;是否发起外网请求(SSRF/数据外发);依赖锁定与供应链风险。以最小权限运行,并在生产环境启用前审计代码与依赖。

A comprehensive TypeScript monorepo for Domain-Driven Design, providing enterprise-ready tools for building maintainable, scalable applications with clean architecture patterns.

License: MIT Node.js Version TypeScript

📦 Packages

| Package | Version | Description | | ---------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | | @woltz/rich-domain | npm | Core DDD library with entities, aggregates, value objects, and automatic change tracking | | @woltz/rich-domain-prisma | npm | Prisma adapter with Unit of Work and batch operations | | @woltz/rich-domain-typeorm | npm | TypeORM adapter with transaction support | | @woltz/rich-domain-criteria-zod | npm | Zod-based criteria builder for type-safe queries | | @woltz/rich-domain-cli | npm | CLI for project scaffolding and code generation | | @woltz/rich-domain-export | npm | Export data to @woltz/rich-domain repository |

🚀 Quick Start

Installation

# Core library npm install @woltz/rich-domain # With your preferred ORM npm install @woltz/rich-domain-prisma # or npm install @woltz/rich-domain-typeorm # CLI tools npm install -g @woltz/rich-domain-cli

Using the CLI

# Initialize a new project from template rich-domain init my-app --template fullstack # Generate domain from Prisma schema rich-domain generate --schema prisma/schema.prisma # Manually add entities rich-domain add User name:string email:string --with-repo

Basic Example

import { Aggregate, Entity, Id } from "@woltz/rich-domain"; import { z } from "zod"; // Define your domain model class User extends Aggregate<UserProps> { protected static validation = { schema: z.object({ id: z.custom<Id>(), name: z.string(), email: z.string().email(), posts: z.array(z.instanceof(Post)), }), }; addPost(title: string, content: string): void { const post = new Post({ title, content }); this.props.posts.push(post); } // Getters... } // Automatic change tracking const user = new User({ /* ... */ }); user.addPost("First Post", "Content"); const changes = user.getChanges(); // { // creates: [{ entity: "Post", ... }], // updates: [], // deletes: [] // }

🎯 Core Features

Domain-Driven Design Building Blocks

  • Entities - Objects with identity and lifecycle
  • Aggregates - Consistency boundaries with automatic change tracking
  • Value Objects - Immutable objects compared by value
  • Domain Events - Cross-aggregate communication
  • Repository Pattern - Abstract persistence layer

Validation & Type Safety

  • Standard Schema Support - Works with Zod, Valibot, ArkType
  • Full TypeScript Integration - Type inference for field paths and operations
  • Lifecycle Hooks - onCreate, onBeforeUpdate, business rules
  • Custom Exceptions - 20+ domain-specific error types

Data Management

  • Automatic Change Tracking - Track nested entities and collections
  • Batch Operations - Optimized bulk inserts, updates, deletes
  • Type-Safe Queries - Fluent Criteria API with full type safety
  • Pagination - Built-in paginated results with metadata

Persistence Adapters

  • ORM Agnostic - Core library works with any persistence layer
  • Prisma Adapter - Unit of Work with AsyncLocalStorage
  • TypeORM Adapter - Transaction decorator support
  • Custom Adapters - Easy to implement for other ORMs

Developer Experience

  • CLI Tooling - Project scaffolding and code generation
  • React Components - Data tables with shadcn/ui integration
  • React Query Integration - useCriteriaQuery hook
  • Documentation - Comprehensive guides and API reference

📁 Repository Structure

rich-domain/
├── packages/
│   ├── rich-domain/              # Core library
│   ├── rich-domain-prisma/       # Prisma adapter
│   ├── rich-domain-typeorm/      # TypeORM adapter
│   ├── rich-domain-criteria-zod/ # Zod criteria builder
│   ├── react-rich-domain/        # React components
│   └── rich-domain-cli/          # CLI tool
├── examples/
│   ├── backend/
│   │   └── fastify-with-prisma/  # Fastify + Prisma example
│   └── frontend/
│       └── react-with-react-query/ # React + React Query example
├── docs/                         # Mintlify documentation
└── package.json                  # Workspace configuration

🛠️ Development

Prerequisites

  • Node.js >= 22.12.0
  • npm, pnpm, yarn, or bun

Setup

# Clone the repository git clone https://github.com/tarcisioandrade/rich-domain.git cd rich-domain # Install dependencies npm install # Build all packages npm run build # Run tests npm test # Type checking npm run check # Linting npm run lint

Workspace Commands

# Build specific package npm run build --workspace=@woltz/rich-domain # Test specific package npm run test --workspace=@woltz/rich-domain # Clean all packages npm run clean

📖 Documentation

Visit our complete documentation for:

  • Getting Started - Quick start guide and installation
  • Core Concepts - Entities, Aggregates, Value Objects, Change Tracking
  • Validation - Schema validation, hooks, error handling
  • Criteria - Type-safe query building
  • Repository - Persistence abstraction and mappers
  • Integrations - Prisma, TypeORM, Zod, React
  • CLI Reference - Command-line tools guide

🎓 Examples

Backend Examples

Fastify with Prisma

Complete REST API with:

  • Domain-Driven Design architecture
  • Prisma ORM integration
  • BullMQ for background jobs
  • Zod validation
  • Docker setup
cd examples/backend/fastify-with-prisma npm install npm run docker:up npm run db:push npm run dev

Frontend Examples

React with React Query

Full-stack application with:

  • React + Vite
  • React Query integration
  • shadcn/ui components
  • Data tables with criteria
  • Type-safe queries
cd examples/frontend/react-with-react-query npm install npm run dev

🏗️ Architecture Principles

Clean Architecture

┌─────────────────────────────────────────────┐
│              Presentation Layer             │
│         (Controllers, REST/GraphQL)         │
├─────────────────────────────────────────────┤
│             Application Layer               │
│          (Use Cases, Services)              │
├─────────────────────────────────────────────┤
│               Domain Layer                  │
│   (Entities, Aggregates, Value Objects)     │
├─────────────────────────────────────────────┤
│           Infrastructure Layer              │
│    (Repositories, ORM, External APIs)       │
└─────────────────────────────────────────────┘

DDD Patterns Implemented

  • Strategic Design

    • Bounded Contexts
    • Ubiquitous Language
    • Domain Events
  • Tactical Design

    • Entities & Aggregates
    • Value Objects
    • Repositories
    • Domain Services
    • Factories
  • Supporting Patterns

    • Unit of Work
    • Specification (Criteria)
    • Change Tracking
    • Event Sourcing (via Domain Events)

🔧 Configuration

Monorepo Setup

The repository uses npm workspaces for package management:

{ "workspaces": ["packages/*", "examples/**/*"] }

TypeScript Configuration

All packages use:

  • TypeScript 5.4+
  • Strict mode enabled
  • Dual module format (ESM + CommonJS)
  • Full type definitions

Publishing

Each package is independently versioned and published to npm:

# Publish specific package npm publish --workspace=@woltz/rich-domain # Version update npm run release --workspace=@woltz/rich-domain

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Workflow

  1. Fork the repository
  2. Create a
五维分析
清晰度8/10
创新性7/10
实用性9/10
完整性8/10
可维护性9/10
优缺点分析

优点

  • 全面的DDD支持
  • 类型安全的查询
  • 自动变更跟踪
  • 与ORM无关

缺点

  • 对初学者来说复杂
  • 需要TypeScript知识
  • 学习曲线可能陡峭
  • 高级用例的示例有限

相关技能

swift-expert

A
toolCo-Pilot / 辅助式
86/ 100

“看起来很能打,但别让配置把人劝退。”

react-expert

A
toolCo-Pilot / 辅助式
86/ 100

“看起来很能打,但别让配置把人劝退。”

rails-expert

A
toolCo-Pilot / 辅助式
86/ 100

“看起来很能打,但别让配置把人劝退。”

免责声明:本内容来源于 GitHub 开源项目,仅供展示和评分分析使用。

版权归原作者所有 tarcisioandrade.