How gabrieljsilva/decorated-factory works
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.