Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple databases with Sequelize #183

Closed
picbenoit opened this issue Jun 19, 2020 · 3 comments
Closed

Multiple databases with Sequelize #183

picbenoit opened this issue Jun 19, 2020 · 3 comments

Comments

@picbenoit
Copy link

Hi,

I'm trying to add a new database connection on my Nest project, but I do not success to do it even following the documentation (https://docs.nestjs.com/techniques/database#multiple-databases-1)

All was working well with my first database connection.

The error I obtain is "Nest can't resolve dependencies of the AccountsService (?, heroku_AccountRepository). Please make sure that the argument herokuConnection at index [0] is available in the AccountsModule context."

Here is my connections :

[
  SequelizeModule.forRootAsync({
    imports: [ConfigModule],
    useFactory: (configService: ConfigService) => ({
      dialect: 'postgres',
      host: configService.get<string>('db.host'),
      port: configService.get<number>('db.port'),
      username: configService.get<string>('db.user'),
      password: configService.get<string>('db.password'),
      database: configService.get<string>('db.database'),
      schema: configService.get<string>('db.schema'),
      autoLoadModels: true,
      synchronize: false,
    }),
    inject: [ConfigService],
  }),
  SequelizeModule.forRootAsync({
    imports: [ConfigModule],
    useFactory: (configService: ConfigService) => ({
      name: 'heroku',
      dialect: 'postgres',
      host: configService.get<string>('heroku.host'),
      port: configService.get<number>('heroku.port'),
      username: configService.get<string>('heroku.user'),
      password: configService.get<string>('heroku.password'),
      database: configService.get<string>('heroku.database'),
      schema: configService.get<string>('heroku.schema'),
      autoLoadModels: true,
      synchronize: false,
    }),
    inject: [ConfigService],
  })
]

Here is my module :

import { Module } from '@nestjs/common';
import { AccountsService } from './accounts.service';
import { Account } from './account.model';
import { SequelizeModule } from '@nestjs/sequelize';

@Module({
  imports: [
    SequelizeModule.forFeature([Account], 'heroku')
  ],
  providers: [AccountsService],
  exports: [AccountsService]
})
export class AccountsModule {}

Here is my service :

import { Injectable } from '@nestjs/common';
import { Account } from './account.model';
import { InjectModel } from '@nestjs/sequelize';

@Injectable()
export class AccountsService {
  constructor(
    @InjectModel(Account, 'heroku')
    private model: typeof Account,
  ) {}

  async find(sfid: string): Promise<Account> {
    return this.model.findOne({
      where: { sfid }
    });
  }
}

I saw that maybe I need to inject the connection on the service but I do not success to do it. Specially I do not understand what is the Sequelize type in the example.

Does exist some github repo with sequelize multiple datatase example ?

Thank you by advance for your help,

Ben.

@kamilmysliwiec kamilmysliwiec transferred this issue from nestjs/nest Jun 22, 2020
@kamilmysliwiec
Copy link
Member

Please provide a minimum reproduction repository.

@picbenoit
Copy link
Author

Hello @kamilmysliwiec

I thought I had put the essentials. Whick parts of code do you need more ?

I was a project with one connection worked well. I tried to put a new postgres connection whom I named heroku. The first one does not have name (default). I create a new module Accounts, with module, service and model files (module and service file are shown above).

I specify "heroku" connection for my account model :
SequelizeModule.forFeature([Account], 'heroku')

@InjectModel(Account, 'heroku')

And I finally obtain "Nest can't resolve dependencies of the AccountsService (?, heroku_AccountRepository). Please make sure that the argument herokuConnection at index [0] is available in the AccountsModule context."

If I do not put "heroku" connection parameter, my app is compiling.

@picbenoit
Copy link
Author

After a lot of tries and checking TypeORM issues, I find a solution to my issue.

The name of the connection must be outside the useFactory, like this :

  SequelizeModule.forRootAsync({
    imports: [ConfigModule],
    // HERE
    name: 'heroku',
    useFactory: (configService: ConfigService) => ({
      // NOT HERE
      // name: 'heroku',
      dialect: 'postgres',
      host: configService.get<string>('heroku.host'),
      port: configService.get<number>('heroku.port'),
      username: configService.get<string>('heroku.user'),
      password: configService.get<string>('heroku.password'),
      database: configService.get<string>('heroku.database'),
      schema: configService.get<string>('heroku.schema'),
      autoLoadModels: true,
      synchronize: false,
    }),
    inject: [ConfigService],
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants