Skip to content

Commit fb9d098

Browse files
added db connection
1 parent 0d35cac commit fb9d098

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

Diff for: .env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL="mongodb://localhost:27017/logger"

Diff for: docker-compose.yml

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ services:
66
container_name: mongo
77
image: mongo
88
restart: always
9+
ports:
10+
- 27017:27017
11+
env_file:
12+
- .env
913
volumes:
1014
- ./data:/data/db
1115

Diff for: src/database/__init__.py

Whitespace-only changes.

Diff for: src/database/connection.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import Optional, Any, List
2+
3+
from beanie import init_beanie, PydanticObjectId
4+
from models.production_run import ProductionRun
5+
6+
from motor.motor_asyncio import AsyncIOMotorClient
7+
from pydantic import BaseSettings, BaseModel
8+
9+
10+
class Settings(BaseSettings):
11+
DATABASE_URL: Optional[str] = None
12+
SECRET_KEY: Optional[str] = "default"
13+
14+
async def initialize_database(self):
15+
client = AsyncIOMotorClient(self.DATABASE_URL)
16+
await init_beanie(database=client.get_default_database(),
17+
document_models=[ProductionRun])
18+
19+
class Config:
20+
env_file = ".env"
21+
22+
23+
class Database:
24+
def __init__(self, model):
25+
self.model = model
26+
27+
async def save(self, document):
28+
await document.create()
29+
return
30+
31+
async def get(self, id: PydanticObjectId) -> bool:
32+
doc = await self.model.get(id)
33+
if doc:
34+
return doc
35+
return False
36+
37+
async def get_all(self) -> List[Any]:
38+
docs = await self.model.find_all().to_list()
39+
return docs
40+
41+
async def update(self, id: PydanticObjectId, body: BaseModel) -> Any:
42+
doc_id = id
43+
des_body = body.dict()
44+
45+
des_body = {k: v for k, v in des_body.items() if v is not None}
46+
update_query = {"$set": {
47+
field: value for field, value in des_body.items()
48+
}}
49+
50+
doc = await self.get(doc_id)
51+
if not doc:
52+
return False
53+
await doc.update(update_query)
54+
return doc
55+
56+
async def delete(self, id: PydanticObjectId) -> bool:
57+
doc = await self.get(id)
58+
if not doc:
59+
return False
60+
await doc.delete()
61+
return True

Diff for: src/main.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
from fastapi import FastAPI
2+
from database.connection import Settings
23

34
from routes.production_run import production_run_router
45

56
import uvicorn
67

78
api = FastAPI(docs_url=None, redoc_url="/docs")
89

10+
settings = Settings()
11+
12+
api.include_router(production_run_router)
13+
14+
15+
@api.on_event("startup")
16+
async def init_db():
17+
await settings.initialize_database()
18+
919

1020
@api.get('/')
1121
async def home() -> dict:
@@ -14,8 +24,5 @@ async def home() -> dict:
1424
}
1525

1626

17-
api.include_router(production_run_router)
18-
19-
2027
if __name__ == '__main__':
2128
uvicorn.run("main:api", host="0.0.0.0", port=8000, reload=True)

Diff for: src/models/production_run.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from datetime import datetime
2+
from beanie import Document
23
from pydantic import BaseModel
34

45

5-
class ProductionRun(BaseModel):
6+
class ProductionRun(Document):
67
id: int
78
name: str
89
dt: datetime = None

0 commit comments

Comments
 (0)