【TS】你們要的TypeORM中文文檔Ta來了

TypeORM

TypeORM 是一個ORM框架,它可以運(yùn)行在 NodeJS、Browser、Cordova、PhoneGap、Ionic、React Native、Expo 和 Electron 平臺上,可以與 TypeScript 和 JavaScript (ES5,ES6,ES7,ES8)一起使用。 它的目標(biāo)是始終支持最新的 JavaScript 特性并提供額外的特性以幫助你開發(fā)任何使用數(shù)據(jù)庫的(不管是只有幾張表的小型應(yīng)用還是擁有多數(shù)據(jù)庫的大型企業(yè)應(yīng)用)應(yīng)用程序。

不同于現(xiàn)有的所有其他 JavaScript ORM 框架,TypeORM 支持 Active RecordData Mapper 模式,這意味著你可以以最高效的方式編寫高質(zhì)量的、松耦合的、可擴(kuò)展的、可維護(hù)的應(yīng)用程序。

TypeORM 參考了很多其他優(yōu)秀 ORM 的實(shí)現(xiàn), 比如 Hibernate, DoctrineEntity Framework

TypeORM 的一些特性:

  • 支持 DataMapperActiveRecord (隨你選擇)
  • 實(shí)體和列
  • 數(shù)據(jù)庫特性列類型
  • 實(shí)體管理
  • 存儲庫和自定義存儲庫
  • 清晰的對象關(guān)系模型
  • 關(guān)聯(lián)(關(guān)系)
  • 貪婪和延遲關(guān)系
  • 單向的,雙向的和自引用的關(guān)系
  • 支持多重繼承模式
  • 級聯(lián)
  • 索引
  • 事務(wù)
  • 遷移和自動遷移
  • 連接池
  • 主從復(fù)制
  • 使用多個數(shù)據(jù)庫連接
  • 使用多個數(shù)據(jù)庫類型
  • 跨數(shù)據(jù)庫和跨模式查詢
  • 優(yōu)雅的語法,靈活而強(qiáng)大的 QueryBuilder
  • 左聯(lián)接和內(nèi)聯(lián)接
  • 使用聯(lián)查查詢的適當(dāng)分頁
  • 查詢緩存
  • 原始結(jié)果流
  • 日志
  • 監(jiān)聽者和訂閱者(鉤子)
  • 支持閉包表模式
  • 在模型或者分離的配置文件中聲明模式
  • json / xml / yml / env 格式的連接配置
  • 支持 MySQL / MariaDB / Postgres / SQLite / Microsoft SQL Server / Oracle / sql.js
  • 支持 MongoDB NoSQL 數(shù)據(jù)庫
  • 可在 NodeJS / 瀏覽器 / Ionic / Cordova / React Native / Expo / Electron 平臺上使用
  • 支持 TypeScript 和 JavaScript
  • 生成高性能、靈活、清晰和可維護(hù)的代碼
  • 遵循所有可能的最佳實(shí)踐
  • 命令行工具

還有更多...

TypeORM功能很強(qiáng)大,但是文檔也有點(diǎn).......

文檔始終有點(diǎn)不如意啊...

痛點(diǎn):

typeorm的文檔說明站點(diǎn):https://typeorm.io/ 國內(nèi)訪問超級慢,內(nèi)容還無法加載,百度了一大堆,沒有一篇文檔是全的。要么就是寫到一半的文檔, 對初學(xué)者來說,入門就更難了。

解決方案:

為了讓更多的人方便學(xué)習(xí),站長業(yè)余時間,將源站typeorm.io的文檔內(nèi)容整理并同步了一份到

TypeORM 中文網(wǎng) https://typeorm.biunav.com/

若后期還有建議搭建的文檔站點(diǎn),請留言。

快速開始

通過使用 TypeORM 你的 models 看起來像這樣:

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstName: string;

  @Column()
  lastName: string;

  @Column()
  age: number;
}

邏輯操作就像是這樣:

const user = new User();
user.firstName = "Timber";
user.lastName = "Saw";
user.age = 25;
await repository.save(user);

const allUsers = await repository.find();
const firstUser = await repository.findOne(1); // find by id
const timber = await repository.findOne({
  firstName: "Timber",
  lastName: "Saw",
});

await repository.remove(timber);

或者,如果你更喜歡使用ActiveRecord實(shí)現(xiàn),也可以這樣用:

import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";

@Entity()
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstName: string;

  @Column()
  lastName: string;

  @Column()
  age: number;
}

邏輯操作如下所示:

const user = new User();
user.firstName = "Timber";
user.lastName = "Saw";
user.age = 25;
await user.save();

const allUsers = await User.find();
const firstUser = await User.findOne(1);
const timber = await User.findOne({ firstName: "Timber", lastName: "Saw" });

await timber.remove();

入門

安裝

  1. 通過npm安裝:

    npm install typeorm --save

  2. 你還需要安裝 reflect-metadata:

    npm install reflect-metadata --save

    并且需要在應(yīng)用程序的全局位置導(dǎo)入(例如在app.ts中)

    import "reflect-metadata";

  3. 你可能還需要安裝 node typings(以此來使用 Node 的智能提示):

    npm install @types/node --save

  4. 安裝數(shù)據(jù)庫驅(qū)動:

    • MySQL 或者 MariaDB

      npm install mysql --save (也可以安裝 mysql2)

    • PostgreSQL

      npm install pg --save

    • SQLite

      npm install sqlite3 --save

    • Microsoft SQL Server

      npm install mssql --save

    • sql.js

      npm install sql.js --save

    • Oracle

      npm install oracledb --save

      根據(jù)你使用的數(shù)據(jù)庫,僅安裝其中一個即可。
      要使 Oracle 驅(qū)動程序正常工作,需要按照其站點(diǎn)中的安裝說明進(jìn)行操作。

    • MongoDB (試驗(yàn)性)

      npm install mongodb --save

    • NativeScript, react-nativeCordova

      查看 支持的平臺

TypeScript 配置

此外,請確保你使用的是 TypeScript 編譯器版本2.3或更高版本,并且已經(jīng)在tsconfig.json中啟用了以下設(shè)置:

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

你可能還需要在編譯器選項(xiàng)的lib中啟用es6,或者從@types安裝es6-shim。

快速開始

開始使用 TypeORM 的最快方法是使用其 CLI 命令生成啟動項(xiàng)目。
只有在 NodeJS 應(yīng)用程序中使用 TypeORM 時,此操作才有效。如果你使用的是其他平臺,請繼續(xù)執(zhí)行分步指南。

首先全局安裝 TypeORM:

npm install typeorm -g

然后轉(zhuǎn)到要創(chuàng)建新項(xiàng)目的目錄并運(yùn)行命令:

typeorm init --name MyProject --database mysql

其中name是項(xiàng)目的名稱,database是您將使用的數(shù)據(jù)庫。

數(shù)據(jù)庫可以是以下值之一: mysql, mariadb, postgres, sqlite, mssql, oracle, mongodb,
cordova, react-native, expo, nativescript.

此命令將在MyProject目錄中生成一個包含以下文件的新項(xiàng)目:

MyProject
├── src              // TypeScript 代碼
│   ├── entity       // 存儲實(shí)體(數(shù)據(jù)庫模型)的位置
│   │   └── User.ts  // 示例 entity
│   ├── migration    // 存儲遷移的目錄
│   └── index.ts     // 程序執(zhí)行主文件
├── .gitignore       // gitignore文件
├── ormconfig.json   // ORM和數(shù)據(jù)庫連接配置
├── package.json     // node module 依賴
├── README.md        // 簡單的 readme 文件
└── tsconfig.json    // TypeScript 編譯選項(xiàng)

你還可以在現(xiàn)有 node 項(xiàng)目上運(yùn)行typeorm init,但要注意,此操作可能會覆蓋已有的某些文件。

接下來安裝項(xiàng)目依賴項(xiàng):

cd MyProject
npm install

在安裝過程中,編輯ormconfig.json文件并在其中放置您自己的數(shù)據(jù)庫連接配置選項(xiàng):

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "test",
  "password": "test",
  "database": "test",
  "synchronize": true,
  "logging": false,
  "entities": ["src/entity/**/*.ts"],
  "migrations": ["src/migration/**/*.ts"],
  "subscribers": ["src/subscriber/**/*.ts"]
}

絕大多數(shù)情況下,你只需要配置
host, username, password, database 或者 port。

完成配置并安裝所有 node modules 后,即可運(yùn)行應(yīng)用程序:

npm start

至此你的應(yīng)用程序應(yīng)該成功運(yùn)行并將新用戶插入數(shù)據(jù)庫。你可以繼續(xù)使用此項(xiàng)目并集成所需的其他模塊并創(chuàng)建更多實(shí)體。

你可以通過運(yùn)行typeorm init --name MyProject --database mysql --express來生成一個更高級的 Express 項(xiàng)目

分步指南

您對 ORM 有何期待?您期望它將為您創(chuàng)建數(shù)據(jù)庫表,并且無需編寫大量難以維護(hù)的 SQL 語句來查找/插入/更新/刪除您的數(shù)據(jù)。本指南將向您展示如何從頭開始設(shè)置 TypeORM 并實(shí)現(xiàn)這些操作。

創(chuàng)建一個模型

使用數(shù)據(jù)庫從創(chuàng)建表開始。如何告訴 TypeORM 創(chuàng)建數(shù)據(jù)庫表?答案是 - 通過模型。
應(yīng)用程序中的模型即是數(shù)據(jù)庫中的表。

舉個例子, 你有一個Photo 模型:

export class Photo {
  id: number;
  name: string;
  description: string;
  filename: string;
  views: number;
}

并且希望將 photos 存儲在數(shù)據(jù)庫中。要在數(shù)據(jù)庫中存儲內(nèi)容,首先需要一個數(shù)據(jù)庫表,并從模型中創(chuàng)建數(shù)據(jù)庫表。但是并非所有模型,只有您定義為entities的模型。

創(chuàng)建一個實(shí)體

Entity是由@Entity裝飾器裝飾的模型。將為此類模型創(chuàng)建數(shù)據(jù)庫表。你可以使用 TypeORM 處理各處的實(shí)體,可以使用它們 load/insert/update/remove 并執(zhí)行其他操作。

讓我們將Photo模型作為一個實(shí)體

import { Entity } from "typeorm";

@Entity()
export class Photo {
  id: number;
  name: string;
  description: string;
  filename: string;
  views: number;
  isPublished: boolean;
}

現(xiàn)在,將為Photo實(shí)體創(chuàng)建一個數(shù)據(jù)庫表,我們將能夠在應(yīng)用程序中的任何位置使用它。
我們已經(jīng)創(chuàng)建了一個數(shù)據(jù)庫表,但是沒有哪個字段屬于哪一列,下面讓我們在數(shù)據(jù)庫表中創(chuàng)建幾列。

添加表列

要添加數(shù)據(jù)庫列,你只需要將要生成的實(shí)體屬性加上@Column裝飾器。

import { Entity, Column } from "typeorm";

@Entity()
export class Photo {
  @Column()
  id: number;

  @Column()
  name: string;

  @Column()
  description: string;

  @Column()
  filename: string;

  @Column()
  views: number;

  @Column()
  isPublished: boolean;
}

現(xiàn)在 id, name, description, filename, viewsisPublished 列將會被添加到photo表中。
數(shù)據(jù)庫中的列類型是根據(jù)你使用的屬性類型推斷的,例如: number將被轉(zhuǎn)換為integer,string將轉(zhuǎn)換為varcharboolean轉(zhuǎn)換為bool等。但你也可以通過在@Column裝飾器中隱式指定列類型來使用數(shù)據(jù)庫支持的任何列類型。

我們已經(jīng)生成了一個包含列的數(shù)據(jù)庫表,但還剩下一件事。每個數(shù)據(jù)庫表必須具有包含主鍵的列。

創(chuàng)建主列

每個實(shí)體必須至少有一個主鍵列。這是必須的,你無法避免。要使列成為主鍵,您需要使用@PrimaryColumn裝飾器。

import { Entity, Column, PrimaryColumn } from "typeorm";

@Entity()
export class Photo {
  @PrimaryColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  description: string;

  @Column()
  filename: string;

  @Column()
  views: number;

  @Column()
  isPublished: boolean;
}

創(chuàng)建自動生成的列

假設(shè)你希望 id 列自動生成(這稱為 auto-increment/sequence/serial/generated identity column)。為此你需要將@PrimaryColumn裝飾器更改為@PrimaryGeneratedColumn裝飾器:

import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Photo {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  description: string;

  @Column()
  filename: string;

  @Column()
  views: number;

  @Column()
  isPublished: boolean;
}

列數(shù)據(jù)類型

接下來,讓我們修復(fù)數(shù)據(jù)類型。默認(rèn)情況下,字符串被映射到一個 varchar(255)類型(取決于數(shù)據(jù)庫類型)。
數(shù)字被映射到一個類似整數(shù)類型(取決于數(shù)據(jù)庫類型)。但是我們不希望所有的列都是有限的 varchars 或整數(shù),讓我們修改下代碼以設(shè)置想要的數(shù)據(jù)類型:

import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Photo {
  @PrimaryGeneratedColumn()
  id: number;

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

  @Column("text")
  description: string;

  @Column()
  filename: string;

  @Column("double")
  views: number;

  @Column()
  isPublished: boolean;
}

列類型是特定于數(shù)據(jù)庫的。你可以設(shè)置數(shù)據(jù)庫支持的任何列類型。有關(guān)支持的列類型的更多信息,請參見此處

創(chuàng)建數(shù)據(jù)庫的連接

當(dāng)實(shí)體被創(chuàng)建后,讓我們創(chuàng)建一個index.ts(或app.ts,無論你怎么命名)文件,并配置數(shù)據(jù)庫連接::

import "reflect-metadata";
import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";

createConnection({
  type: "mysql",
  host: "localhost",
  port: 3306,
  username: "root",
  password: "admin",
  database: "test",
  entities: [Photo],
  synchronize: true,
  logging: false,
})
  .then((connection) => {
    // 這里可以寫實(shí)體操作相關(guān)的代碼
  })
  .catch((error) => console.log(error));

我們在此示例中使用 MySQL,你可以使用任何其他受支持的數(shù)據(jù)庫。要使用其他數(shù)據(jù)庫,只需將選項(xiàng)中的type更改為希望使用的數(shù)據(jù)庫類型:mysql,mariadb,postgres,sqlite,mssql,oracle,cordova,nativescript,react-native,expo 或 mongodb。同時還要確保 host, port, username, password 和數(shù)據(jù)庫設(shè)置的正確性。

我們將 Photo 實(shí)體添加到此連接的實(shí)體列表中。所有需要在連接中使用的每個實(shí)體都必須加到這個表中。

設(shè)置synchronize可確保每次運(yùn)行應(yīng)用程序時實(shí)體都將與數(shù)據(jù)庫同步。

加載目錄中所有實(shí)體

之后當(dāng)我們創(chuàng)建更多實(shí)體時,都需要將一一它們添加到配置中的實(shí)體中,但是這不是很方便,所以我們可以設(shè)置整個目錄,從中連接所有實(shí)體并在連接中使用:

import { createConnection } from "typeorm";

createConnection({
  type: "mysql",
  host: "localhost",
  port: 3306,
  username: "root",
  password: "admin",
  database: "test",
  entities: [__dirname + "/entity/*.js"],
  synchronize: true,
})
  .then((connection) => {
    // 這里可以寫實(shí)體操作相關(guān)的代碼
  })
  .catch((error) => console.log(error));

但要小心這種方法。
如果使用的是ts-node,則需要指定.ts文件的路徑。
如果使用的是outDir,那么需要在outDir目錄中指定.js文件的路徑。
如果使用outDir,當(dāng)你刪除或重命名實(shí)體時,請確保清除outDir目錄并再次重新編譯項(xiàng)目,因?yàn)楫?dāng)你刪除.ts源文件時,其編譯的.js版本不會從輸出目錄中刪除,并且 TypeORM 依然會從outDir中加載這些文件,從而導(dǎo)致異常。

啟動應(yīng)用

現(xiàn)在可以啟動app.ts,啟動后可以發(fā)現(xiàn)數(shù)據(jù)庫自動被初始化,并且 Photo 這個表也會創(chuàng)建出來。

+-------------+--------------+----------------------------+
|                         photo                           |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| name        | varchar(500) |                            |
| description | text         |                            |
| filename    | varchar(255) |                            |
| views       | int(11)      |                            |
| isPublished | boolean      |                            |
+-------------+--------------+----------------------------+

添加和插入 photo

現(xiàn)在創(chuàng)建一個新的 photo 存到數(shù)據(jù)庫:

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";

createConnection(/*...*/)
  .then((connection) => {
    let photo = new Photo();
    photo.name = "Me and Bears";
    photo.description = "I am near polar bears";
    photo.filename = "photo-with-bears.jpg";
    photo.views = 1;
    photo.isPublished = true;

    return connection.manager.save(photo).then((photo) => {
      console.log("Photo has been saved. Photo id is", photo.id);
    });
  })
  .catch((error) => console.log(error));

保存實(shí)體后,它將獲得新生成的 ID。 save方法返回傳遞給它的同一對象的實(shí)例。但它不是對象的新副本,只是修改了它的"id"并返回它。

使用 async/await 語法

我們可以使用最新的 ES8(ES2017)功能,并使用 async / await 語法代替:

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";

createConnection(/*...*/)
  .then(async (connection) => {
    let photo = new Photo();
    photo.name = "Me and Bears";
    photo.description = "I am near polar bears";
    photo.filename = "photo-with-bears.jpg";
    photo.views = 1;
    photo.isPublished = true;

    await connection.manager.save(photo);
    console.log("Photo has been saved");
  })
  .catch((error) => console.log(error));

使用 Entity Manager

我們剛創(chuàng)建了一張新 photo 并將其保存在數(shù)據(jù)庫中。使用EntityManager你可以操縱應(yīng)用中的任何實(shí)體。

例如,加載已經(jīng)保存的實(shí)體:

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";

createConnection(/*...*/)
  .then(async (connection) => {
    /*...*/
    let savedPhotos = await connection.manager.find(Photo);
    console.log("All photos from the db: ", savedPhotos);
  })
  .catch((error) => console.log(error));

savedPhotos是一個 Photo 對象數(shù)組,其中包含從數(shù)據(jù)庫加載的數(shù)據(jù)。

了解更多有關(guān) EntityManager 的信息。

使用 Repositories

現(xiàn)在讓我們重構(gòu)之前的代碼,并使用Repository而不是EntityManager。每個實(shí)體都有自己的存儲庫,可以處理其實(shí)體的所有操作。當(dāng)你經(jīng)常處理實(shí)體時,Repositories 比 EntityManagers 更方便使用:

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";

createConnection(/*...*/)
  .then(async (connection) => {
    let photo = new Photo();
    photo.name = "Me and Bears";
    photo.description = "I am near polar bears";
    photo.filename = "photo-with-bears.jpg";
    photo.views = 1;
    photo.isPublished = true;

    let photoRepository = connection.getRepository(Photo);

    await photoRepository.save(photo);
    console.log("Photo has been saved");

    let savedPhotos = await photoRepository.find();
    console.log("All photos from the db: ", savedPhotos);
  })
  .catch((error) => console.log(error));

了解更多有關(guān) Repository 的信息。

從數(shù)據(jù)庫加載

讓我們使用 Repository 嘗試更多的加載操作:

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";

createConnection(/*...*/)
  .then(async (connection) => {
    /*...*/
    let allPhotos = await photoRepository.find();
    console.log("All photos from the db: ", allPhotos);

    let firstPhoto = await photoRepository.findOne(1);
    console.log("First photo from the db: ", firstPhoto);

    let meAndBearsPhoto = await photoRepository.findOne({
      name: "Me and Bears",
    });
    console.log("Me and Bears photo from the db: ", meAndBearsPhoto);

    let allViewedPhotos = await photoRepository.find({ views: 1 });
    console.log("All viewed photos: ", allViewedPhotos);

    let allPublishedPhotos = await photoRepository.find({ isPublished: true });
    console.log("All published photos: ", allPublishedPhotos);

    let [allPhotos, photosCount] = await photoRepository.findAndCount();
    console.log("All photos: ", allPhotos);
    console.log("Photos count: ", photosCount);
  })
  .catch((error) => console.log(error));

在數(shù)據(jù)庫中更新

讓我們從數(shù)據(jù)庫加載出 photo,更新并保存到數(shù)據(jù)庫:

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";

createConnection(/*...*/)
  .then(async (connection) => {
    /*...*/
    let photoToUpdate = await photoRepository.findOne(1);
    photoToUpdate.name = "Me, my friends and polar bears";
    await photoRepository.save(photoToUpdate);
  })
  .catch((error) => console.log(error));

這個id = 1的 photo 在數(shù)據(jù)庫中就成功更新了。

從數(shù)據(jù)庫中刪除

讓我們從數(shù)據(jù)庫中刪除我們的 Photo:

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";

createConnection(/*...*/)
  .then(async (connection) => {
    /*...*/
    let photoToRemove = await photoRepository.findOne(1);
    await photoRepository.remove(photoToRemove);
  })
  .catch((error) => console.log(error));

這個id = 1的 photo 在數(shù)據(jù)庫中被移除了。

創(chuàng)建一對一的關(guān)系

讓我們與另一個類創(chuàng)建一對一的關(guān)系。先在PhotoMetadata.ts中創(chuàng)建一個新類。此 PhotoMetadata 類應(yīng)包含 photo 的其他元信息:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  OneToOne,
  JoinColumn,
} from "typeorm";
import { Photo } from "./Photo";

@Entity()
export class PhotoMetadata {
  @PrimaryGeneratedColumn()
  id: number;

  @Column("int")
  height: number;

  @Column("int")
  width: number;

  @Column()
  orientation: string;

  @Column()
  compressed: boolean;

  @Column()
  comment: string;

  @OneToOne((type) => Photo)
  @JoinColumn()
  photo: Photo;
}

這里我們使用了一個名為@OneToOne的新裝飾器,它允許我們在兩個實(shí)體之間創(chuàng)建一對一的關(guān)系。
type => Photo是一個函數(shù),返回我們想要與之建立關(guān)系的實(shí)體的類。由于特定于語言的關(guān)系,我們只能使用一個返回類的函數(shù),而不是直接使用該類。
同時也可以把它寫成()=> Photo,但是type => Photo顯得代碼更有可讀性。type 變量本身不包含任何內(nèi)容。

我們還添加了一個@JoinColumn裝飾器,表明實(shí)體鍵的對應(yīng)關(guān)系。關(guān)系可以是單向的或雙向的。但是只有一方是擁有者。在關(guān)系的所有者方面需要使用@JoinColumn 裝飾器。

如果運(yùn)行該應(yīng)用程序,你將看到一個新生成的表,它將包含一個帶有關(guān)系外鍵的列:

+-------------+--------------+----------------------------+
|                     photo_metadata                      |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| height      | int(11)      |                            |
| width       | int(11)      |                            |
| comment     | varchar(255) |                            |
| compressed  | boolean      |                            |
| orientation | varchar(255) |                            |
| photoId     | int(11)      | FOREIGN KEY                |
+-------------+--------------+----------------------------+

保存一對一的關(guān)系

現(xiàn)在來創(chuàng)建一個 photo,它的元信息將它們互相連接起來。

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";
import { PhotoMetadata } from "./entity/PhotoMetadata";

createConnection(/*...*/)
  .then(async (connection) => {
    // 創(chuàng)建 photo
    let photo = new Photo();
    photo.name = "Me and Bears";
    photo.description = "I am near polar bears";
    photo.filename = "photo-with-bears.jpg";
    photo.isPublished = true;

    // 創(chuàng)建 photo metadata
    let metadata = new PhotoMetadata();
    metadata.height = 640;
    metadata.width = 480;
    metadata.compressed = true;
    metadata.comment = "cybershoot";
    metadata.orientation = "portait";
    metadata.photo = photo; // 聯(lián)接兩者

    // 獲取實(shí)體 repositories
    let photoRepository = connection.getRepository(Photo);
    let metadataRepository = connection.getRepository(PhotoMetadata);

    // 先保存photo
    await photoRepository.save(photo);

    // 然后保存photo的metadata
    await metadataRepository.save(metadata);

    // 完成
    console.log(
      "Metadata is saved, and relation between metadata and photo is created in the database too"
    );
  })
  .catch((error) => console.log(error));

反向關(guān)系

關(guān)系可以是單向的或雙向的。目前 PhotoMetadata 和 Photo 之間的關(guān)系是單向的。關(guān)系的所有者是 PhotoMetadata,而 Photo 對 PhotoMetadata 一無所知。這使得從 Photo 中訪問 PhotoMetadata 變得很復(fù)雜。要解決這個問題,我們應(yīng)該在 PhotoMetadata 和 Photo 之間建立雙向關(guān)系。讓我們來修改一下實(shí)體:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  OneToOne,
  JoinColumn,
} from "typeorm";
import { Photo } from "./Photo";

@Entity()
export class PhotoMetadata {
  /* ... other columns */

  @OneToOne((type) => Photo, (photo) => photo.metadata)
  @JoinColumn()
  photo: Photo;
}
import { Entity, Column, PrimaryGeneratedColumn, OneToOne } from "typeorm";
import { PhotoMetadata } from "./PhotoMetadata";

@Entity()
export class Photo {
  /* ... other columns */

  @OneToOne((type) => PhotoMetadata, (photoMetadata) => photoMetadata.photo)
  metadata: PhotoMetadata;
}

photo => photo.metadata是用來指定反向關(guān)系的名稱。Photo 類的元數(shù)據(jù)屬性是在 Photo 類中存儲 PhotoMetadata 的地方。你可以選擇簡單地將字符串傳遞給@OneToOne裝飾器,而不是傳遞返回 photo 屬性的函數(shù),例如"metadata"。這種函數(shù)類型的方法使我們的重構(gòu)更容易。

注意,我們應(yīng)該僅在關(guān)系的一側(cè)使用@JoinColumn裝飾器。你把這個裝飾者放在哪一方將是這段關(guān)系的擁有方。關(guān)系的擁有方包含數(shù)據(jù)庫中具有外鍵的列。

取出關(guān)系對象的數(shù)據(jù)

在一個查詢中加載 photo 及 photo metadata 有兩種方法。使用find *或使用QueryBuilder。我們先使用find *方法。 find *方法允許你使用FindOneOptions / FindManyOptions接口指定對象。

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";
import { PhotoMetadata } from "./entity/PhotoMetadata";

createConnection(/*...*/)
  .then(async (connection) => {
    /*...*/
    let photoRepository = connection.getRepository(Photo);
    let photos = await photoRepository.find({ relations: ["metadata"] });
  })
  .catch((error) => console.log(error));

photos 將包含來自數(shù)據(jù)庫的 photos 數(shù)組,每個 photo 將包含其 photo metadata。詳細(xì)了解本文檔中的查找選項(xiàng)。

使用查找選項(xiàng)很簡單,但是如果你需要更復(fù)雜的查詢,則應(yīng)該使用QueryBuilder。 QueryBuilder允許以更優(yōu)雅的方式使用更復(fù)雜的查詢:

import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";
import { PhotoMetadata } from "./entity/PhotoMetadata";

createConnection(/*...*/)
  .then(async (connection) => {
    /*...*/
    let photos = await connection
      .getRepository(Photo)
      .createQueryBuilder("photo")
      .innerJoinAndSelect("photo.metadata", "metadata")
      .getMany();
  })
  .catch((error) => console.log(error));

QueryBuilder允許創(chuàng)建和執(zhí)行幾乎任何復(fù)雜性的 SQL 查詢。使用QueryBuilder時,請考慮創(chuàng)建 SQL 查詢。在此示例中,"photo"和"metadata"是應(yīng)用于所選 photos 的 ?? 別名。你可以使用別名來訪問所選數(shù)據(jù)的列和屬性。

使用 cascades 自動保存相關(guān)對象

我們可以在關(guān)系中設(shè)置cascade選項(xiàng),這是就可以在保存其他對象的同時保存相關(guān)對象。讓我們更改一下的 photo 的@OneToOne裝飾器:

export class Photo {
  /// ... other columns

  @OneToOne((type) => PhotoMetadata, (metadata) => metadata.photo, {
    cascade: true,
  })
  metadata: PhotoMetadata;
}

使用cascade允許就不需要邊存 photo 邊存元數(shù)據(jù)對象。我們可以簡單地保存一個 photo 對象,由于使用了 cascade,metadata 也將自動保存。

createConnection(options)
  .then(async (connection) => {
    // 創(chuàng)建 photo 對象
    let photo = new Photo();
    photo.name = "Me and Bears";
    photo.description = "I am near polar bears";
    photo.filename = "photo-with-bears.jpg";
    photo.isPublished = true;

    // 創(chuàng)建 photo metadata 對象
    let metadata = new PhotoMetadata();
    metadata.height = 640;
    metadata.width = 480;
    metadata.compressed = true;
    metadata.comment = "cybershoot";
    metadata.orientation = "portait";

    photo.metadata = metadata; // this way we connect them

    // 獲取 repository
    let photoRepository = connection.getRepository(Photo);

    // 保存photo的同時保存metadata
    await photoRepository.save(photo);

    console.log("Photo is saved, photo metadata is saved too.");
  })
  .catch((error) => console.log(error));

創(chuàng)建多對一/一對多關(guān)系

讓我們創(chuàng)建一個多對一/一對多的關(guān)系。假設(shè)一個 photo 有一個 author,每個 author 都可以有多個 photos。首先讓我們創(chuàng)建一個Author類:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  OneToMany,
  JoinColumn,
} from "typeorm";
import { Photo } from "./Photo";

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany((type) => Photo, (photo) => photo.author) // note: we will create author property in the Photo class below
  photos: Photo[];
}

Author 包含反向關(guān)系。
OneToMany 總是反向的, 并且總是與 ManyToOne一起出現(xiàn)。

現(xiàn)在讓我們將關(guān)系的所有者方添加到 Photo 實(shí)體中:

import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from "typeorm";
import { PhotoMetadata } from "./PhotoMetadata";
import { Author } from "./Author";

@Entity()
export class Photo {
  /* ... other columns */

  @ManyToOne((type) => Author, (author) => author.photos)
  author: Author;
}

在多對一/一對多的關(guān)系中,擁有方總是多對一的。這意味著使用@ManyToOne的類將存儲相關(guān)對象的 id。
運(yùn)行應(yīng)用程序后,ORM 將創(chuàng)建author表:

+-------------+--------------+----------------------------+
|                          author                         |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| name        | varchar(255) |                            |
+-------------+--------------+----------------------------+

它還將修改photo表,添加新的author列并為其創(chuàng)建外鍵:

+-------------+--------------+----------------------------+
|                         photo                           |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| name        | varchar(255) |                            |
| description | varchar(255) |                            |
| filename    | varchar(255) |                            |
| isPublished | boolean      |                            |
| authorId    | int(11)      | FOREIGN KEY                |
+-------------+--------------+----------------------------+

創(chuàng)建多對多關(guān)系

假設(shè)一個 photo 可以放在多個 albums 中,每個 albums 可以包含多個 photo。讓我們創(chuàng)建一個Album類:

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToMany,
  JoinTable,
} from "typeorm";

@Entity()
export class Album {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @ManyToMany((type) => Photo, (photo) => photo.albums)
  @JoinTable()
  photos: Photo[];
}

@JoinTable需要指定這是關(guān)系的所有者方。

現(xiàn)在添加反向關(guān)系到Photo類:

export class Photo {
  /// ... other columns

  @ManyToMany((type) => Album, (album) => album.photos)
  albums: Album[];
}

運(yùn)行后,ORM 將創(chuàng)建album_photos_photo_albums_聯(lián)結(jié)表。

+-------------+--------------+----------------------------+
|                album_photos_photo_albums                |
+-------------+--------------+----------------------------+
| album_id    | int(11)      | PRIMARY KEY FOREIGN KEY    |
| photo_id    | int(11)      | PRIMARY KEY FOREIGN KEY    |
+-------------+--------------+----------------------------+

記得在 ORM 中使用 ConnectionOptions 注冊Album類:

const options: ConnectionOptions = {
  // ... other options
  entities: [Photo, PhotoMetadata, Author, Album],
};

現(xiàn)在讓我們將 albums 和 photos 插入我們的數(shù)據(jù)庫:

let connection = await createConnection(options);

// create a few albums
let album1 = new Album();
album1.name = "Bears";
await connection.manager.save(album1);

let album2 = new Album();
album2.name = "Me";
await connection.manager.save(album2);

// create a few photos
let photo = new Photo();
photo.name = "Me and Bears";
photo.description = "I am near polar bears";
photo.filename = "photo-with-bears.jpg";
photo.albums = [album1, album2];
await connection.manager.save(photo);

// now our photo is saved and albums are attached to it
// now lets load them:
const loadedPhoto = await connection
  .getRepository(Photo)
  .findOne(1, { relations: ["albums"] });

loadedPhoto 如下所示:

{
    id: 1,
    name: "Me and Bears",
    description: "I am near polar bears",
    filename: "photo-with-bears.jpg",
    albums: [{
        id: 1,
        name: "Bears"
    }, {
        id: 2,
        name: "Me"
    }]
}

使用 QueryBuilder

你可以使用 QueryBuilder 構(gòu)建幾乎任何復(fù)雜性的 SQL 查詢。例如,可以這樣做:

let photos = await connection
  .getRepository(Photo)
  .createQueryBuilder("photo") // first argument is an alias. Alias is what you are selecting - photos. You must specify it.
  .innerJoinAndSelect("photo.metadata", "metadata")
  .leftJoinAndSelect("photo.albums", "album")
  .where("photo.isPublished = true")
  .andWhere("(photo.name = :photoName OR photo.name = :bearName)")
  .orderBy("photo.id", "DESC")
  .skip(5)
  .take(10)
  .setParameters({ photoName: "My", bearName: "Mishka" })
  .getMany();

此查詢選擇所有 published 的 name 等于"My"或"Mishka"的 photos。它將從結(jié)果中的第 5 個(分頁偏移)開始,并且僅選擇 10 個結(jié)果(分頁限制)。得到的結(jié)果將按 ID 降序排序。photo 的 albums 將被 left-joined,其元數(shù)據(jù)將被 inner joined。

由于 QueryBuilder 的自由度更高,因此在項(xiàng)目中可能會大量的使用它。
更多關(guān)于 QueryBuilder 的信息,可查看。

示例

查看示例用法。

下面這些 repositories 可以幫助你快速開始:

擴(kuò)展

這幾個擴(kuò)展可以簡化 TypeORM 的使用,并將其與其他模塊集成:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容