import type { Aggregate } from "../core/entities.js"; import type { Criteria } from "../criteria.js"; import { PaginatedResult } from "./paginated-result.js"; import { Mapper } from "./mapper.js"; export abstract class ReadRepository> { /** * Find entities based on criteria. * @param criteria - The criteria to use for the search. If not provided, all entities will be returned. (optional) * @returns A promise that resolves to a paginated result of entities. */ abstract find(criteria?: Criteria): Promise>; abstract findById(id: string): Promise; abstract findManyByIds(ids: string[]): Promise; /** * Count the number of entities based on criteria. * @param criteria - The criteria to use for the count. If not provided, all entities will be counted. (optional) * @returns A promise that resolves to the number of entities. */ abstract count(criteria?: Criteria): Promise; /** * Check if an entity exists based on its identifier. * @param id - The identifier of the entity to check. * @returns A promise that resolves to a boolean. */ abstract exists(id: string): Promise; } export abstract class WriteRepository> { /** * * Save or update an entity. * @param entity - The entity to save. * @returns void */ abstract save(entity: Agg): Promise; abstract delete(entity: Agg): Promise; } export abstract class WriteAndRead> { /** * Find entities based on criteria. * @param criteria - The criteria to use for the search. If not provided, all entities will be returned. (optional) * @returns A promise that resolves to a paginated result of entities. */ abstract find(criteria?: Criteria): Promise>; abstract findById(id: string): Promise; abstract findManyByIds(ids: string[]): Promise; /** * * Save or update an entity. * @param entity - The entity to save. * @returns void */ abstract save(entity: Agg): Promise; abstract delete(entity: Agg): Promise; /** * Count the number of entities based on criteria. * @param criteria - The criteria to use for the count. If not provided, all entities will be counted. (optional) * @returns A promise that resolves to the number of entities. */ abstract count(criteria?: Criteria): Promise; /** * Check if an entity exists based on its identifier. * @param id - The identifier of the entity to check. * @returns A promise that resolves to a boolean. */ abstract exists(id: string): Promise; } export abstract class Repository< TDomain extends Aggregate, > extends WriteAndRead { protected abstract readonly toDomainMapper: Mapper; protected abstract readonly toPersistenceMapper: Mapper; /** * Provide the model name of the repository. Usually the table name in the database. * @returns The model name of the repository. */ protected abstract get model(): any; }