node.jstypescriptsequelize.jstypescript-genericssequelize-typescript

TypeScript - Repository pattern with Sequelize


I'm converting my Express API Template to TypeScript and I'm having some issues with the repositories.

With JavaScript, I would do something like this:

export default class BaseRepository {
  async all() {
    return this.model.findAll();
  }

  // other common methods
}
import BaseRepository from './BaseRepository';
import { User } from '../Models';

export default class UserRepository extends BaseRepository {
  constructor() {
    super();
    this.model = User;
  }

  async findByEmail(email) {
    return this.model.findOne({
      where: {
        email,
      },
    });
  }

  // other methods

Now, with TypeScript, the problem is that it doesn't know the type of this.model, and I can't pass a concrete model to BaseRepository, because, well, it is an abstraction. I've found that sequelize-typescript exports a ModelCtor which declares all the static model methods like findAll, create, etc., and I also could use another sequelize-typescript export which is Model to properly annotate the return type.

So, I ended up doing this:

import { Model, ModelCtor } from 'sequelize-typescript';

export default abstract class BaseRepository {
  protected model: ModelCtor;

  constructor(model: ModelCtor) {
    this.model = model;
  }

  public async all(): Promise<Model[]> {
    return this.model.findAll();
  }

  // other common methods
}
import { Model } from 'sequelize-typescript';
import BaseRepository from './BaseRepository';
import { User } from '../Models';

export default class UserRepository extends BaseRepository {
  constructor() {
    super(User);
  }

  public async findByEmail(email: string): Promise<Model | null> {
    return this.model.findOne({
      where: {
        email,
      },
    });
  }

  // other methods
}

Ok, this works, TypeScript doesn't complain about methods like findOne or create not existing, but that generates another problem.

Now, for example, whenever I get a User from the repository, if I try to access one of its properties, like user.email, TypeScript will complain that this property does not exist. Of course, because the type Model does not know about the specifics of each model.

Ok, it's treason generics then.

Now BaseRepository uses a generic Model type which the methods also use:

export default abstract class BaseRepository<Model> {
  public async all(): Promise<Model[]> {
    return Model.findAll();
  }

  // other common methods
}

And the concrete classes pass the appropriate model to the generic type:

import BaseRepository from './BaseRepository';
import { User } from '../Models';

export default class UserRepository extends BaseRepository<User> {
  public async findByEmail(email: string): Promise<User | null> {
    return User.findOne({
      where: {
        email,
      },
    });
  }

  // other methods
}

Now IntelliSense lights up correctly, it shows both abstract and concrete classes methods and the model properties (e.g. user.email).

But, as you have imagined, that leads to more problems.

Inside BaseRepository, where the methods use the Model generic type, TypeScript complains that 'Model' only refers to a type, but is being used as a value here. Not only that, but TypeScript also doesn't know (again) that the static methods from the model exist, like findAll, create, etc.

Another problem is that in both abstract and concrete classes, as the methods don't use this anymore, ESLint expects the methods to be static: Expected 'this' to be used by class async method 'all'. Ok, I can just ignore this rule in the whole file and the error is gone. It would be even nicer to have all the methods set to static, so I don't have to instantiate the repository, but maybe I'm dreaming too much.

Worth mentioning that although I can just silence those errors with // @ts-ignore, when I execute this, it doesn't work: TypeError: Cannot read property 'create' of undefined\n at UserRepository.<anonymous>

I researched a lot, tried to make all methods static, but static methods can't reference the generic type (because it is considered an instance property), tried some workarounds, tried to pass the concrete model in the constructor of BaseRepository along with the class using the generic type, but nothing seems to work so far.

In case you want to check the code: https://github.com/andresilva-cc/express-api-template/tree/main/src/App/Repositories

EDIT:

Found this: Sequelize-Typescript typeof model

Ok, I removed some unnecessary code from that post and that kinda works:

import { Model } from 'sequelize-typescript';

export default abstract class BaseRepository<M extends Model> {
  constructor(protected model: typeof Model) {}

  public async all(attributes?: string[]): Promise<M[]> {
    // Type 'Model<{}, {}>[]' is not assignable to type 'M[]'.
    // Type 'Model<{}, {}>' is not assignable to type 'M'.
    // 'Model<{}, {}>' is assignable to the constraint of type 'M', but 'M' could be instantiated with a different subtype of constraint 'Model<any, any>'.
    return this.model.findAll({
      attributes,
    });
  }
import BaseRepository from './BaseRepository';
import { User } from '../Models';

export default class UserRepository extends BaseRepository<User> {
  constructor() {
    super(User);
  }
}

I mean, if I put some // @ts-ignore it at least executes, and IntelliSense lights up perfectly, but TypeScript complains.


Solution

  • We faced the same problem. The solution was to declare returning types with an interface that an abstract repository class implements.

    Code for the interface:

    export type RepoResult<M> = Promise<Result<M | undefined, RepoError | undefined>>;
        
    export interface IRepo<M> {
      save(model: M): RepoResult<M>;
      findById(id: string): RepoResult<M>;
      search(parameterName: string, parameterValue: string, sortBy: string, order: number, pageSize: number, pageNumber: number): RepoResult<M[]>;
      getAll(): RepoResult<M[]>;
      deleteById(id: string): RepoResult<M>;
      findByIds(ids: string[]): RepoResult<M[]>;
      deleteByIds(ids: string[]): RepoResult<any>;
    };
    

    Code for the abstract class:

    export abstract class Repo<M extends sequelize.Model> implements IRepo<M> {
      protected Model!: sequelize.ModelCtor<M>;
      constructor(Model: sequelize.ModelCtor<M>) {
        this.Model = Model;
      }
    
      public async save(doc: M) {
        try {
          const savedDoc = await doc.save();
          return Result.ok(savedDoc);
        } catch (ex: any) {
          logger.error(ex);
          return Result.fail(new RepoError(ex.message, 500));
        }
      }
    
      public async findById(id: string) {
        try {
          const doc = await this.Model.findOne({
            where: {
              id: id
            }
          });
          if (!doc) {
            return Result.fail(new RepoError('Not found', 404));
          }
    
          return Result.ok(doc);
        } catch (ex: any) {
          return Result.fail(new RepoError(ex.message, 500));
        }
      }
    }
    

    Hope it helps. Have a nice day:)

    EDIT: Result is a class that looks like this:

    export class Result<V, E> {
      public isSuccess: boolean;
      public isFailure: boolean;
      private error: E;
      private value: V;
    
      private constructor(isSuccess: boolean, value: V, error: E) {
        if (isSuccess && error) {
          throw new Error('Successful result must not contain an error');
        } else if (!isSuccess && value) {
          throw new Error('Unsuccessful error must not contain a value');
        }
    
        this.isSuccess = isSuccess;
        this.isFailure = !isSuccess;
        this.value = value;
        this.error = error;
      }
    
      public static ok<V>(value: V): Result<V, undefined> {
        return new Result(true, value, undefined);
      }
    
      public static fail<E>(error: E): Result<undefined, E> {
        return new Result(false, undefined, error);
      }
    
      public getError(): E {
        if (this.isSuccess) {
          throw new Error('Successful result does not contain an error');
        }
    
        return this.error;
      }
    
      public getValue(): V {
        if (this.isFailure) {
          throw new Error('Unsuccessful result does not contain a value');
        }
    
        return this.value;
      }
    }
    
    type RepoErrorCode = 404 | 500;
    
    export class RepoError extends Error {
      public code: RepoErrorCode;
      constructor(message: string, code: RepoErrorCode) {
        super(message);
        this.code = code;
      }
    }
    

    RepoResult type:

    export type RepoResult<M> = Promise<Result<M | undefined, RepoError | undefined>>;
    

    You can find more info on the pattern at the link below: https://khalilstemmler.com/articles/enterprise-typescript-nodejs/functional-error-handling/