簡體   English   中英

typeorm如何寫入不同的數據庫?

[英]typeorm how to write to different databases?

我正在嘗試在一個數據庫中創建一個表,在另一個數據庫中創建另一個表。 為此,有 2 個實體使用@Entity裝飾器尋址正確的數據庫。 問題是typeorm或者更確切地說是SQL會引發用戶 x 無法在數據庫 y 中寫入的錯誤。

如何正確處理不同的數據庫?

// src/entity/User.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'

@Entity({database: 'db1'})
export class User {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  mame: string
}
// src/entity/Movie.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'

@Entity({database: 'db2'})
export class Movie {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  title: string
}

連接使用其正確的憑據進行創建:

// src/index.ts
    await createConnection({
      name: 'connection1',
      host: 'SERVER1',
      username: 'bob',
      password: 'xxx',
      type: 'mssql',
      database: 'db1',
      synchronize: true,
      entities: ['src/entity/**/*.ts'],
      migrations: ['src/migration/**/*.ts'],
      subscribers: ['src/subscriber/**/*.ts'],
      cli: {
        entitiesDir: 'src/entity',
        migrationsDir: 'src/migration',
        subscribersDir: 'src/subscriber',
      },
    })

    await createConnection({
      name: 'connection2',
      host: 'SERVER2',
      username: 'mike',
      password: 'xxx',
      type: 'mssql',
      database: 'db2',
      synchronize: true,
      entities: ['src/entity/**/*.ts'],
      migrations: ['src/migration/**/*.ts'],
      subscribers: ['src/subscriber/**/*.ts'],
      cli: {
        entitiesDir: 'src/entity',
        migrationsDir: 'src/migration',
        subscribersDir: 'src/subscriber',
      },
    })

我們確實需要使用裝飾器,因為我們還在 class 中使用type-graphql docrator。 有趣的是,當實體的裝飾器留空時,我們會看到在兩個數據庫中都創建了兩個表。 所以憑證是正確的。

我在這里發現了類似的問題並此處請求幫助。

謝謝您的幫助。

在@Aluan 的評論的幫助下,我自己想出了這個問題。 因此,以下是其他任何遇到此問題的人的步驟:

  1. 修改數組entities ,使每個連接/數據庫都有自己的包含實體文件的文件夾,並將您最常使用的連接命名為default
// src/index.ts
 await createConnections([
      {
        name: 'default',
        host: 'SERVER1',
        username: 'bob',
        password: 'kiwi,
        type: 'mssql',
        database: 'db1',
        ...
       "synchronize": true,
       "entities": ["src/db1/entity/**/*.ts"],
      },
      {
        name: 'connection2,
        host: 'SERVER2',
        username: 'Mike',
        password: 'carrot',
        type: 'mssql',
        database: 'db2,
        ...
       "synchronize": true,
       "entities": ["src/db2/entity/**/*.ts"],
    ])
  1. 在其各自的文件夾中為每個數據庫創建實體文件:
    • src/db1/entity/Fruit.ts > db1 中的表
    • src/db2/entity/Vegetables.ts > db2 中的表

使用"synchronize": true每個表將在正確的數據庫中自動創建

  1. 訪問表中的數據:
    • 對於default連接::
import { Fruit} from 'src/db1/entity/Fruit.ts'
  fruits() {
    return Fruit.find()
  }
  • 對於非默認連接:
import { getRepository } from 'typeorm'
import { Vegetable} from 'src/db2/entity/Vegetable.ts'
  vegetables() {
      return async () => await getRepository(Vegetable).find()
  }

或者

  async vegetables() {
    return await getRepository(vegetables, 'connection2').find()
  }

我希望這可以幫助其他人與我一樣遇到同樣的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM