From 7780a20a30f399ebcd7ecc1a7f93036a0a6139d4 Mon Sep 17 00:00:00 2001 From: Thiago Zanivan Date: Sun, 12 Apr 2020 03:45:08 -0300 Subject: [PATCH] feat: interfaces for repositories --- src/repository/CrudRepository.ts | 91 ++++++++++++++++++++++++++++++++ src/repository/Repository.ts | 15 ++++++ src/repository/index.ts | 2 + src/rimo.ts | 1 + test/core/AggregateRoot.test.ts | 4 +- 5 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/repository/CrudRepository.ts create mode 100644 src/repository/Repository.ts create mode 100644 src/repository/index.ts diff --git a/src/repository/CrudRepository.ts b/src/repository/CrudRepository.ts new file mode 100644 index 0000000..8d63284 --- /dev/null +++ b/src/repository/CrudRepository.ts @@ -0,0 +1,91 @@ +import { Entity, EntityId } from '@rimo/core' +import { Repository } from './Repository' + +/** + * Interface for generic CRUD operations on a repository for a specific entity. + * + * @typeParam T the entity type the repository manages + * @typeParam ID the type of the id of the entity the repository manages + */ +export interface CrudRepository, ID extends EntityId> + extends Repository { + /** + * Saves a given entity. Use the returned instance for further operations + * as the save operation might have changed the entity instance. + * + * @param entity to save. + * @return the saved entity. + */ + save: (entity: S) => Promise + + /** + * Saves all given entities. + * + * @param entities to save. + * @return the saved entities. + */ + saveAll: (entities: S[]) => Promise + + /** + * Retrieves an entity by its ID. + * + * @param id of the entity. + * @return the entity with the given ID or `undefined` if none found. + */ + findById: (id: ID) => Promise + + /** + * Returns whether an entity with the given ID exists. + * + * @param id of the entity. + * @return `true` if an entity with the given id exists, `false` otherwise. + */ + existsById: (id: ID) => Promise + + /** + * Returns all instances of the type. + * + * @return all entities + */ + findAll: () => Promise + + /** + * Returns all instances of the type `T` with the given IDs. + * + * If some or all ids are not found, no entities are returned for these IDs. + * + * Note that the order of elements in the result is not guaranteed. + * + * @param ids of entities to be retuned. + * @return the entities. + */ + findAllById: (ids: ID[]) => Promise + + /** + * Returns the number of entities. + * + * @return the number of entities. + */ + count: () => Promise + + /** + * Deletes the entity with the given id. + * + * @param id of entity to delete. + */ + deleteById: (id: ID) => Promise + + /** + * Deletes a given entity. + * + * @param entity to delete. + */ + delete: (entity: T) => Promise + + /** + * Deletes the given entities, or all when no arguments are provided. + * + * @param entities to delete or `undefined` to delete all. + */ + deleteAll: (entities?: T[]) => Promise +} diff --git a/src/repository/Repository.ts b/src/repository/Repository.ts new file mode 100644 index 0000000..5242df4 --- /dev/null +++ b/src/repository/Repository.ts @@ -0,0 +1,15 @@ +import { Entity, EntityId } from '@rimo/core' + +/** + * A repository interface. Captures the entity type to manage and its ID type. + * + * This interface's purpose is to hold type information, as well as being able + * to discover interfaces that extend this one. + * + * Domain repositories extending this interface can selectively expose CRUD methods by simply declaring methods of the + * same signature as those declared in {@link CrudRepository}. + * + * @typeParam T the entity type the repository manages + * @typeParam ID the type of the id of the entity the repository manages + */ +export interface Repository, ID extends EntityId> {} diff --git a/src/repository/index.ts b/src/repository/index.ts new file mode 100644 index 0000000..a375d67 --- /dev/null +++ b/src/repository/index.ts @@ -0,0 +1,2 @@ +export * from './Repository' +export * from './CrudRepository' diff --git a/src/rimo.ts b/src/rimo.ts index 46d458a..e702dc4 100644 --- a/src/rimo.ts +++ b/src/rimo.ts @@ -1 +1,2 @@ export * from './core' +export * from './repository' diff --git a/test/core/AggregateRoot.test.ts b/test/core/AggregateRoot.test.ts index cb71b74..2e06db9 100644 --- a/test/core/AggregateRoot.test.ts +++ b/test/core/AggregateRoot.test.ts @@ -109,8 +109,8 @@ describe('Entity test', () => { it('should be able to create domain events', () => { const stats = new PostStats() - new PostLikesAggregatorHandler(stats) - new PostLikesNotificationHandler() + const aggregatorHandler = new PostLikesAggregatorHandler(stats) + const notificationHandler = new PostLikesNotificationHandler() const post = new Post({}) post.like('thiagozf')