TypeORM - multiple databases/schemas

오픈소스·2023년 5월 13일
0

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 request
  • Using 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.
    • You can also specify a table path instead of entity:
      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


https://github.com/typeorm/typeorm/blob/d4607a86723eef07e62e6d7321a07f3ae5ed1f90/src/decorator/entity/Entity.ts

/**
 * 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

https://github.com/typeorm/typeorm/blob/d4607a86723eef07e62e6d7321a07f3ae5ed1f90/src/decorator/options/EntityOptions.ts

0개의 댓글