### Install Decorated Factory and Dependencies Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Installs the decorated-factory package, along with @faker-js/faker and reflect-metadata, which are required for its functionality. Supports both npm and yarn package managers. ```shell npm i decorated-factory @faker-js/faker reflect-metadata -D ``` ```shell yarn add decorated-factory @faker-js/faker reflect-metadata --dev ``` -------------------------------- ### Define and Generate Entities with One-to-One Relationships in TypeScript Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Demonstrates how to define TypeScript classes with single-object relationships and generate instances using the decorated-factory. It shows how to create entities with and without populated relationships, and how to get plain objects for serialization. ```typescript import { FactoryType, FactoryValue } from 'decorated-factory'; import { Factory } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class Photo { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.image.url()) url: string; @FactoryValue(faker => faker.lorem.sentence()) description: string; } class User { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; // Relationship only generated when requested with .with() @FactoryType(() => Photo) photo: Photo; } // Without relation (photo is undefined) const userWithoutPhoto = factory.one(User).make(); // Result: User { id: 123, name: "Jane Smith", photo: undefined } // With relation populated const userWithPhoto = factory.one(User).with('photo').make(); // Result: User { id: 123, name: "Jane Smith", photo: Photo { id: 456, url: "https://...", description: "Beautiful sunset" } } // As plain object for JSON serialization const payload = factory.one(User).with('photo').plain(); // Result: { id: 123, name: "Jane Smith", photo: { id: 456, url: "https://...", description: "Beautiful sunset" } } ``` -------------------------------- ### TypeScript Error Handling with decorated-factory Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Demonstrates common errors encountered when using the decorated-factory library and how they are caught. This includes issues like negative amounts in `.with()` or `.make()`, attempting to override nested paths without proper setup, and trying to generate arrays of `AutoIncrement` values. ```typescript import { FactoryType, FactoryValue } from 'decorated-factory'; import { Factory, AutoIncrement } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class Photo { @FactoryValue(faker => faker.image.url()) url: string; } class User { @FactoryType(() => AutoIncrement) id: number; @FactoryType(() => [Photo]) photos: Photo[]; } // Error: Negative amounts not allowed try { factory.one(User).with(-1, 'photos').make(); } catch (error) { console.error(error.message); // "Amount supplied to .with() must be zero or positive, but got -1" } // Error: Cannot override nested path without parent relation try { factory.one(User) .set('photos.url', 'https://example.com') .make(); } catch (error) { console.error(error.message); // "Cannot override nested path "photos.url" – missing .with("photos") call" } // Error: Cannot generate arrays of AutoIncrement try { class Task { @FactoryType(() => [AutoIncrement]) ids: number[]; } factory.one(Task).with(5, 'ids').make(); } catch (error) { console.error(error.message); // "cannot generate an array of AutoIncrement values" } // Error: Negative size in make() try { factory.many(User).make(-5); } catch (error) { console.error(error.message); // "Amount supplied to .make() must be zero or positive, but got -5" } ``` -------------------------------- ### Generate Deep Object Graphs with Nested Relationships in TypeScript Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Shows how to create complex, deeply nested object structures by chaining the .with() method to define multiple levels of relationships. This example includes auto-incrementing IDs, embedded objects, and arrays of related objects within other arrays. ```typescript import { FactoryType, FactoryValue } from 'decorated-factory'; import { Factory, AutoIncrement } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class Upload { @FactoryValue(faker => faker.image.url()) url: string; @FactoryValue(faker => faker.number.int({ min: 1000, max: 9999 })) size: number; } class Tag { @FactoryValue(faker => faker.word.noun()) name: string; } class Photo { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.lorem.sentence()) description: string; @FactoryType(() => Upload) upload: Upload; @FactoryType(() => [Tag]) tags: Tag[]; } class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => [Photo]) photos: Photo[]; } // Generate deeply nested structure const complexUser = factory .one(User) .with(3, 'photos') // 3 photos .with('photos.upload') // Upload for each photo .with(2, 'photos.tags') // 2 tags for each photo .make(); // Result: // User { // id: 1, // name: "Alice Johnson", // photos: [ // Photo { id: 1, description: "...", upload: Upload {...}, tags: [ Tag {...}, Tag {...} ] }, // Photo { id: 2, description: "...", upload: Upload {...}, tags: [ Tag {...}, Tag {...} ] }, // Photo { id: 3, description: "...", upload: Upload {...}, tags: [ Tag {...}, Tag {...} ] } // ] // } ``` -------------------------------- ### Use FactoryValue Decorator for Custom Value Generation Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Illustrates the use of the @FactoryValue decorator to define custom value generation logic for class properties using Faker.js functions. It covers generating primitive types, random integers, full names, emails, and arrays of values, including an example of generating a single instance and multiple instances with array values. ```typescript import { FactoryValue } from 'decorated-factory'; import { Factory } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class User { // Generate random integers between 1-1000 @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; // Generate random full names @FactoryValue(faker => faker.person.fullName()) name: string; // Generate email addresses @FactoryValue(faker => faker.internet.email()) email: string; // Array of primitive values using isArray option @FactoryValue(faker => faker.lorem.word(), { isArray: true }) tags: string[]; } // Generate single instance const user = factory.one(User).make(); // Result: User { id: 742, name: "John Doe", email: "john@example.com", tags: [] } // Generate with array values const userWithTags = factory.one(User).with(3, 'tags').make(); // Result: User { id: 742, name: "John Doe", email: "john@example.com", tags: ["lorem", "ipsum", "dolor"] } // Generate multiple instances const users = factory.many(User).make(5); // Result: [ User {...}, User {...}, User {...}, User {...}, User {...} ] ``` -------------------------------- ### Quick-start: Define and Generate a User Instance Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Demonstrates the basic usage of Decorated Factory. It shows how to import necessary modules, define a User class with @FactoryValue decorators, and generate a single User instance using the Factory builder. ```typescript import 'reflect-metadata'; import { fakerPT_BR } from '@faker-js/faker'; import { Factory, FactoryValue } from 'decorated-factory'; const factory = new Factory(fakerPT_BR); class User { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; } const user = factory.one(User).make(); ``` -------------------------------- ### API Reference for Decorated Factory Builders Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Provides a summary of the core builder methods available in Decorated Factory, including creating single or multiple instances, defining relations, setting and excluding fields, and generating output. ```text Factory.one(Type) // builder for one Factory.many(Type) // builder for many .with(amount?, path) // opt-in relations (any depth) .set(path, value) // overrides (optional) .without(path) // exclusions (optional) .make(size?) | .plain(size?) ``` -------------------------------- ### Initialize Decorated Factory with Faker.js Locale Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Demonstrates how to initialize the Decorated Factory with a specific Faker.js locale, such as Brazilian Portuguese. It also shows how to create a factory instance with the default English locale. ```typescript import 'reflect-metadata'; import { fakerPT_BR } from '@faker-js/faker'; import { Factory } from 'decorated-factory'; // Create factory with Brazilian Portuguese locale const factory = new Factory(fakerPT_BR); // Use different locales as needed import { faker } from '@faker-js/faker'; // English (default) const factoryEN = new Factory(faker); ``` -------------------------------- ### Generate Instances with Custom Faker Values Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Illustrates how to define custom values for object fields using the `FactoryValue` decorator and the Faker library. This allows for realistic and varied data generation based on Faker's extensive provider API. ```typescript class User { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; } const singleUser = factory.one(User).make(); const fiveUsers = factory.many(User).make(5); ``` -------------------------------- ### Generate Class Instances vs. Plain Objects in TypeScript Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Illustrates the difference between generating full class instances (with methods) and plain objects (for serialization) using decorated-factory. Plain objects are ideal for API payloads as they serialize cleanly. ```typescript import { FactoryValue } from 'decorated-factory'; import { Factory } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class User { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryValue(faker => faker.internet.email()) email: string; // Class method getDisplayName(): string { return `${this.name} (${this.email})`; } } // Generate class instance with methods const userInstance = factory.one(User).make(); console.log(userInstance instanceof User); // true console.log(userInstance.getDisplayName()); // "John Doe (john@example.com)" // Generate plain object (no methods, no prototype) const userPlain = factory.one(User).plain(); console.log(userPlain instanceof User); // false console.log(typeof userPlain.getDisplayName); // "undefined" // Plain objects are perfect for API responses async function createUserAPI() { const payload = factory.one(User).plain(); const response = await fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) // Serializes cleanly }); return response.json(); } // Generate multiple plain objects const userPayloads = factory.many(User).plain(10); // Result: Array of 10 plain objects (not User instances) ``` -------------------------------- ### Creating Empty Arrays with .with() Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Demonstrates how to create an empty array for a specific relation using the `.with(0, 'relationName')` method. Negative amounts will throw an error. ```ts const emptyPhotosUser = factory.one(User).with(0, 'photos').make(); ``` -------------------------------- ### Use Key Binding for Foreign Keys Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Explains and demonstrates how to use key binding (`key` and `inverseKey`) with `@FactoryType` to automatically set foreign keys when generating related objects. This ensures referential integrity between parent and child records. ```typescript class Photo { @FactoryValue(faker => faker.image.url()) url: string; userId: number; // copied from parent User.id } class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => [Photo], { key: 'id', inverseKey: 'userId' }) photos: Photo[]; } const userWithPhotos = factory.one(User).with(5, 'photos').make(); ``` -------------------------------- ### Generating Plain Objects with .plain() Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Explains how to generate plain JavaScript objects instead of class instances using the `.plain()` method. This is ideal for preparing data for API payloads or JSON serialization. ```ts const payload = factory .one(User) .with(2, 'photos') .plain(); ``` -------------------------------- ### Define One-to-One Relationships Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Shows how to define a one-to-one relationship between two classes, `User` and `Photo`, using `@FactoryType`. This ensures that related objects are created and associated correctly during factory generation. ```typescript class Photo { @FactoryValue(faker => faker.image.url()) url: string; } class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => Photo) photo: Photo; } const user = factory.one(User).with('photo').make(); ``` -------------------------------- ### Generate Nested Object Graphs Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Demonstrates how to generate nested object structures, such as a `User` with `photos`, where each `photo` in turn has an `upload` object. The `.with('photos.upload')` syntax recursively defines nested relationships. ```typescript class Upload { @FactoryValue(faker => faker.image.url()) url: string; } class Photo { @FactoryValue(faker => faker.lorem.sentence()) description: string; @FactoryType(() => Upload) upload: Upload; } class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => [Photo]) photos: Photo[]; } const complexUser = factory .one(User) .with(3, 'photos') .with('photos.upload') .make(); ``` -------------------------------- ### Define One-to-Many Relationships Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Illustrates the creation of a one-to-many relationship, where a `User` can have multiple `Photo` objects. The `[Photo]` syntax with `@FactoryType` indicates an array of related objects. ```typescript class Photo { @FactoryValue(faker => faker.image.url()) url: string; } class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => [Photo]) photos: Photo[]; } const gallery = factory.one(User).with(5, 'photos').make(); ``` -------------------------------- ### Generate Arrays of Primitives with Faker Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Demonstrates generating arrays of primitive types, such as an array of strings for tags, using `FactoryValue` with the `isArray: true` option. This is useful for fields that represent collections of simple data. ```typescript class User { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryValue(faker => faker.lorem.word(), { isArray: true }) tags: string[]; } const userWithFiveTags = factory .one(User) .with(5, 'tags') .make(); ``` -------------------------------- ### Generate Nested Arrays of Objects Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Shows how to generate nested arrays, such as a `User` having `photos`, and each `photo` having an array of `tags`. This allows for complex data structures with multiple levels of nesting. ```typescript class Tag { @FactoryValue(faker => faker.word.noun()) name: string; } class Photo { @FactoryValue(faker => faker.image.url()) url: string; @FactoryType(() => [Tag]) tags: Tag[]; } class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => [Photo]) photos: Photo[]; } const userWithTaggedPhotos = factory .one(User) .with(4, 'photos') .with(2, 'photos.tags') .make(); ``` -------------------------------- ### Handle Explicit Circular References Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Demonstrates how to define and generate objects with explicit circular references, such as a `User` having a `photo` and the `photo` referencing back to the `User`. Decorated Factory manages these cycles to prevent infinite loops. ```typescript class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => Photo) photo: Photo; } class Photo { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.image.url()) url: string; @FactoryType(() => User) user: User; } const circular = factory .one(User) .with('photo') .with('photo.user') .make(); ``` -------------------------------- ### Handle Circular References in TypeScript Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Demonstrates how to create entities with circular dependencies using the decorated-factory library. It shows how to explicitly link entities that refer to each other, creating a graph structure. Be aware that nested levels might create new instances unless explicitly managed. ```typescript import { FactoryType, FactoryValue } from 'decorated-factory'; import { Factory, AutoIncrement } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => Photo) photo: Photo; } class Photo { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.image.url()) url: string; @FactoryType(() => User) user: User; } // Generate circular reference by explicitly requesting both sides const circular = factory .one(User) .with('photo') .with('photo.user') .make(); // Result: // User { // id: 1, // name: "Alice", // photo: Photo { // id: 1, // url: "https://...", // user: User { id: 2, name: "Bob", photo: undefined } // } // } // Note: Each level creates new instances unless explicitly controlled ``` -------------------------------- ### Use Built-in Helpers for Sequential IDs and UUIDs Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Showcases the use of built-in Decorated Factory helpers like `AutoIncrement` for sequential integers and `UUID` for generating RFC-4122 v4 compliant UUID strings. These helpers are useful for creating unique identifiers within generated data. ```typescript class Task { @FactoryType(() => AutoIncrement) id: number; @FactoryType(() => UUID) taskId: string; } const task = factory.one(Task).make(); ``` -------------------------------- ### Generate JavaScript Primitives with Decorated Factory Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Demonstrates how Decorated Factory can generate default values for common JavaScript primitive types like String, Number, Boolean, Date, and arrays of strings. This simplifies the creation of complex objects by handling basic type instantiations automatically. ```typescript class Document { @FactoryType(() => String) title: string; @FactoryType(() => Number) version: number; @FactoryType(() => Boolean) isPublished: boolean; @FactoryType(() => Date) createdAt: Date; @FactoryType(() => [String]) tags: string[]; } const document = factory.one(Document).with(3, 'tags').make(); ``` -------------------------------- ### Excluding Fields with .without() Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Demonstrates how to exclude specific fields from the generated object using the `.without(fieldName)` method. This is useful for creating data structures with only necessary fields. ```ts const anonymousUser = factory .one(User) .without('name') .make(); ``` -------------------------------- ### TypeScript: Foreign Key Binding with Decorated Factory Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Automatically copies parent keys to child entities for database relationships using Decorated Factory. This is useful for establishing links between parent and child objects, such as linking comments to a post or a profile to a user. It supports one-to-many and one-to-one relationships. ```typescript import { FactoryType, FactoryValue } from 'decorated-factory'; import { Factory, AutoIncrement } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class Comment { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.lorem.paragraph()) text: string; // This will be automatically populated from parent Post.id postId: number; } class Post { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.lorem.sentence()) title: string; // Key binding: copy 'id' from Post to 'postId' in Comment @FactoryType(() => [Comment], { key: 'id', inverseKey: 'postId' }) comments: Comment[]; } const post = factory.one(Post).with(3, 'comments').make(); // Result: // Post { // id: 1, // title: "How to use Decorated Factory", // comments: [ // Comment { id: 1, text: "Great article!", postId: 1 }, // Comment { id: 2, text: "Very helpful", postId: 1 }, // Comment { id: 3, text: "Thanks for sharing", postId: 1 } // ] // } // Works with one-to-one relationships too class Profile { @FactoryValue(faker => faker.lorem.paragraph()) bio: string; userId: number; } class User { @FactoryType(() => AutoIncrement) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryType(() => Profile, { key: 'id', inverseKey: 'userId' }) profile: Profile; } const user = factory.one(User).with('profile').make(); // Result: User { id: 1, name: "John", profile: Profile { bio: "...", userId: 1 } } ``` -------------------------------- ### Overriding Single Field Value with .set() Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Shows how to override the value of a specific field using the `.set(fieldName, value)` method. This is useful for setting default or specific values for generated objects. ```ts const namedUser = factory.one(User) .set('name', 'John Doe') .make(); ``` -------------------------------- ### Define and Generate Entities with One-to-Many Relationships in TypeScript Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Illustrates how to define TypeScript classes for one-to-many relationships and generate entities with arrays of related objects. This includes generating a specific number of related items, empty arrays, and generating multiple parent entities each with related items. ```typescript import { FactoryType, FactoryValue } from 'decorated-factory'; import { Factory } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class Photo { @FactoryValue(faker => faker.image.url()) url: string; @FactoryValue(faker => faker.lorem.sentence()) description: string; } class User { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; // Array relationship - wrap type in array notation @FactoryType(() => [Photo]) photos: Photo[]; } // Generate user with 5 photos const gallery = factory.one(User).with(5, 'photos').make(); // Result: User { id: 123, name: "Bob", photos: [ Photo {...}, Photo {...}, Photo {...}, Photo {...}, Photo {...} ] } // Generate empty array const userNoPhotos = factory.one(User).with(0, 'photos').make(); // Result: User { id: 123, name: "Bob", photos: [] } // Generate multiple users each with 3 photos const users = factory.many(User).with(3, 'photos').make(10); // Result: [ User { photos: [3 items] }, User { photos: [3 items] }, ... ] (10 users total) ``` -------------------------------- ### Excluding Nested Fields within a Relation with .without() Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Shows how to exclude a nested field within a relation using dot notation in the `.without(path)` method. The parent relation must be requested first with `.with()` to ensure proper exclusion. ```ts const userWithoutPhotoText = factory .one(User) .with('photo') .without('photo.description') .make(); ``` -------------------------------- ### Use FactoryType Decorator for Relationships and Built-in Types Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Explains the @FactoryType decorator for defining relationships and utilizing built-in types like AutoIncrement and UUID. It also shows how to use @FactoryType with standard JavaScript types (String, Number, Boolean, Date, Array) for automatic value generation. ```typescript import { FactoryType, FactoryValue } from 'decorated-factory'; import { Factory, AutoIncrement, UUID } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); // Built-in helper types class Task { // AutoIncrement generates 1, 2, 3... sequentially per builder @FactoryType(() => AutoIncrement) id: number; // UUID generates RFC-4122 v4 UUIDs @FactoryType(() => UUID) taskId: string; @FactoryValue(faker => faker.lorem.sentence()) title: string; } // Built-in JavaScript types (no explicit value generation needed) class Document { @FactoryType(() => String) title: string; // Generates random words @FactoryType(() => Number) version: number; // Generates 1-10000 @FactoryType(() => Boolean) isPublished: boolean; // Generates true/false @FactoryType(() => Date) createdAt: Date; // Generates past dates @FactoryType(() => [String]) tags: string[]; // Array of strings } const task = factory.one(Task).make(); // Result: Task { id: 1, taskId: "d7f3e429-9d5b-42f9-b7de-8ba0e50bc9f6", title: "Complete project" } const document = factory.one(Document).with(3, 'tags').make(); // Result: Document { title: "lorem", version: 5432, isPublished: true, createdAt: 2024-01-15, tags: ["word1", "word2", "word3"] } ``` -------------------------------- ### Overriding Nested Fields and Array Elements with .set() Source: https://github.com/gabrieljsilva/decorated-factory/blob/master/README.md Illustrates how to override values within nested objects or elements of an array relation using dot notation in the `.set(path, value)` method. The parent relation must be requested first with `.with()` to avoid errors. ```ts const modifiedDescriptions = factory.one(User) .with(5, 'photos') .set('photos.description', 'Same description for all photos') .make(); ``` -------------------------------- ### TypeScript: Overriding Generated Values with set() in Decorated Factory Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Override specific properties of generated objects after their initial configuration using the set() method in Decorated Factory. This allows for precise control over generated data, enabling the modification of simple properties, nested properties (after calling .with()), or even entire objects. It also demonstrates an error case when attempting to override without the necessary .with() call. ```typescript import { FactoryValue } from 'decorated-factory'; import { Factory } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class Photo { @FactoryValue(faker => faker.image.url()) url: string; @FactoryValue(faker => faker.lorem.sentence()) description: string; } class User { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryValue(faker => faker.internet.email()) email: string; @FactoryType(() => [Photo]) photos: Photo[]; } // Override simple property const specificUser = factory .one(User) .set('name', 'John Doe') .set('email', 'john.doe@example.com') .make(); // Result: User { id: 742, name: "John Doe", email: "john.doe@example.com" } // Override nested properties (must call .with() first) const uniformPhotos = factory .one(User) .with(5, 'photos') .set('photos.description', 'Same description for all photos') .make(); // Result: User with 5 photos all having the same description // Error example - attempting to override without .with() try { factory.one(User) .set('photos.description', 'Description') // Throws error! .make(); } catch (error) { // Error: Cannot override path "photos.description" – missing .with("photos") call } // Override objects for testing specific scenarios const testUser = factory .one(User) .with('photos') .set('photos', [{ url: 'https://test.jpg', description: 'Test photo' }]) .make(); ``` -------------------------------- ### TypeScript: Excluding Fields with without() in Decorated Factory Source: https://context7.com/gabrieljsilva/decorated-factory/llms.txt Omit specific fields from generated output using the without() method in Decorated Factory. This is useful for security or to simplify data by excluding sensitive information like passwords or non-essential details like uploaded timestamps. It supports excluding single fields, multiple fields, and nested properties. ```typescript import { FactoryType, FactoryValue } from 'decorated-factory'; import { Factory } from 'decorated-factory'; import { faker } from '@faker-js/faker'; const factory = new Factory(faker); class Photo { @FactoryValue(faker => faker.image.url()) url: string; @FactoryValue(faker => faker.lorem.sentence()) description: string; @FactoryValue(faker => faker.date.past()) uploadedAt: Date; } class User { @FactoryValue(faker => faker.number.int({ min: 1, max: 1000 })) id: number; @FactoryValue(faker => faker.person.fullName()) name: string; @FactoryValue(faker => faker.internet.email()) email: string; @FactoryValue(faker => faker.internet.password()) password: string; @FactoryType(() => Photo) photo: Photo; } // Exclude sensitive fields const publicUser = factory .one(User) .without('password') .make(); // Result: User { id: 123, name: "John", email: "john@example.com", password: undefined } // Exclude multiple fields const minimalUser = factory .one(User) .without('password') .without('email') .make(); // Result: User { id: 123, name: "John", email: undefined, password: undefined } // Exclude nested properties const userWithLimitedPhoto = factory .one(User) .with('photo') .without('photo.description') .without('photo.uploadedAt') .make(); // Result: User { id: 123, name: "John", photo: Photo { url: "https://...", description: undefined, uploadedAt: undefined } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.