Created
January 19, 2022 15:46
-
-
Save royib/a098bc7b1cde486e35ce6b6ff0b01a74 to your computer and use it in GitHub Desktop.
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
import { Model } from 'mongoose'; | |
import { IGenericRepository } from '../../../core'; | |
export class MongoGenericRepository<T> implements IGenericRepository<T> { | |
private _repository: Model<T>; | |
private _populateOnFind: string[]; | |
constructor(repository: Model<T>, populateOnFind: string[] = []) { | |
this._repository = repository; | |
this._populateOnFind = populateOnFind; | |
} | |
getAll(): Promise<T[]> { | |
return this._repository.find().populate(this._populateOnFind).exec(); | |
} | |
get(id: any): Promise<T> { | |
return this._repository.findById(id).populate(this._populateOnFind).exec(); | |
} | |
create(item: T): Promise<T> { | |
return this._repository.create(item); | |
} | |
update(id: string, item: T) { | |
return this._repository.findByIdAndUpdate(id, item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment