# 🏅 Day 19 - ORM 與資料庫操作基礎
## TypeORM 簡介
TypeORM 是一個流行的 Node.js ORM(Object-Relational Mapping)框架,支援 TypeScript 和 JavaScript。它能幫助開發者透過物件操作的方式與資料庫交互,而不需要直接編寫繁瑣的 SQL 語句。
https://typeorm.io/
## TypeORM 定義資料庫表結構
### 舉例說明:
```js
const CreditPackage = new EntitySchema({
name: "CreditPackage", // 定義表的名稱(Entity 名稱)
tableName: "CREDIT_PACKAGE", // 對應資料庫中的表名
columns: {
id: {
primary: true, // 設定為主鍵
type: "uuid", // 主鍵使用 UUID 格式
generated: "uuid", // 自動生成 UUID
nullable: false // 不可為空
},
name: {
type: "varchar", // 欄位類型為 varchar
length: 50, // 最大長度為 50
nullable: false, // 不可為空
unique: true // 必須唯一
},
description: {
type: "text", // 欄位類型為 text
nullable: true // 可為空
},
price: {
type: "decimal", // 欄位類型為 decimal
precision: 10, // 數字最大 10 位數
scale: 2, // 小數點後 2 位
nullable: false // 不可為空
},
createdAt: {
type: "timestamp", // 欄位類型為時間戳記
default: () => "CURRENT_TIMESTAMP", // 預設值為當前時間
nullable: false // 不可為空
}
},
relations: {
users: {
type: "many-to-one", // 關聯類型為多對一
target: "User", // 關聯的目標表
joinColumn: true // 指定關聯欄位為外鍵
}
}
});
```
---
## 題目
請根據以下要求完成 Skill 的 EntitySchema 定義
Skill 表的結構:
1. id:主鍵,uuid 格式,必須自動生成且不可為空。
2. name:名稱,varchar(50),不可為空且唯一。
3. createdAt:建立時間,timestamp,必須自動填入。
回報區
---
| # | Discord | CodePen / 答案 |
| ---------------- | -------- |--|
| 1 | 馬德 | [CodePen](https://codepen.io/maywang/pen/GgKbxOK?editors=0010)
|2 | pja._. | [CodePen](https://codepen.io/PJA0103/pen/yyBdvgB?editors=0011)
| 3 | adengg | [CodePen](https://codepen.io/Osases/pen/bNbPMyj?editors=0012)
| 4 | helena | [CodePen](https://codepen.io/helena27/pen/raBEKVa)
| 5 | benson | [CodePen](https://codepen.io/DevilButler/pen/jENjpOW?editors=0010) |
| 6 | bian_yang_mofa | [CodePen](https://codepen.io/cssf998811/pen/bNbPjzB?editors=0010) |
|7|janetlai|[Codepen](https://codepen.io/eiddkqxz-the-builder/pen/jENjvGx)
|8|ZoeKang|[CodePen](https://codepen.io/byehywmx-the-animator/pen/JoPQmdK?editors=0010)
| 9 | poyi | [CodePen](https://codepen.io/poyi-the-flexboxer/pen/bNbPQYE?editors=0010) |
| 10 | Tau | [CodePen](https://codepen.io/Tau-Hsu/pen/LEPKXeX?editors=0010) |
| 11 | sian | [CodePen](https://codepen.io/uxitysjl-the-flexboxer/pen/MYgMZKX?editors=0010) |
| 12 | Tammy | [CodePen](https://codepen.io/Tammy_Tsai/pen/ByBgvBQ) |
| 13 | JanllyGuo | [CodePen](https://codepen.io/kedugaw/pen/OPLeeMe?editors=0010) |
| 14 | ss585801 | [CodePen](https://codepen.io/ss585801/pen/yyBxBde?editors=0010) |
| 15 | Heidi | [CodePen](https://codepen.io/Heidi-Hsiao/pen/bNbXBjZ?editors=0010)
| 16 | hananhpun | [CodePen](https://codepen.io/hannahpun/pen/EaYqGdz) |
| 17 | JC | [CodePen](https://codepen.io/lifetimingwhisper/pen/QwLeYEZ) |
| 18 | daffytseng | [CodePen](https://codepen.io/Daffy-Tseng/pen/YPzKYKg)|
| 19 | sui_hsilan | [CodePen](https://codepen.io/suihsilan/pen/myddyjJ?editors=0010) |
| 20 | hsin yu | [CodePen](https://codepen.io/tina2793778/pen/ogNNXVB) |
| 21 | HarryKuo | [CodePen](https://codepen.io/harry_kuo/pen/pvooJmm?editors=0010)|
| 22. | 地呱 | [Codepen](https://codepen.io/wiibxzdw-the-bold/pen/azbzBXo?editors=0010) |
| 23. | yutzu | [Codepen](https://codepen.io/dtafsrmf-the-lessful/pen/raNamjq?editors=0010) |
|24.|Lee|[CodePen](https://codepen.io/leemo-tseng/pen/ByaNbOx?editors=0010)|
| 25. | JerryOOO |[CodePen](https://codepen.io/fatiangel/pen/MYWyWGX?editors=0010)|
|26.|wuyuli_21403| [CodePen](https://codepen.io/Job-Wilhelm/pen/qEBjZzz) |
|27.|shiang| [CodePen](https://codepen.io/shiang29/pen/ByaYNKY?editors=0010) |
<!-- 可複製下方格式
| | | [CodePen]() |
-->
<!--
```解答
const Skill = new EntitySchema({
name: "Skill",
tableName: "SKILL",
columns: {
id: {
primary: true,
type: "uuid",
generated: "uuid",
nullable: false
},
name: {
type: "varchar",
length: 50,
nullable: false,
unique: true
},
createdAt: {
type: "timestamp",
createDate: true,
name: "created_at",
nullable: false
}
}
})
```
-->