question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeORM takes longer to connect in NestJS than normal

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

TypeORM’s createConnection takes longer to run in NestJS than when you run it on its own. For me it normally takes 2x or longer than it takes outside NestJS.

To test this I edited createConnectionFactory in node_modules/@nestjs/typeorm/dist/typeorm-core.module.js to add var dd = Date.now() before the return yield and the following after .toPromise():

  .then(data => {
    console.log(`finished connection +${Date.now() - dd}ms`);
    return data;
  });

The result for me is:

finished connection +13894ms
[Nest] 25686   - 10/08/2019, 2:48 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +13797ms

Tweaking the code to remove the .pipe(typeorm_utils_1.handleRetry(options.retryAttempts, options.retryDelay)) and remove the RxJS code does not significantly change the timing.

To confirm this is a NestJS issue and not a TypeORM issue, I wrote the following script:

import { createConnection } from 'typeorm';
let dd = Date.now();
createConnection({
  type: 'mariadb',
  host: '127.0.0.1',
  port: 3306,
  username: 'root',
  password: 'password',
  database: 'myproject',
  charset: 'utf8_general_ci',
  entities: [process.cwd() + '/**/*.entity{.ts,.js}'],
  migrations: [process.cwd() + '/migrations/*.ts'],
  synchronize: false,
  logger: 'debug',
})
  .then(conn => {
    console.log(`Completed in +${Date.now() - dd}ms`);
    conn.close();
  })
  .catch(console.error);

This is a typeorm createConnection with the exact same options as NestJS passes createConnection with my config.

My results are:

$ npx ts-node -r tsconfig-paths/register ./test.ts 
Completed in +6422ms

Expected behavior

TypeORM’s createConnection should not take longer in NestJS than it normally takes in ts-node.

Minimal reproduction of the problem with instructions

I’m not sure I can build a minimal reproduction. The performance difference only becomes perceptible when you have a large application.

Environment

Nest version: 6.2.0, @nestjs/typeorm 6.1.3

I’ll try and upgrade later and see if anything is different. But this has been an issue for awhile and I didn’t notice anything performance related in the release notes.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
SylvSylvcommented, Jan 10, 2020

I’ve had the same problem for quite some time, and I tried to load entities directly as suggested by @kamilmysliwiec

MyEntitiesFolder 
|-- Client.entity.ts
|-- Entry.entity.ts
|-- Invoice.entity.ts
|-- MyBaseEntity.entity.ts
|-- Payment.entity.ts
|-- PaymentMethod.entity.ts
|-- Settlement.entity.ts
|-- User.entity.ts
|-- Vat.entity.ts
const configWithHarcodedEntities: TypeOrmModuleOptions = {
  ...configService.getTypeOrmConfig(),
  entities: [
    Client,
    Entry,
    Invoice,
    MyBaseEntity,
    Payment,
    PaymentMethod,
    Settlement,
    User,
    Vat
  ]
}

@Module({
  imports: [
    RouterModule.forRoutes(routes),
    TypeOrmModule.forRoot(
    //   configService.getTypeOrmConfig() //classic ormconfig.json with **/*.entity{.ts,.js}
      configWithHarcodedEntities
    ),
    CrudModule,
    JsonapiModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})

Results

load entities directly:
TypeOrmCoreModule dependencies initialized +68ms
with glob path:
TypeOrmCoreModule dependencies initialized +20000ms - +30000ms

After that I used a more targeted glob path:

{
    ...
    // entities: ['**/*.entity{.ts,.js}'],
    entities: ["src/modules/domain-invoice/*.entity{.ts,.js}"],
}

And loading time was still very small:
TypeOrmCoreModule dependencies initialized +122ms

Hope it helps.

1reaction
kamilmysliwieccommented, Oct 9, 2019

I don’t think this is related to the @nestjs/typeorm implementation specifically. If you look at the source code, you’ll see that not too much really happens around the “native” createConnection function. I’d suggest removing glob paths (both entities and migrations) and load entities directly, then compare the performance - perhaps that’s the place where the issue comes from. Also, one thing to note is that Nest resolves all dependencies asynchronously, so if there are lots of synchronous operations to perform, the timestamp difference will be higher. Although, it doesn’t mean createConnection() took longer, but rather the JS engine couldn’t call the callback yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Check if connection was successful using NestJs and Mysql
If your connection is failing you will get a very clear error in your console: ... createConnection, getConnectionManager } from 'typeorm';
Read more >
Nest.js Step-by-Step: Part 2 - CODE Magazine
js integrates with TypeORM. Finally, the step by step demonstration shows you how to convert the code from Part 1 into code that...
Read more >
SQL (TypeORM) | NestJS - A progressive Node.js framework
The first step we need to do is to establish the connection with our database using new DataSource().initialize() class imported from the typeorm...
Read more >
typeorm/typeorm - Gitter
this is not working for me to find items from mongodb collection. How to use ... Though when I use the custom type...
Read more >
Comparing 4 popular NestJS ORMs - LogRocket Blog
query('SELECT * FROM [TableName];');. You can use either JavaScript or TypeScript with TypeORM. Using TypeORM with TypeScript is more natural as ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found