ข้ามไปยังเนื้อหา

โครงสร้างโปรเจกต์

โปรเจกต์ NestJS หรือ Express มี convention ของตัวเอง FastAPI ไม่ได้บังคับโครงสร้าง แต่ community ได้ลงตัวกับรูปแบบที่ดูคุ้นเคย

TypeScript
# โปรเจกต์ Express / NestJS
my-api/
├── src/
│ ├── main.ts # bootstrap
│ ├── app.module.ts # root module (NestJS)
│ ├── items/
│ │ ├── items.controller.ts
│ │ ├── items.service.ts
│ │ └── items.dto.ts
│ └── database/
│ └── database.module.ts
├── test/
├── package.json
└── tsconfig.json
Python
# โปรเจกต์ FastAPI
my-api/
├── app/
│ ├── main.py # FastAPI() instance + startup
│ ├── routers/
│ │ └── items.py # APIRouter — เหมือน controller
│ ├── models/
│ │ └── item.py # SQLAlchemy ORM models
│ ├── schemas/
│ │ └── item.py # Pydantic schemas (DTOs)
│ ├── dependencies.py # shared Depends() factories
│ └── database.py # engine + Session
├── tests/
├── pyproject.toml # เหมือน package.json
└── .env

การ mapping แทบจะ 1:1:

  • controllerrouters/ (ใช้ APIRouter)
  • service → เพิ่ม services/ layer ได้ แต่ FastAPI ไม่บังคับ
  • dtoschemas/ (Pydantic models)
  • entitymodels/ (SQLAlchemy models)
  • app.module.tsmain.py
TypeScript
// Express src/main.ts
import express from 'express';
import { itemsRouter } from './items/items.router';
const app = express();
app.use(express.json());
app.use('/items', itemsRouter);
app.listen(3000, () => console.log('listening on 3000'));
Python
# app/main.py
from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.routers import items
# Startup/shutdown via lifespan: code before yield runs at startup,
# code after yield runs at shutdown (e.g. open/close a DB pool).
@asynccontextmanager
async def lifespan(app: FastAPI):
print("Server is starting up")
yield
print("Server is shutting down")
app = FastAPI(title="Items API", version="1.0.0", lifespan=lifespan)
app.include_router(items.router, prefix="/items", tags=["items"])
# รัน: uvicorn app.main:app --reload
TypeScript
// package.json
{
"name": "my-api",
"version": "1.0.0",
"scripts": {
"start": "node dist/main.js",
"dev": "ts-node-dev src/main.ts",
"build": "tsc"
},
"dependencies": {
"express": "^4.18.0",
"zod": "^3.22.0"
},
"devDependencies": {
"typescript": "^5.0.0",
"@types/express": "^4.17.0"
}
}
Python
# pyproject.toml (PEP 517/518 — ใช้กับ pip / poetry / uv)
[project]
name = "my-api"
version = "1.0.0"
requires-python = ">=3.11"
dependencies = [
"fastapi>=0.115",
"uvicorn[standard]>=0.29",
"pydantic>=2.0",
"sqlalchemy>=2.0",
]
[project.optional-dependencies]
dev = ["pytest", "httpx", "mypy"]
[tool.uvicorn]
# เริ่ม: uvicorn app.main:app --reload
# หรือ: python -m uvicorn app.main:app --reload

รันที่เครื่องตัวเอง — ต้องการ FastAPI + server สร้างโครงสร้างด้านบน, ติดตั้ง dependencies ด้วย pip install -e ".[dev]" หรือ uv sync แล้วรัน uvicorn app.main:app --reload

ใน FastAPI project, Pydantic schemas (รูปร่าง request/response) นิยมวางไว้ในไดเรกทอรีใด?
อะไรคือ FastAPI equivalent ของการลงทะเบียน NestJS module ใน app.module.ts?
ไฟล์ใดในโปรเจกต์ Python ยุคใหม่ทำหน้าที่เหมือน package.json?