Created
September 20, 2022 16:39
-
-
Save newerton/b287493826426f65e52d916af21fd011 to your computer and use it in GitHub Desktop.
This file contains hidden or 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