-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce base repository for basic crud
- Loading branch information
Showing
4 changed files
with
149 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,5 @@ | ||
import { TaskEntity } from "../entities/task_entity"; | ||
import { CreateDto } from "../dtos/tasks/create_dto"; | ||
import { ReadDto } from "../dtos/tasks/read_dto"; | ||
import { UpdateDto } from "../dtos/tasks/update_dto"; | ||
import { DeleteDto } from "../dtos/tasks/delete_dto"; | ||
import { ITaskModelDocument, TaskModel } from "../../models/task_model"; | ||
import { TaskMapper } from "../mappers/task_mapper"; | ||
import { FractalRepository } from "../../../../public/repositories/fracatal_repository"; | ||
|
||
export class TaskRepository { | ||
async create(dto: CreateDto): Promise<TaskEntity> { | ||
const task = await TaskModel.create({ | ||
name: dto.name, | ||
description: dto.description | ||
}); | ||
|
||
return new TaskMapper().fromModel(task); | ||
} | ||
|
||
async read(dto: ReadDto): Promise<TaskEntity[]> { | ||
if (dto.id) { | ||
const task = await TaskModel.findOne({ id: dto.id }); | ||
|
||
if (task !== undefined && task !== null && task.name) { | ||
return [new TaskEntity(task.name, task.description)]; | ||
} | ||
|
||
return []; | ||
} | ||
// Let us read and map to entity but in future this should be memoized | ||
const tasks = await TaskModel.find({}); | ||
const taskEntities = tasks.map((task: ITaskModelDocument) => new TaskMapper().fromModel(task)); | ||
|
||
return Promise.resolve(taskEntities); | ||
} | ||
|
||
async update(dto: UpdateDto): Promise<TaskEntity> { | ||
const updatedTask = await TaskModel.update(dto.id as string, { | ||
name: dto.name, | ||
description: dto.description | ||
}); | ||
|
||
return new TaskMapper().fromModel(updatedTask); | ||
} | ||
|
||
delete(dto: DeleteDto): void { | ||
TaskModel.delete(dto.id as string); | ||
}; | ||
export class TaskRepository extends FractalRepository { | ||
// add other methods | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export class FractalRepository { | ||
private createDto: any; | ||
private readDto: any; | ||
private updateDto: any; | ||
private deleteDto: any; | ||
private mapper: any; | ||
private model: any; | ||
|
||
constructor() { | ||
this.createDto = {}; | ||
this.readDto = {}; | ||
this.updateDto = {}; | ||
this.deleteDto = {}; | ||
this.mapper = {}; | ||
this.model = {}; | ||
} | ||
|
||
setCreateDto(createDto: any): FractalRepository { | ||
this.createDto = createDto; | ||
return this; | ||
} | ||
|
||
setReadDto(readDto: any): FractalRepository { | ||
this.readDto = readDto; | ||
return this; | ||
} | ||
|
||
setUpdateDto(updateDto: any): FractalRepository { | ||
this.updateDto = updateDto; | ||
return this; | ||
} | ||
|
||
setDeleteDto(deleteDto: any): FractalRepository { | ||
this.deleteDto = deleteDto; | ||
return this; | ||
} | ||
|
||
setMapper(mapper: any): FractalRepository { | ||
this.mapper = mapper; | ||
return this; | ||
} | ||
|
||
setModel(model: any): FractalRepository { | ||
this.model = model; | ||
return this; | ||
} | ||
|
||
create = async (): Promise<any> => { | ||
const record = await this.model.create({ ...this.createDto }); | ||
|
||
return this.mapper.fromModel(record); | ||
} | ||
|
||
read = async (): Promise<object[] | never[] | undefined | any[]> => { | ||
if (this.readDto.id !== undefined) { | ||
const record = await this.model.findOne({ id: this.readDto.id }); | ||
|
||
return record._id || record.id ? [this.mapper.fromModel(record)] : []; | ||
} | ||
|
||
const records = await this.model | ||
.find({ ...this.readDto }); | ||
|
||
const entities = records && records.map( | ||
(record: any) => this.mapper | ||
.fromModel(record) | ||
); | ||
|
||
return entities ?? [] | ||
}; | ||
|
||
update = async (): Promise<object | null | any> => { | ||
|
||
const updatedRecord = await this.model | ||
.update(this.updateDto.id as string, { | ||
...this.updateDto | ||
}); | ||
|
||
return updatedRecord | ||
? this.mapper.fromModel(updatedRecord) | ||
: null; | ||
} | ||
|
||
delete = async (): Promise<object | null | any> => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
/* eslint @typescript-eslint/no-unused-vars: 0 */ | ||
const deletedRecord = await this.model.delete(this.deleteDto.id as string); | ||
|
||
return deletedRecord | ||
? this.mapper.fromModel(deletedRecord) | ||
: null; | ||
} | ||
} |