# gabrieljsilva/decorated-factory architecture

> Architecture diagram of the GitHub repository gabrieljsilva/decorated-factory, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gabrieljsilva/decorated-factory
- Repository: https://github.com/gabrieljsilva/decorated-factory

## Overview

##### Project Type and Purpose
- This is an open-source TypeScript library (“Decorated Factory”) that uses decorators plus `reflect-metadata` to generate class instances populated with fake/test data via `@faker-js/faker`.
- It is not a full-stack app or microservice; it’s a developer tool intended for test‐data factory generation.

##### Key Architectural Components
A. Source Modules (src/)
- constants
– metadata-keys.constants.ts: keys used when storing/retrieving metadata via `reflect-metadata`.
- decorators
– factory-field.decorator.ts
– factory-relation-field.decorator.ts
– These attach metadata to class fields.
- lib
– factory.ts: core `Factory` class that reads metadata and uses a Faker instance to build objects, lists, relations.
– overridable.ts: `Overridable<T>` wrapper, enabling instance value overrides.
- interfaces
- utils
– extend-array-metadata.ts: merges metadata when arrays of relations are declared.
– resolve-path.ts: resolves nested property paths for partial selection.
- index.ts: public API entry point exporting decorators, Factory, Overridable.

B. Build & Distribution
- rollup.config.mjs: bundles source into both CommonJS (`.cjs.js`) and ES Module (`.esm.js`) outputs under `dist/`.
- Type declarations (`.d.ts`) are emitted alongside compiled code.
- tsconfig.json and biome.json provide compiler and linter settings.
- vitest.config.ts for test configuration; tests live alongside source in `.spec.ts` files.

C. External Dependencies
- -metadata (runtime metadata reflection)
- @faker-js/faker (in dev/build, used by consumers to supply Faker)

##### Relationships and Data Flows
- At runtime, when user code does `@FactoryField` or `@FactoryRelationField`, the decorator writes metadata (with keys from constants) into the class prototype via `reflect-metadata`.
- When `new Factory(faker)` is called, the Factory class:
1. Reflects on class metadata.
2. Calls the provided faker callback for each `FactoryField`.
3. For each `FactoryRelationField`, recursively invokes itself to generate related instances (honoring key/inverseKey binding for foreign‐key scenarios).
4. For arrays, uses `extend-array-metadata` utilities to collect multiple metadata entries.
- `create()` returns an `Overridable` wrapper that applies override functions against the generated instance.

##### Architectural Patterns & Design Principles
- Decorator-based metadata registration (leveraging reflection).
- Single Responsibility:
- Decorators only register metadata.
- Factory only generates instances.
- Overridable only manages overrides.
- Recursive composition for relations and arrays.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "External Dependencies"
        RM["reflect-metadata"]:::external
        FK["@faker-js/faker"]:::external
    end

    subgraph "Constants Module"
        CMIdx["constants/index.ts"]:::metadata
        CMKeys["metadata-keys.constants.ts"]:::metadata
    end

    subgraph "Decorators Module"
        DIdx["decorators/index.ts"]:::metadata
        DField["factory-field.decorator.ts"]:::metadata
        DRel["factory-relation-field.decorator.ts"]:::metadata
    end

    subgraph "Interfaces Module"
        IIdx["interfaces/index.ts"]:::metadata
        IDeep["deep-partial.ts"]:::metadata
        IPaths["paths.ts"]:::metadata
        ISelect["select.ts"]:::metadata
        IType["type.ts"]:::metadata
    end

    subgraph "Utils Module"
        UIdx["utils/index.ts"]:::util
        UExt["extend-array-metadata.ts"]:::util
        URes["resolve-path.ts"]:::util
    end

    subgraph "Core Module"
        CIdx["lib/index.ts"]:::core
        CFactory["factory.ts"]:::core
        COver["overridable.ts"]:::core
    end

    subgraph "Public API"
        API["index.ts"]:::util
    end

    subgraph "Build Pipeline"
        Rollup["rollup.config.mjs"]:::util
        TSConf["tsconfig.json"]:::util
        Biome["biome.json"]:::util
        Vitest["vitest.config.ts"]:::util
        Dist["dist/"]:::util
    end

    %% Relationships
    DField -->|"uses metadata keys"| CMKeys
    DRel -->|"uses metadata keys"| CMKeys
    DField -->|"writes metadata"| RM
    DRel -->|"writes metadata"| RM
    CFactory -->|"reads metadata"| RM
    CFactory -->|"uses"| UExt
    CFactory -->|"uses"| URes
    CIdx -->|"contracts"| IIdx
    CFactory -->|"recursive relations"| CFactory
    CFactory -->|"wraps create()"| COver

    API -->|"exports"| CMIdx
    API --> CMKeys
    API --> DIdx
    API --> CIdx
    API --> IIdx
    API --> UIdx

    Rollup -->|"bundles src to"| Dist
    TSConf --> Rollup
    Biome --> Rollup
    Vitest --> Rollup

    %% Click Events
    click CMKeys "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/constants/metadata-keys.constants.ts"
    click CMIdx "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/constants/index.ts"
    click DField "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/decorators/factory-field.decorator.ts"
    click DRel "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/decorators/factory-relation-field.decorator.ts"
    click DIdx "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/decorators/index.ts"
    click CFactory "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/lib/factory.ts"
    click COver "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/lib/overridable.ts"
    click CIdx "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/lib/index.ts"
    click IDeep "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/interfaces/deep-partial.ts"
    click IPaths "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/interfaces/paths.ts"
    click ISelect "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/interfaces/select.ts"
    click IType "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/interfaces/type.ts"
    click IIdx "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/interfaces/index.ts"
    click UExt "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/utils/extend-array-metadata.ts"
    click URes "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/utils/resolve-path.ts"
    click UIdx "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/utils/index.ts"
    click API "https://github.com/gabrieljsilva/decorated-factory/blob/master/src/index.ts"
    click Rollup "https://github.com/gabrieljsilva/decorated-factory/blob/master/rollup.config.mjs"
    click TSConf "https://github.com/gabrieljsilva/decorated-factory/blob/master/tsconfig.json"
    click Biome "https://github.com/gabrieljsilva/decorated-factory/blob/master/biome.json"
    click Vitest "https://github.com/gabrieljsilva/decorated-factory/blob/master/vitest.config.ts"

    %% Styles
    classDef metadata fill:#D0E8FF,stroke:#0366d6
    classDef core fill:#D0FFD0,stroke:#28a745
    classDef util fill:#EEEEEE,stroke:#666666
    classDef external fill:#FFD0D0,stroke:#d73a49
```
