Class InMemoryRepository<TEntity>Abstract

Base in-memory repository implementation.

Provides common CRUD operations for entities stored in memory. Useful for development, testing, and prototyping.

class InMemoryProductRepository extends InMemoryRepository<Product> {
async findByName(name: string): Promise<Product | null> {
return this.findOne(p => p.name === name);
*
async findByCategory(category: string): Promise<Product[]> {
return this.findMany(p => p.category === category);
}
}

Type Parameters

  • TEntity extends Entity<string>

    The entity type this repository manages

Implements

Constructors

Methods

  • Checks if an entity exists.

    Parameters

    • id: string

      The entity ID (string) to check

    Returns Promise<boolean>

    true if entity exists, false otherwise

  • Finds the first entity matching a predicate.

    Parameters

    • predicate: (entity: TEntity) => boolean

      Function to test each entity

    Returns Promise<null | TEntity>

    The first matching entity or null

    const product = await repository.findOne(p => p.price > 100);
    
  • Counts entities matching an optional predicate.

    Parameters

    • Optionalpredicate: (entity: TEntity) => boolean

      Optional function to test each entity

    Returns Promise<number>

    Count of matching entities

    const totalCount = await repository.count();
    const activeCount = await repository.count(p => p.status === 'active');
  • Deletes multiple entities by their IDs.

    Parameters

    • ids: string[]

      Array of entity IDs (strings) to delete

    Returns Promise<void>

    await repository.deleteMany(['id1', 'id2', 'id3']);
    

Properties

entities: Map<string, TEntity> = ...