Advanced Guides

Nested relationships

Use dot-paths with .with() to generate deep graphs on demand

Overview

Request deep relations via dot paths. Only requested branches are materialized.

Example

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();

Multiple nested arrays

Multiple nested arrays are supported:

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();

Notes

Always call .with() on the parent path before overriding children with .set() or excluding with .without().

On this page