Skip to content

Why was the custom repository syntax changed? #9013

@nimloth05

Description

@nimloth05

I'm currently migrating from 0.2 to 0.3 and noticed that the syntax for custom repository was changed. I personally find this change very odd but I assume typeorm devs have a perfectly fine explanation for this - I lack to find it though. Remark: I know that this issue is not a documentation issue per se but SO is not the place to discuss things that's why I decided to post it here.

Where I'm coming from: My project only uses a single relational database with various small custom repositories.

Here is a simple example repository from 0.2:

import {User} from "../bl/user";
import {EntityManager, EntityRepository, Repository} from "typeorm";

@EntityRepository(User)
export class UserRepository extends Repository<User> {

  findByEmail(email: string): Promise<User | undefined> {
    // Implementation not relevant
  }

  isEmailAlreadyInUser(email: string): Promise<boolean> {
      // Implementation not relevant
  }
}

export function getUserRepository(manager: EntityManager): UserRepository {
  return manager.getCustomRepository(UserRepository);
}

New 0.3 version:

// I have to manually create a type so UserRepository exists in type space.
export type UserRepositoryType = Repository<User> & {
  findByEmail(email: string): Promise<User | null>;
  isEmailAlreadyInUser(email: string): Promise<boolean>;
}

// this UserRepository only exists in value space. Without my previous type declaration I cannot return it from my helper function.
const UserRepository: UserRepositoryType = AppDataSource.getRepository(User).extend({
  findByEmail(email: string): Promise<User | null> {
    // Implementation not relevant
  },

  isEmailAlreadyInUser(email: string): Promise<boolean> {
    // Implementation not relevant
  }
});

// Helper function, so clients are shielded of typeoRM access.
export function getUserRepository(manager: EntityManager): UserRepositoryType {
  return manager.withRepository(UserRepository);
}

I'm wondering two things:

  • Why was the class declaration style dropped? It seems much more "TS like" then the other. (Which seems more JS like but JS doesn't know about type space...)
  • Why is the repo bound to a datasource? A repo - as I use it - is a concept from DDD in which case it encapsulates certain aspects of my business logic. Why should this logic change depending on the database I use?

As I sad before, this is not a critic since I'd like to understand the reasoning behind the change first.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions