FastAPI使用MongoDB

こちらを参考にしました。
FastAPI、React、MongoDBでの開発方法

在Ubuntu上安装库。

sudo pip3 install motor

程序

文件夹结构

$ tree
.
├── database.py
├── main.py
└── model.py
# ------------------------------------------------------------------
#	database.py
#
#					May/02/2022
# ------------------------------------------------------------------
from model import City

import sys
import	datetime
#
import motor.motor_asyncio
from pymongo import MongoClient
import pymongo

# ------------------------------------------------------------------
client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://localhost/?retryWrites=true&w=majority')
database = client.city
collection = database.tochigi

# ------------------------------------------------------------------
async def fetch_one_city(id):
	document = await collection.find_one({"id":id})
	return document
#
# ------------------------------------------------------------------
async def fetch_all_cities():
	cities = []
	cursor = collection.find({})
	async for document in cursor:
		cities.append(City(**document))
	return cities
#
# ------------------------------------------------------------------
async def create_city(city):
	document = city
	result = await collection.insert_one(document)
	return document
#
# ------------------------------------------------------------------
async def update_city(id, population):
	sys.stderr.write("*** update_city ***\n")
	date_mod = datetime.date.today()
	await collection.update_one({"id":id}, {"$set": {
		'population': population,
		'date_mod': '%s' % date_mod
		}})
	document = await collection.find_one({"id": id})
	return document
#
# ------------------------------------------------------------------
async def remove_city(id):
	await collection.delete_one({"id":id})
	return True
#
# ------------------------------------------------------------------
# ------------------------------------------------------------------
#	main.py
#
#						May/02/2022
# ------------------------------------------------------------------
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from database import (
	fetch_one_city,
	fetch_all_cities,
	create_city,
	update_city,
	remove_city,
)

from model import City

# ------------------------------------------------------------------
app = FastAPI()

origins = [
	'http://localhost:3000',
	'http://localhost',
	]

app.add_middleware(
	CORSMiddleware,
	allow_origins=origins,
	allow_credentials=True,
	allow_methods=["*"],
	allow_headers=["*"],
)

# ------------------------------------------------------------------
@app.get("/")
def read_root():
	return {"Saluton"}

# ------------------------------------------------------------------
@app.get("/api/city")
async def get_cities():
	response = await fetch_all_cities()
	return response

# ------------------------------------------------------------------
@app.get("/api/city/{id}", response_model=City)
async def get_city_by_id(id):
	response = await fetch_one_city(id)
	if response:
		return response
	raise HTTPException(404, f"there is no City item with this id {id}")

# ------------------------------------------------------------------
@app.post("/api/city", response_model=City)
async def post_city(city:City):
	response = await create_city(city.dict())
	if response:
		return response
	raise HTTPException(400, "Sometheng went wrong / Bad Request")

# ------------------------------------------------------------------
@app.put("/api/city/{id}/", response_model=City)
async def put_city(id:str, population:int):
	response = await update_city(id, population)
	if response:
		return response
	raise HTTPException(404, f"there is no City item with this id {id}")

# ------------------------------------------------------------------
@app.delete("/api/city/{id}")
async def delete_city(id):
	response = await remove_city(id)
	if response:
		return "Successfully deleted city item!"
	raise HTTPException(404, f"there is no city item with this id {id}")

# ------------------------------------------------------------------
# ------------------------------------------------------------------
#	model.py
#
#					May/02/2022
# ------------------------------------------------------------------
from pydantic import BaseModel

# ------------------------------------------------------------------
class City(BaseModel):
	id: str
	name: str
	population: int
	date_mod: str
# ------------------------------------------------------------------

启动服务器

uvicorn main:app --reload
image.png

测试脚本

http  http://localhost:8000/api/city < in01.json
http  http://localhost:8000/api/city < in02.json
http  http://localhost:8000/api/city < in03.json
http  http://localhost:8000/api/city < in04.json
{
	"id": "t0921",
	"name": "宇都宮",
	"population": "34569",
	"date_mod": "2009-6-7"
}
{
	"id": "t0922", 
	"name": "小山",
	"population": "17952",
	"date_mod": "2009-5-19"
}
{
 	"id": "t0923",
	"name": "佐野",
	"population": "26929",
	"date_mod": "2009-3-28"
}
{
	"id": "t0924",
	"name": "足利",
	"population": "25197",
	"date_mod": "2009-12-21"
}

用mongosh确认执行结果。

city> show collections
tochigi
city> db.tochigi.find()
[
  {
    _id: ObjectId("626fb6cb78ddd6a16c5f7eca"),
    id: 't0921',
    name: '宇都宮',
    population: 34569,
    date_mod: '2009-6-7'
  },
  {
    _id: ObjectId("626fb6cb78ddd6a16c5f7ecb"),
    id: 't0922',
    name: '小山',
    population: 17952,
    date_mod: '2009-5-19'
  },
  {
    _id: ObjectId("626fb6cc78ddd6a16c5f7ecc"),
    id: 't0923',
    name: '佐野',
    population: 26929,
    date_mod: '2009-3-28'
  },
  {
    _id: ObjectId("626fb6cd78ddd6a16c5f7ecd"),
    id: 't0924',
    name: '足利',
    population: 25197,
    date_mod: '2009-12-21'
  }
]
city> 

获取所有的数据

http http://localhost:8000/api/city

通过指定ID来获取数据。

http http://localhost:8000/api/city/t0923

数据更新

http PUT http://localhost:8000/api/city/t0923/ population==42100

删除数据

http DELETE http://localhost:8000/api/city/t0922
广告
将在 10 秒后关闭
bannerAds