Can we create a joint table in TypeORM for tables from different databases?

1.1k views Asked by At

I am trying to create a joint table for two tables, that are from different PostgreSQL databases. Working with TypeORM, I have a problem defining the @ManyToMany(() => 'TableFromAnotherDb') in TypeScript. I've created an interface that has the needed property for the joint table, but having the interface in mind - it's unuseful when it's assigned in the ManyToMany part, because it refers to a type, and I am trying to use it as a value.

Also, does having two simultaneous database connections is necessary here? Because I am trying to mask the interface for the table needed from the second database. Any recommendation for avoiding this problem while keeping my typescript compiler happy?

1

There are 1 answers

1
Alex Wayne On

I highly doubt that TypeORM allows for linking between databases like this. The problem is that most of the relations auto-generate SQL queries that pull in the various data base tables it needs to operate on. If you have two databases, one SQL queries can't get all the info it needs. So your application needs to be the glue that binds them together.

I think the best you can get is store the ID in the entity, and then manually query each connection.

@Entity()
class Thing {
  @Column()
  otherThingId: number
}

// usage
const thing = await ThingRepository.find(123)
const otherThing = await OtherThingRepository.find(thing.otherThingId)