Skip to content

Commit

Permalink
refactor: pr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocaseiro committed Dec 7, 2022
1 parent 3d7e324 commit 443adfc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { UpdateQuery } from 'mongoose';
import { PopulateOptions, UpdateQuery } from 'mongoose';
import { ModelProps, SelectedValues } from '../types';

export type PopulateType = PopulateOptions | (PopulateOptions | string)[];

export interface BaseInterfaceRepository<T> {
findAll(selectedValues?: SelectedValues<T>): Promise<T[]>;

findOneById(id: any, selectedValues?: SelectedValues<T>, populate?: any): Promise<T>;
findOneById(id: any, selectedValues?: SelectedValues<T>, populate?: PopulateType): Promise<T>;

findAllWithQuery(query: any, selectedValues?: SelectedValues<T>, populate?: any): Promise<T[]>;
findAllWithQuery(
query: any,
selectedValues?: SelectedValues<T>,
populate?: PopulateType
): Promise<T[]>;

findOneByField(fields: ModelProps<T>): Promise<T>;

create(item: T): Promise<T>;

update(id: string, item: T);
update(id: string, item: T): Promise<T>;

deleteMany(field: ModelProps<T>, withSession: boolean): Promise<number>;

Expand All @@ -22,11 +28,11 @@ export interface BaseInterfaceRepository<T> {

findOneAndRemoveByField(fields: ModelProps<T>, withSession: boolean): Promise<T>;

startTransaction();
startTransaction(): Promise<void>;

commitTransaction();
commitTransaction(): Promise<void>;

abortTransaction();
abortTransaction(): Promise<void>;

endSession();
endSession(): Promise<void>;
}
10 changes: 4 additions & 6 deletions backend/src/libs/repositories/mongo/mongo-generic.repository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ClientSession, Model, PopulateOptions, UpdateQuery } from 'mongoose';
import { BaseInterfaceRepository } from '../interfaces/base.repository.interface';
import { ClientSession, Model, UpdateQuery } from 'mongoose';
import { BaseInterfaceRepository, PopulateType } from '../interfaces/base.repository.interface';
import { ModelProps, SelectedValues } from '../types';

type PopulateType = PopulateOptions | (PopulateOptions | string)[];

export class MongoGenericRepository<T> implements BaseInterfaceRepository<T> {
protected _repository: Model<T>;
protected _session: ClientSession;
Expand Down Expand Up @@ -49,8 +47,8 @@ export class MongoGenericRepository<T> implements BaseInterfaceRepository<T> {
return this._repository.create(item);
}

update(id: string, item: T) {
return this._repository.findByIdAndUpdate(id, item);
update(id: string, item: T): Promise<T> {
return this._repository.findByIdAndUpdate(id, item).exec();
}

findOneByFieldAndUpdate(value: ModelProps<T>, query: UpdateQuery<T>): Promise<T> {
Expand Down

0 comments on commit 443adfc

Please sign in to comment.