Skip to content

Feature: Enhanced Lazy Evaluation #46

@Goldziher

Description

@Goldziher

Feature: Enhanced Lazy Evaluation

Extend lazy evaluation capabilities beyond the current use method to support more complex scenarios.

Current State

The library already has a use() method that enables lazy evaluation for circular references and complex relationships. This feature would enhance and extend these capabilities.

Proposed Features

  • Enhanced lazy method with field dependencies

    • Allow specifying which fields trigger re-evaluation
    • Support for conditional dependencies
  • Async lazy evaluation support

    • Handle promises in lazy evaluation
    • Maintain type safety with async operations
  • Conditional lazy evaluation

    • Evaluate only when certain conditions are met
    • Support for predicate functions
  • Proper type inference

    • Preserve TypeScript type inference throughout
    • No degradation of type safety

Implementation Requirements

  • Build upon existing use() method infrastructure
  • Maintain backward compatibility
  • Ensure TypeScript type inference is preserved
  • Consider integration with circular reference handling
  • Add comprehensive tests for all new lazy evaluation patterns

Example API

// Field dependencies
const factory = new Factory<User>((faker) => ({
  id: faker.string.uuid(),
  name: faker.person.fullName(),
  email: factory.lazy((user) => `${user.name.toLowerCase().replace(' ', '.')}@example.com`, ['name'])
}));

// Async lazy evaluation
const factory = new Factory<User>((faker) => ({
  id: faker.string.uuid(),
  apiKey: factory.lazyAsync(async (user) => await generateApiKey(user.id))
}));

// Conditional lazy evaluation
const factory = new Factory<Order>((faker) => ({
  status: faker.helpers.arrayElement(['pending', 'completed', 'cancelled']),
  completedAt: factory.lazyIf(
    (order) => order.status === 'completed',
    () => faker.date.recent()
  )
}));

Testing Requirements

  • Unit tests for each lazy evaluation pattern
  • Type-level tests to ensure inference works correctly
  • Performance tests for complex lazy evaluation chains
  • Integration tests with existing use() method

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