Skip to content

Feature: Explicit Association Support #43

@Goldziher

Description

@Goldziher

Feature: Explicit Association Support

Add explicit support for handling associations between different factory types, making it easier to create related entities.

Context

The library already has database adapter examples (Prisma, Mongoose, TypeORM) that show how to persist data. This feature would add first-class support for defining relationships between factories in an ORM-agnostic way.

Proposed Features

  • association method - Define a relationship to another factory
  • hasMany method - Define one-to-many relationships
  • hasOne method - Define one-to-one relationships
  • belongsTo method - Define inverse relationships
  • Handle circular references properly - Prevent infinite loops
  • Support for sharing associated instances - Reuse associated objects
  • Proper TypeScript types for associations - Full type safety

Implementation Requirements

  • ORM-agnostic implementation that works with any persistence layer
  • Reference existing database adapter examples for patterns
  • Consider transaction support implications
  • Handle performance for large association graphs
  • Provide clear documentation on memory usage patterns

Example API

const UserFactory = new Factory<User>((faker) => ({
  id: faker.string.uuid(),
  name: faker.person.fullName(),
  email: faker.internet.email()
}));

const PostFactory = new Factory<Post>((faker) => ({
  id: faker.string.uuid(),
  title: faker.lorem.sentence(),
  content: faker.lorem.paragraphs(),
  author: faker.association(UserFactory), // Creates associated user
  authorId: faker.association(UserFactory, 'id') // Just the ID
}));

const BlogFactory = new Factory<Blog>((faker) => ({
  id: faker.string.uuid(),
  name: faker.company.name(),
  owner: faker.hasOne(UserFactory),
  posts: faker.hasMany(PostFactory, { count: 5 }) // Create 5 posts
}));

// Share associations
const user = UserFactory.build();
const posts = PostFactory.batch(10, { author: user, authorId: user.id });

// With persistence
const blog = await BlogFactory.buildAsync({}, { 
  adapter: prismaAdapter,
  cascade: true // Save all associations
});

Performance Considerations

  • Lazy loading vs eager loading strategies
  • Batch creation optimization
  • Memory usage for large graphs
  • Transaction batching for database operations

Testing Requirements

  • Unit tests for each association type
  • Integration tests with sample adapters
  • Performance tests for large association graphs
  • Circular reference handling tests
  • Type safety verification tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgood first issueGood for newcomershelp wantedExtra attention is neededonlydust-waveContribute to awesome OSS repos during OnlyDust's open source week

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions