add read me

This commit is contained in:
mamoss 2025-11-12 23:28:16 +07:00
parent 92f4ccb30a
commit 37790d56f4
3 changed files with 146 additions and 4 deletions

109
README.md
View file

@ -1 +1,108 @@
# bma-ehr-organization # hrms-api-org
ระบบ API สำหรับจัดการข้อมูลบุคลากรและโครงสร้างองค์กร (HRMS)
---
## ✨ Features
- **RESTful API** (Express)
- **ฐานข้อมูล MySQL** (TypeORM)
- **Cronjob อัตโนมัติ** (node-cron)
- **WebSocket** สำหรับ real-time
- **RabbitMQ integration**
- **Swagger UI** สำหรับ API docs
- รองรับการ **import/export ข้อมูล**
---
## 🗂️ โครงสร้างโปรเจกต์หลัก
| Path | รายละเอียด |
| ----------------------------------- | ------------------------------------- |
| `src/controllers/` | API Controllers |
| `src/entities/` | Entity สำหรับ TypeORM |
| `src/services/` | Service logic (RabbitMQ, WebSocket) |
| `src/database/data-source.ts` | ตั้งค่าเชื่อมต่อฐานข้อมูล |
| `src/migration/` | ไฟล์ migration ของฐานข้อมูล |
| `static/` | ไฟล์ static และ config |
| `scripts/clean-migration-fk-idx.js` | สคริปต์ลบบรรทัด FK*/idx* ใน migration |
---
## 🚀 การใช้งานเบื้องต้น
1. **ติดตั้ง dependencies**
```sh
npm install
```
2. **สร้างไฟล์ `.env`**
- กำหนดค่าฐานข้อมูลและ API_KEY ตามต้องการ
3. **Build และ Start**
```sh
npm run build
npm start
```
4. **ดู API docs**
- เปิดที่ `http://localhost:3000/api-docs`
---
## 🛠️ คำสั่งที่สำคัญ (npm scripts)
- `npm run dev` : รันแบบ development (hot reload)
- `npm run build` : สร้างไฟล์สำหรับ production
- `npm run migration:generate` : สร้าง migration ใหม่ เช่น
```sh
npm run migration:generate src/migration/update_table_0811202s
```
- `npm run migration:run` : รัน migration
- `node scripts/clean-migration-fk-idx.js` : ลบบรรทัดที่มี `FK_` หรือ `idx_` ใน migration อัตโนมัติ (ควรรันหลัง gen migration ทุกครั้ง)
---
## 🐳 การ Build/Deploy ด้วย act (local GitHub Actions)
หากต้องการ build และ deploy ด้วย workflow release (เช่น ทดสอบ pipeline บนเครื่อง)
ให้ใช้คำสั่งนี้:
```sh
act workflow_dispatch -W .github/workflows/release.yaml --input IMAGE_VER=latest -s DOCKER_USER=admin -s DOCKER_PASS=FPTadmin2357 -s SSH_PASSWORD=FPTadmin2357
```
**อธิบาย option ที่ใช้:**
- `-W .github/workflows/release.yaml` : ระบุ workflow ที่จะรัน
- `--input IMAGE_VER=latest` : กำหนด tag ของ docker image (เช่น latest)
- `-s DOCKER_USER=...` : กำหนด secret สำหรับ docker registry
- `-s DOCKER_PASS=...` : กำหนด secret สำหรับ docker registry
- `-s SSH_PASSWORD=...` : กำหนด secret สำหรับ ssh deploy
> ⚠️ **หมายเหตุ:** สำหรับ production ห้ามใช้รหัสผ่านจริงใน public repo หรือแชร์ credentials
---
## ⚠️ หมายเหตุเกี่ยวกับ Migration
- หลังจากใช้ `npm run migration:generate` แล้ว **ต้องลบบรรทัดที่มี `FK_` หรือ `idx_` ออกจากไฟล์ migration ทุกครั้ง** (ทั้งในฟังก์ชัน up/down)
- สามารถใช้สคริปต์นี้ช่วยลบอัตโนมัติ:
```sh
node scripts/clean-migration-fk-idx.js
```
- สคริปต์นี้จะค้นหาและแทนที่บรรทัดที่มี `FK_` หรือ `idx_` ด้วย comment `// removed FK_/idx_ auto-cleanup` ในทุกไฟล์ migration
---
## 📄 License
Distributed under the ISC License.

View file

@ -0,0 +1,37 @@
// Script: scripts/clean-migration-fk-idx.js
// ลบบรรทัดที่มี FK_ หรือ idx_ ในไฟล์ migration ทั้งหมด และแทนที่ด้วย comment
const fs = require("fs");
const path = require("path");
const MIGRATION_DIR = path.join(__dirname, "../src/migration");
function processFile(filePath) {
const lines = fs.readFileSync(filePath, "utf8").split("\n");
let changed = false;
const newLines = lines.map((line) => {
if (/FK_|idx_/.test(line)) {
changed = true;
return " // removed FK_/idx_ auto-cleanup";
}
return line;
});
if (changed) {
fs.writeFileSync(filePath, newLines.join("\n"), "utf8");
console.log("Cleaned:", filePath);
}
}
function walk(dir) {
fs.readdirSync(dir).forEach((f) => {
const fullPath = path.join(dir, f);
if (fs.statSync(fullPath).isDirectory()) {
walk(fullPath);
} else if (f.endsWith(".ts")) {
processFile(fullPath);
}
});
}
walk(MIGRATION_DIR);
console.log("Migration FK_/idx_ cleanup complete.");

View file

@ -47,9 +47,7 @@ export const AppDataSource = new DataSource({
logging: true, logging: true,
// timezone: "Z", // timezone: "Z",
entities: entities:
process.env.NODE_ENV !== "production" process.env.NODE_ENV !== "production" ? ["src/entities/*.ts"] : ["dist/entities/*{.ts,.js}"],
? ["src/entities/**/*.ts"]
: ["dist/entities/**/*{.ts,.js}"],
migrations: migrations:
process.env.NODE_ENV !== "production" process.env.NODE_ENV !== "production"
? ["src/migration/**/*.ts"] ? ["src/migration/**/*.ts"]