Making Nest.js and TypeORM work with multiple databases

lsmod
2 min readMar 3, 2023

--

NestJS and TypeORM gave me a hard time the other day while trying to figure out how to make an API work with multiple databases.

I have two modules, each one using a different database.

The solution was pretty simple tho… But the official NestJS documentation is missing one little detail on this matter: You have to specify a name when creating the datasource (TypeOrmModule.forRoot — this is in the doc) AND at the repository injection (which is not specified in the documentation 🤦)!

The documentation speaks about specifying the name when injecting a datasource or an entity manager, though.

Anyway, here’s an example of setting up two databases, one per module:

Creating two Datasource:

// app.modules.ts
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";

import { CourseModule } from "./course/course.module";
import { CourseEntity } from "./course/course.entity";
import { StudentModule } from "./student/student.module";
import { StudentEntity } from "./student/entities/student.entity";
import { AppController } from "./app.controller";

@Module({
imports: [
TypeOrmModule.forRoot({
// no name: by default datasource will be named "default"
entities: [StudentEntity],
...moreOptions,
}),
TypeOrmModule.forRoot({
// we name the second DB datasource
// so we can use it later in CourseModule
name: "coursesDB",
entities: [CourseEntity],
...secondDBOptions,
}),
StudentModule,
CourseModule,
],
controllers: [AppController],
providers: [],
})
export class AppModule {}

Using default Datasource:

// student.module.ts
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";

import { StudentController } from "./student.controller";
import { StudentEntity } from "./entities/student.entity";
import { StudentService } from "./services/view.service";

@Module({
// using default datasource
imports: [TypeOrmModule.forFeature([StudentEntity])],
controllers: [StudentController],
providers: [StudentService],
})
export class StudentModule {}

Using second database (at module level)

// course.module.ts
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";

import { CourseEntity } from "./entities/course.entity";
import { CourseController } from "./course.controller";
import { CourseService } from "./services/course.service";

@Module({
// here we specify the name of the datasource
// otherwise it will use the default one...
imports: [TypeOrmModule.forFeature([CourseEntity], "coursesDB")],
controllers: [CourseController],
providers: [CourseService],
})
export class CourseModule {}

Make our service works with 2nd database

// course.service.ts
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";

import { CourseEntity } from "../entities/course.entity";

@Injectable()
export class CourseService {
constructor(
// we need to name it here as well
// I didn't find anything about this detail in the docs
@InjectRepository(CourseEntity, "coursesDB")
private repository: Repository<CourseEntity>
) { }
}

Conclusion

Well that’s it! I hope this post will help someone encountering the same issue.
Thank you for reading!

--

--

lsmod
lsmod

Written by lsmod

Self-taught programmer; passionate about Rust & Blockchains

Responses (2)