Typeorm connect to multiple database

anıl yıldırım picture anıl yıldırım · Feb 16, 2019 · Viewed 7.9k times · Source

I use node.js , TS and typeorm for back-end project.

I need to connect to a different database in the middleware according to the parameter I send. And I've got to send the query to the database.

ormconfig

[
  {
    "name": "default",
    "type": "postgres",
    "host": "localhost",
    "port": 5432,
    "username": "postgres",
    "password": "12345",
    "database": "dbOne"
  },
  {
    "name": "second-connection",
    "type": "postgres",
    "host": "localhost",
    "port": 5432,
    "username": "postgres",
    "password": "12345",
    "database": "dbTwo"
  }
]

That's my connection settings. After I do that, I'm trying to connect to the middleware.

   const connectionOptions = await getConnectionOptions("second-connection");
   const conTwo = await createConnection(connectionOptions);

   const managerTwo = getManager("second-connection");

   const resultTwo = await managerTwo
      .createQueryBuilder(SysCompany, "company")
      .getOne();

   console.log(resultTwo);

I think I can connect to the database, but I'm having trouble with the repository.

Error

EntityMetadataNotFound: No metadata for "SysCompany" was found.

@Entity()
export class SysCompany extends CoreEntityWithTimestamp {

  @Column({ length: 100 })
  name: string;

  // FK
  // SysPersonnel
  @OneToMany(type => SysPersonnel, personnel => personnel.sysCompany)
  sysPersonnels: SysPersonnel[];

}

Answer

Frank picture Frank · Mar 20, 2019

Maybe typeORM cannot find your JavaScript entity. I had that problem some time ago. You can do the following:

  • Check your destination folder after you built the project. Is your SysCompany.js available?
  • Set the entities property in the configuration. It must contain the path to your JS entities. The typeORM docs state that "Each entity must be registered in your connection options".
{
 "name": "second-connection",
 "type": "postgres",
 "host": "localhost",
 "port": 5432,
 "username": "postgres",
 "password": "12345",
 "database": "dbTwo"
 "entities": ["<path to entities>/**/*.js"]
}

I would also recommend to use a JavaScript configuration file. Your ormconfig.js can then use __dirname (directory name of the current module) to set the path. So if your directories look like this:

project/ormconfig.js
project/dist/entity/SysCompany.js
project/dist/entity/OtherEntity.js

You can use a configuration like this:

import {join} from "path";
...
  entities: [
    join(__dirname, "dist/entity/**/*.js")
  ],
...

You could also prevent duplication by using a base configuration object.

import {join} from "path";

const baseOptions = {
  type: "postgres",
  host: "localhost",
  port: 5432,
  username: "postgres",
  password: "12345",
  entities: [
    join(__dirname, "dist/entity/**/*.js")
  ]
}

const defaultConfig = Object.assign({
  name: "default",
  database: "dbOne",
}, baseOptions);

const secondConfig = Object.assign({
  name: "second-connection",
  database: "dbTwo",
}, baseOptions);

module.exports = [ defaultConfig, secondConfig ];

In the file where you open the connection you could use an import:

import { secondConfig } from "<path to file>/ormconfig";

const conTwo = await createConnection(secondConfig);