Created
March 1, 2021 13:56
-
-
Save Alexzanderk/cf4adc92671e0c9e34c93b8c7a33d7dd to your computer and use it in GitHub Desktop.
sequelize generic repository service
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
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