Skip to content

Commit

Permalink
refactor: make it simple stupid (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
akdasa authored Mar 16, 2023
1 parent f39c5c8 commit f953934
Show file tree
Hide file tree
Showing 5 changed files with 2,168 additions and 1,814 deletions.
11 changes: 5 additions & 6 deletions lib/persistence/Repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Result } from '@lib/core'
import { Aggregate, AnyIdentity } from '@lib/domain/models'
import { Query } from '@lib/persistence'

Expand All @@ -13,19 +12,19 @@ export interface Repository<
* Get all entities.
* @returns All entities.
*/
all(): Promise<Result<readonly TEntity[]>>
all(): Promise<readonly TEntity[]>

/**
* Save entity.
* @param entity Entity to save.
*/
save(entity: TEntity): Promise<Result<void, string>>
save(entity: TEntity): Promise<void>

/**
* Get entity by identity.
* @param id Identity of the entity to load.
*/
get(id: TEntity['id']): Promise<Result<TEntity, string>>
get(id: TEntity['id']): Promise<TEntity>

/**
* Check if entity exists.
Expand All @@ -37,11 +36,11 @@ export interface Repository<
* Find entities by query.
* @param query Query to find entities by.
*/
find(query: Query<TEntity>): Promise<Result<readonly TEntity[]>>
find(query: Query<TEntity>): Promise<readonly TEntity[]>

/**
* Delete entity by identity.
* @param id Identity of the entity to remove.
*/
delete(id: TEntity['id']): Promise<Result<void, string>>
delete(id: TEntity['id']): Promise<void>
}
25 changes: 10 additions & 15 deletions lib/persistence/memory/InMemoryRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Result } from '@lib/core'
import { Aggregate, AnyIdentity } from '@lib/domain/models'
import { Query } from '../Query'
import { Repository } from '../Repository'
Expand All @@ -11,38 +10,34 @@ export class InMemoryRepository<
protected entities = new Map<TEntity['id'], TEntity>()
protected processor = new InMemoryQueryProcessor<TEntity>()

async all(): Promise<Result<readonly TEntity[]>> {
return Result.ok(Array.from(this.entities.values()))
async all(): Promise<readonly TEntity[]> {
return Array.from(this.entities.values())
}

async save(entity: TEntity): Promise<Result<void, string>> {
async save(entity: TEntity): Promise<void> {
const copy = Object.create(entity)
Object.assign(copy, entity)
this.entities.set(entity.id.value, copy)
return Result.ok()
}

async get(id: TEntity['id']): Promise<Result<TEntity, string>> {
async get(id: TEntity['id']): Promise<TEntity> {
const value = this.entities.get(id.value)
if (!value) { return Result.fail(`Entity '${id.value}' not found`) }
return Result.ok(value)
if (!value) { throw new Error(`Entity '${id.value}' not found`) }
return value
}

async exists(id: TEntity['id']): Promise<boolean> {
return this.entities.has(id.value)
}

async find(query: Query<TEntity>): Promise<Result<readonly TEntity[]>> {
async find(query: Query<TEntity>): Promise<readonly TEntity[]> {
const entities = Array.from(this.entities.values())
return Result.ok(this.processor.execute(query, entities))
return this.processor.execute(query, entities)
}

async delete(id: TEntity['id']): Promise<Result<void, string>> {
async delete(id: TEntity['id']): Promise<void> {
const isExists = await this.exists(id)
if (!isExists) {
return Result.fail(`Entity '${id.value}' not found`)
}
if (!isExists) { throw new Error(`Entity '${id.value}' not found`) }
this.entities.delete(id.value)
return Result.ok()
}
}
Loading

0 comments on commit f953934

Please sign in to comment.