nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nest could not find DataSource element

hfhchan-plb opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

An error is thrown when shutting down:

Error: Nest could not find DataSource element (this provider does not exist in the current context)
    at InstanceLinksHost.get (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/core/injector/instance-links-host.js:15:19)
    at ModuleRef.find (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/core/injector/abstract-instance-resolver.js:8:60)
    at ModuleRef.get (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/core/injector/module.js:349:28)
    at TypeOrmCoreModule.<anonymous> (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:117:47)
    at Generator.next (<anonymous>)
    at /Users/user/git/xxxxxxxxxx/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:20:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:16:12)
    at TypeOrmCoreModule.onApplicationShutdown (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:116:16)
    at callAppShutdownHook (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/core/hooks/on-app-shutdown.hook.js:51:35)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async NestApplication.callShutdownHook (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/core/nest-application-context.js:240:13)
    at async NestApplication.close (/Users/user/git/xxxxxxxxxx/node_modules/@nestjs/core/nest-application-context.js:107:9)
    at async /Users/user/git/xxxxxxxxxx/dist/apps/api/src/swagger.js:72:13

Minimum reproduction code

Steps to reproduce

No response

Expected behavior

No Crash

Package version

9.0.1

NestJS version

9.2.1

Node.js version

18.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

The issue looks like it could be fixed by moving

const dataSource = this.moduleRef.get<DataSource>(
getDataSourceToken(this.options as DataSourceOptions) as Type<DataSource>,
);
into the try/catch loop below, but I'm not sure why this.moduleRef.get(getDataSourceToken(this.options)); would be returning undefined.

OK this is not working because I did this:

    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      name: 'pg',
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        /* SNIP */
      }),
    })

instead of:

    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      name: 'pg',
      useFactory: (configService: ConfigService) => ({
        name: 'pg',
        type: 'postgres',
        /* SNIP */
      }),
    })

i.e. I was missing the name parameter inside the object returned by useFactory, hence it couldn't match it up.

This is actually also related to #1512, where the name parameter in TypeOrmModuleOptions is marked deprecated because it inherits fromtypeorm, but is actually required by @nestjs/typeorm to function correctly.