https://orkhan.gitbook.io/typeorm/docs/multiple-data-sources
Using multiple data sources
import { DataSource } from "typeorm"
const db1DataSource = new DataSource({
...
database: "db1",
...
})
const db2DataSource = new DataSource({
...
database: "db2",
...
})
Using multiple databases within a single data source
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
@Entity({ database: "secondDB" })
export class User {
...
}
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
@Entity({ database: "thirdDB" })
export class Photo {
...
}
User
entity will be created inside secondDB
database and Photo
entity inside thirdDB
database. All other entities will be created in a default database defined in the data source options.const users = await dataSource
.createQueryBuilder()
.select()
.from(User, "user")
.addFrom(Photo, "photo")
.andWhere("photo.userId = user.id")
.getMany()
userId
is not a foreign key since its cross-database requestUsing multiple schemas within a single data source
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
@Entity({ schema: "secondSchema" })
export class User {
...
}
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
@Entity({ schema: "thirdSchema" })
export class User {
...
}
User
entity will be created inside secondSchema
schema and Photo
entity inside thirdSchema
schema. All other entities will be created in a default database defined in the data source options.const users = await dataSource
.createQueryBuilder()
.select()
.from("secondSchema.user", "user")
.addFrom("thirdSchema.photo", "photo")
.andWhere("photo.userId = user.id")
.getMany()
https://orkhan.gitbook.io/typeorm/docs/decorator-reference#entity
@Entity({
name: "users",
engine: "MyISAM",
database: "example_dev",
schema: "schema_with_best_tables",
synchronize: false,
orderBy: {
name: "ASC",
id: "DESC",
},
})
export class User {}
https://orkhan.gitbook.io/typeorm/docs/entities
/**
* This decorator is used to mark classes that will be an entity (table or document depend on database type).
* Database schema will be created for all classes decorated with it, and Repository can be retrieved and used for it.
*/
export function Entity(options?: EntityOptions): ClassDecorator
/**
* This decorator is used to mark classes that will be an entity (table or document depend on database type).
* Database schema will be created for all classes decorated with it, and Repository can be retrieved and used for it.
*/
export function Entity(name?: string, options?: EntityOptions): ClassDecorator