Skip to content

Instantly share code, notes, and snippets.

@Alexzanderk
Created March 1, 2021 13:56
Show Gist options
  • Select an option

  • Save Alexzanderk/cf4adc92671e0c9e34c93b8c7a33d7dd to your computer and use it in GitHub Desktop.

Select an option

Save Alexzanderk/cf4adc92671e0c9e34c93b8c7a33d7dd to your computer and use it in GitHub Desktop.

Revisions

  1. Alexzanderk created this gist Mar 1, 2021.
    27 changes: 27 additions & 0 deletions repositoryService.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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.');
    }

    }