Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeORM custom repository: @EntityRepository and getCustomRepository stopped working

Error@EntityRepository is flagged as deprecated

Short answer

TypeORM 0.3 dropped the old way of making custom repositories. The @EntityRepository decorator, getCustomRepository and AbstractRepository are gone or deprecated. Build the custom repository with dataSource.getRepository(Entity).extend({...}), or in NestJS wrap that call in a provider. Inside a transaction, use manager.withRepository.

  • Applies to TypeORM 0.3, @nestjs/typeorm 11, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place that still uses the old API, so you know how many repositories you must rebuild.

    grep -rn "EntityRepository\|getCustomRepository\|AbstractRepository" src/
  2. 2Match what you see, then jump to its fix

    1. 1'EntityRepository' is deprecated, or the class is never picked upA class decorated with @EntityRepository is still in the codeFix
    2. 2EntityManager's getCustomRepository used within transactions is deprecatedgetCustomRepository is still called inside a transactionFix
    3. 3AbstractRepository is deprecatedCode still extends AbstractRepositoryFix

What does the error look like?

  • @EntityRepository is flagged as deprecated
  • EntityManager's getCustomRepository ... is deprecated. Use withRepository method instead.

Cause 1 of 3

A class decorated with @EntityRepository is still in the code

1Check

The TypeORM 0.3.0 release notes say old ways of custom repository creation were dropped. A class that extends Repository and carries @EntityRepository belongs to that old way.

grep -rn "@EntityRepository" src/

2Fix

Remove the decorator and build the repository from the data source with extend(). Methods you add go into the object you pass; inside them, this is the repository.

// user.repository.ts
import { dataSource } from './data-source';
import { User } from './user.entity';

export const UserRepository = dataSource.getRepository(User).extend({
  findByName(firstName: string, lastName: string) {
    return this.createQueryBuilder('user')
      .where('user.firstName = :firstName', { firstName })
      .andWhere('user.lastName = :lastName', { lastName })
      .getMany();
  },
});

3Verify

Compile the project and call the new method once from a script or test.

npx tsc --noEmit

Confirmed by typeorm.io

Did this fix it?

Cause 2 of 3

getCustomRepository is still called inside a transaction

1Check

A transaction has its own query runner and entity manager, so a repository created at module level does not run inside it. Search for getCustomRepository in transaction callbacks.

grep -rn "getCustomRepository" src/

2Fix

Wrap the extended repository with the manager the transaction gives you.

await dataSource.transaction(async (manager) => {
  const userRepository = manager.withRepository(UserRepository);
  const timber = await userRepository.findByName('Timber', 'Saw');
});

3Verify

Run the transaction and check that rolling it back also undoes the repository writes.

npm test

Confirmed by typeorm.io

Did this fix it?

Cause 3 of 3

Code still extends AbstractRepository

1Check

The 0.3.0 release notes list AbstractRepository as deprecated, with the new custom repository style as the replacement.

grep -rn "extends AbstractRepository" src/

2Fix

Move its methods into an extend() call on the entity repository, as in the first cause, and drop the base class.

export const PostRepository = dataSource.getRepository(Post).extend({
  findPublished() {
    return this.find({ where: { published: true } });
  },
});

3Verify

Compile again and run the tests that touch this repository.

npx tsc --noEmit && npm test

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Read the TypeORM release notes before raising the major or minor version, and search the code for each deprecated name.
  • Keep one exported DataSource and build repositories from it, not from global helper functions.
  • Run the compiler in CI so a deprecated or removed API fails the build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

NestJS GraphQL cannot work out the output type of a field or query

Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.

In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.

2 causes, each with a fix

Back end

IntelliJ IDEA breakpoints are ignored in a NestJS app

No 'Debugger listening' line appears when the NestJS app starts

IntelliJ can only debug a NestJS app when the Node.js process runs with the inspector on and the IDE attaches to its port.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.