Skip to content

Commit

Permalink
feat: interfaces for repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozf committed Apr 12, 2020
1 parent a1f4e7c commit 7780a20
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
91 changes: 91 additions & 0 deletions src/repository/CrudRepository.ts
Original file line number Diff line number Diff line change
@@ -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<T extends Entity<any>, ID extends EntityId>
extends Repository<T, ID> {
/**
* 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: <S extends T>(entity: S) => Promise<S>

/**
* Saves all given entities.
*
* @param entities to save.
* @return the saved entities.
*/
saveAll: <S extends T>(entities: S[]) => Promise<S[]>

/**
* 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<T | undefined>

/**
* 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<boolean>

/**
* Returns all instances of the type.
*
* @return all entities
*/
findAll: () => Promise<T[]>

/**
* 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<T[]>

/**
* Returns the number of entities.
*
* @return the number of entities.
*/
count: () => Promise<number>

/**
* Deletes the entity with the given id.
*
* @param id of entity to delete.
*/
deleteById: (id: ID) => Promise<void>

/**
* Deletes a given entity.
*
* @param entity to delete.
*/
delete: (entity: T) => Promise<void>

/**
* Deletes the given entities, or all when no arguments are provided.
*
* @param entities to delete or `undefined` to delete all.
*/
deleteAll: (entities?: T[]) => Promise<void>
}
15 changes: 15 additions & 0 deletions src/repository/Repository.ts
Original file line number Diff line number Diff line change
@@ -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<T extends Entity<any>, ID extends EntityId> {}
2 changes: 2 additions & 0 deletions src/repository/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Repository'
export * from './CrudRepository'
1 change: 1 addition & 0 deletions src/rimo.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './core'
export * from './repository'
4 changes: 2 additions & 2 deletions test/core/AggregateRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 7780a20

Please sign in to comment.