TypeORM - 查詢操作



資料操作用於管理和檢視資料。本節介紹如何使用 QueryBuilder 訪問資料庫查詢,例如插入、更新、選擇和刪除查詢。讓我們逐一詳細瞭解。

構建插入查詢

讓我們建立一個名為Customer的實體,如下所示:

Customer.ts

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm"; 
@Entity() 
export class Customer {       

   @PrimaryGeneratedColumn() 
   id: number; 
   
   @Column() 
   name: string; 
   
   @Column() 
   age: number; 
}

讓我們在 index.ts 中新增以下更改:

index.ts

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

createConnection().then(async connection => { 
   await getConnection().createQueryBuilder()   .insert() 
      .into(Customer)  
      .values([ { name: "Adam",age:11}, 
         { name: "David",age:12} ]) .execute(); 
}).catch(error => console.log(error));

現在,使用以下命令啟動您的應用程式:

npm start

輸出

您可以在螢幕上看到以下輸出:

Data Inserted

現在開啟您的 MySQL 伺服器,可以看到插入的表包含兩個欄位,如下所示:

Table Inserted

構建更新查詢

上一節,我們插入了兩行資料。讓我們檢查更新查詢是如何工作的。在 index.ts 中新增以下更改:

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

createConnection().then(async connection => { 

await getConnection()         
   .createQueryBuilder() .update(Customer) 
   .set({ name: "Michael" }) .where("id = :id", { id: 1 }) .execute(); 
   console.log("data updated"); 
   
}).catch(error => console.log(error));

現在,使用以下命令啟動您的應用程式:

npm start

您可以在螢幕上看到以下輸出:

Data Updated

MySQL 表修改如下所示:

Mysql Table

構建選擇查詢

select 查詢用於顯示錶中的記錄。讓我們在index.ts中新增以下程式碼:

index.ts

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

createConnection().then(async connection => { 

   console.log("Display records from Customer table..."); 
   const cus = new Customer();

   console.log("Loading customers from the database..."); 
   const customers = await connection.manager.find(Customer); console.log("Loaded users: ", customers); 
}).catch(error => console.log(error));

您可以在螢幕上看到以下輸出:

Output

where 表示式

讓我們在查詢中新增 where 表示式來過濾客戶。示例程式碼如下:

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

createConnection().then(async connection => { 
   const customer = await getConnection() .createQueryBuilder() .select("cus") 
   .from(Customer, "cus") .where("cus.id = :id", { id: 1 }) .getOne(); 
   
   console.log(customer); 
})
.catch(error => console.log(error));

上面的程式將返回第一個 id 的記錄。您可以在螢幕上看到以下輸出:

First Id Records

同樣,您也可以嘗試其他表示式。

構建刪除查詢

上一節,我們已經插入、更新和選擇資料。讓我們檢查刪除查詢是如何工作的。在 index.ts 中新增以下更改:

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

createConnection().then(async connection => { 
   await getConnection() .createQueryBuilder() 
   .delete() 
   .from(Customer) 
   .where("id = :id", { id: 1 }) .execute();
console.log("data deleted"); }).catch(error => console.log(error));

您可以在螢幕上看到以下輸出:

Output on Screen

您的 MySQL 表將修改如下:

Mysql Table is Modified
廣告