Giter VIP home page Giter VIP logo

Comments (13)

kamilmysliwiec avatar kamilmysliwiec commented on May 22, 2024 3

See that name:
https://github.com/nestjs/typeorm/blob/master/lib/interfaces/typeorm-options.interface.ts
should be passed at the same level as useFactory in case of the multiple connections.

 TypeOrmModule.forRootAsync({
      name: 'gameServerMainConnection',
      useFactory: (config: ConfigService) => ({
        ...
        type: 'mysql',
        host: '127.0.0.1',
     }),
})

from typeorm.

maroon1 avatar maroon1 commented on May 22, 2024 1

still not working! can't inject relative entity to feature module. @kamilmysliwiec

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: (config: ConfigService) => config.typeorm[0] as any,
      inject: [ConfigService],
    }),
    TypeOrmModule.forRootAsync({
      useFactory: (config: ConfigService) => ({
        name: 'gameServerMainConnection',
        type: 'mysql',
        host: '127.0.0.1',
        port: 3306,
        username: 'root',
        password: 'root',
        database: 'test',
        entities: [SystemSettingEntity],
        logging: true,
        dateStrings: true,
        timezone: '+08:00',
        supportBigNumbers: true,
        bigNumberStrings: false,
      }),
      inject: [ConfigService],
    }),
    TypeOrmModule.forRootAsync({
      useFactory: (config: ConfigService) => config.typeorm[2] as any,
      inject: [ConfigService],
    }),
  ],
  exports: [TypeOrmModule],
})
export class DatabaseModule {}
@Module({
  imports: [
    TypeOrmModule.forFeature([SystemSettingEntity], 'gameServerMainConnection'),
  ],
  controllers: [SystemSettingController],
  providers: [SystemSettingService],
})
export class SystemSettingModule {}
[Nest] 26092   - 2018-9-20 17:12:28   [ExceptionHandler] Nest can't resolve dependencies of the SystemSettingEntityRepository (?). Please make sure that the argument at index [0] is available in the current context. +5ms
Error: Nest can't resolve dependencies of the SystemSettingEntityRepository (?). Please make sure that the argument at index [0] is available in the current context.
    at Injector.lookupComponentInExports (D:\Projects\game-platform\game-management\game-management-server\node_modules\@nestjs\core\injector\injector.js:146:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
    at startup (internal/bootstrap/node.js:238:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
 1: node::DecodeWrite
 2: node::DecodeWrite
 3: v8::internal::interpreter::BytecodeDecoder::Decode
 4: v8::internal::RegExpImpl::Exec
 5: v8::internal::RegExpImpl::Exec
 6: v8::internal::RegExpImpl::Exec
 7: 0000000277904281

from typeorm.

vgladimir avatar vgladimir commented on May 22, 2024 1

@chriscarpenter12 The second example is right. But I don't understand why you return Promise from ConfigService. In the get method you can return string and I think it is better. Btw, It is your decision and I don't know your project specification. If you have a Promises then you can use async function.

TypeOrmModule.forRootAsync({
  useFactory: async (config: ConfigService) => ({
    name: await config.get('some.key.here'),
    ...other keys
  }),
  inject: [ConfigService],
}),

from typeorm.

ddehghan avatar ddehghan commented on May 22, 2024 1

I have minim repro of the name issue outside of the useFactory. it causes and error if I have t ORM connections.
#156 .

from typeorm.

acepace avatar acepace commented on May 22, 2024 1

The documentation seems to be wrong as the result of useFactory is ignored if there is a config named default.

from typeorm.

kamilmysliwiec avatar kamilmysliwiec commented on May 22, 2024

Thanks for reporting. Fixed in 5.2.2!

from typeorm.

Diluka avatar Diluka commented on May 22, 2024

Maybe name property shall be exclude from TypeOrmModuleOptions returned by useFactory

Oh no, my connection name is provided by ConfigService. How to do this?

from typeorm.

chriscarpenter12 avatar chriscarpenter12 commented on May 22, 2024

This threw me off too. I was trying to pass the name key with the result of the useFactory as I was pulling all the variables from my ConfigService.

from typeorm.

vgladimir avatar vgladimir commented on May 22, 2024

@Diluka @chriscarpenter12 Sorry, but was it the problem? Me is also passing connection name from config. All is working fine.

from typeorm.

Diluka avatar Diluka commented on May 22, 2024

I don't use name or ormconfig anymore. I use ConfigService to provide all the options.

from typeorm.

chriscarpenter12 avatar chriscarpenter12 commented on May 22, 2024

@vgladimir Can you show us how you are using ConfigService for the name key?

I could be wrong, but I couldn't get it to work with ConfigService either as name is outside the injection of ConfigService in the useFactory.

TypeOrmModule.forRootAsync({
  name: ConfigService.get('some.key.here'), // <-- Config is inaccessible here
  useFactory: (config: ConfigService) => config.typeorm[2] as any,
  inject: [ConfigService],
}),

Hence we were trying to erroneously pass name with the config in useFactory.

TypeOrmModule.forRootAsync({
  useFactory: (config: ConfigService) => ({
    name: config.get('some.key.here'),
    ...other keys
  }),
  inject: [ConfigService],
}),

The name is a string and we can't return a promise from the ConfigService here.

It also doesn't help the docs of nestjs-config reference passing the name key in the config. Which is where I initially had it too.

from typeorm.

chriscarpenter12 avatar chriscarpenter12 commented on May 22, 2024

@vgladimir I don't disagree about the use of async/await, but my point was that if you try to use the name key in the useFactory response it doesn't work. As @kamilmysliwiec said it needed to be outside of useFactory.

from typeorm.

Rainson12 avatar Rainson12 commented on May 22, 2024

this is also the case when you do useClass => (....)... the name property which you would return in the useClass is omitted. Instead the name property must be defined next to the useClass Property.

from typeorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.