diff --git a/README.md b/README.md index 7f9bda54..63577b2f 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/scripts/clean-migration-fk-idx.js b/scripts/clean-migration-fk-idx.js new file mode 100644 index 00000000..6743a7e6 --- /dev/null +++ b/scripts/clean-migration-fk-idx.js @@ -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."); diff --git a/src/database/data-source.ts b/src/database/data-source.ts index 655d6386..9de306d7 100644 --- a/src/database/data-source.ts +++ b/src/database/data-source.ts @@ -47,9 +47,7 @@ export const AppDataSource = new DataSource({ logging: true, // timezone: "Z", entities: - process.env.NODE_ENV !== "production" - ? ["src/entities/**/*.ts"] - : ["dist/entities/**/*{.ts,.js}"], + process.env.NODE_ENV !== "production" ? ["src/entities/*.ts"] : ["dist/entities/*{.ts,.js}"], migrations: process.env.NODE_ENV !== "production" ? ["src/migration/**/*.ts"]