Skip to content

Instantly share code, notes, and snippets.

@royib
Created January 19, 2022 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royib/a098bc7b1cde486e35ce6b6ff0b01a74 to your computer and use it in GitHub Desktop.
Save royib/a098bc7b1cde486e35ce6b6ff0b01a74 to your computer and use it in GitHub Desktop.
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