Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
TypeORM custom repository not overriding createQueryBuilder

I am trying to implement database i18n for TypeORM. I have been able to do that theoratically. But I also want to override built-in repository methods to use i18n (basically intercepting them and adding a query). How can I override createQueryBuilder in TypeORM repository? I can override methods like findMany, findOne and add new methods. But it is not working for createQueryBuilder. I know I can override all select methods in the repository. But I want to do it in one place and tried to override createQueryBuilder. The code shows my custom repository definitions.

    export function TranslatableRepository<T>() {
      return {
        createQueryBuilder<Entity>(
          alias?: string,
          queryRunner?: QueryRunner
        ): SelectQueryBuilder<Entity> {
          throw new Error();
          let qb = this.manager.createQueryBuilder<Entity>(
            this.metadata.target as any,
            alias || this.metadata.targetName,
            queryRunner || this.queryRunner
          );

          qb = qb.leftJoinAndSelect(
            `${alias || this.metadata.targetName}.translations`,
            'translation',
            'translation.locale = :locale',
            {
              locale: this.getLocale()
            }
          );

          return qb;
        },

        getLocale() {
          return getLocaleFromContext();
        }
      } as ThisType<Repository<T> & TTranslatableRepository<T>> &
        TTranslatableRepository<T>;
    }

And I use it like postRepository.extend(TranslatableRepository<Post>()). The createQueryBuilder method above should throw the error. But it is using built-in method. This is the copy of extend method in TypeORM souce code.

    extend(custom) {
        // return {
        //     ...this,
        //     ...custom
        // };
        const thisRepo = this.constructor;
        const { target, manager, queryRunner } = this;
        const cls = new (class extends thisRepo {})(target, manager, queryRunner);
        Object.assign(cls, custom);
        return cls;
    }

How can I override createQueryBuilder or is there any other ways to intercept queries?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda