Skip to content

Instantly share code, notes, and snippets.

@Alexzanderk
Created March 1, 2021 13:56
Show Gist options
  • Save Alexzanderk/cf4adc92671e0c9e34c93b8c7a33d7dd to your computer and use it in GitHub Desktop.
Save Alexzanderk/cf4adc92671e0c9e34c93b8c7a33d7dd to your computer and use it in GitHub Desktop.
sequelize generic repository service
type Constructor<T> = new (...args: any[]) => T;
type ModelType<T extends Model<T>> = Constructor<T> & typeof Model;
export interface IRepository<T extends Model> {
get(id: string): Promise<T| null>;
find(where: FindOptions<T>): Promise<T>;
create(model: T): Promise<T>;
update(key: any, model: T): Promise<T>;
}
export class RepositoryService<T extends Model<T>> implements IRepository<T>{
constructor(protected model: ModelType<T>) {}
get(id: string): Promise<T| null> {
return this.model.findByPk<T>(id);
}
find(where: FindOptions<T>): Promise<T> {
throw new Error('Method not implemented.');
}
create(model: T): Promise<T> {
throw new Error('Method not implemented.');
}
update(key: any, model: T): Promise<T> {
throw new Error('Method not implemented.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment