nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇
https://nestjs.com
MIT License
1.91k stars 206 forks source link

Nestjs warns about "TypeOrmModule" taking a long time to serialize #1873

Closed hfhchan-plb closed 8 months ago

hfhchan-plb commented 8 months ago

Is there an existing issue for this?

Current behavior

An error gets logged to the logs:

The module "TypeOrmModule" is taking 10.88ms to serialize, this may be caused by larger objects statically assigned to the module. More details: https://github.com/nestjs/nest/issues/12738
The module "TypeOrmModule" is taking 20.62ms to serialize, this may be caused by larger objects statically assigned to the module. More details: https://github.com/nestjs/nest/issues/12738

Minimum reproduction code


@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      inject: [],
      name: DBConnection.PG,
      useFactory: (): TypeOrmModuleOptions => ({
        name: DBConnection.PG,
        type: 'postgres',
        host: /* redacted */,
        port: /* redacted */,
        username: /* redacted */,
        password: /* redacted */,
        database: /* redacted */,
        entities:/* array of a ton of entities */,
        keepConnectionAlive: true,
        logger: /* redacted large object */
        ssl: {
          ca: /* huge string */,
        },
        applicationName: getApplicationName(),
        uuidExtension: 'pgcrypto',
        installExtensions: false,
      }),
    }),
  ],
})
export class EntityModule {}

Steps to reproduce

No response

Expected behavior

No error

Package version

10.0.1

NestJS version

10.3.0

Node.js version

No response

In which operating systems have you tested?

Other

No response

micalevisk commented 8 months ago

so what's the issue here?

make sure you're using the latest version of @nestjs/core and @nestjs/common

hfhchan-plb commented 8 months ago

Sorry I've just updated the minimum reproduction code.

I'm using 10.3.0 which appears to be the latest version.

micalevisk commented 8 months ago

ok but what's the expected behavior?

that's just an warning message, not an error, right? You can see why it exists in the issue https://github.com/nestjs/nest/issues/12738

hfhchan-plb commented 8 months ago

I think the expected behavior would be that the TypeORM integration would avoid having a large amount of static data to be serialized?

I would like to find a way to remove that 30 ms extra time per cold start 🤔

micalevisk commented 8 months ago

I believe that this is a limitation that we cannot circumvent on the nestjs side yet

hfhchan-plb commented 8 months ago

This can probably be fixed by replacing the useValue with useFactory like the original issue

micalevisk commented 8 months ago

so please try it out changing the js code of your node_modules/@nestjs/typeorm dir

hfhchan-plb commented 8 months ago

Running this locally I'm seeing 0.06 ms - 0.19 ms without the change and 0.07 ms - 0.13 ms with the change with some spikes going up to 1 ms or 2 ms. As I'm using forRootAsync() the only thing being serialized is generateString(), which basically just calls uuid. So it looks like the runtime itself was stalling to produce random values :shrug:

Closing for now.