Advanced Guides

Circular references

Model and generate explicit circular relations safely

Overview

Use dot paths to explicitly opt into cycles. Only the branches you request are created.

Example

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

Safety notes

Because relations are lazy and opt-in, there is no infinite recursion: only the explicitly requested nodes are built.

On this page