File tree Expand file tree Collapse file tree 5 files changed +21
-29
lines changed
Expand file tree Collapse file tree 5 files changed +21
-29
lines changed Original file line number Diff line number Diff line change 1- from fastapi import APIRouter
1+ from fastapi import APIRouter , Request
22
3- from jsoned .database import schemas_collection
43from jsoned .models .schema_definition import SchemaDefinition
54
65router = APIRouter ()
76
87
98@router .get ("/all" , response_model = list [SchemaDefinition ])
10- async def get_all_schemas ():
11- """
12- Gets all the schemas present in the database and returns them in a list of `SchemaDefinition` models.
13- """
14- response = []
15- documents = schemas_collection .find ({})
16- async for doc in documents :
17- response .append (SchemaDefinition (** doc ))
18-
19- return response
9+ async def get_all_schemas (request : Request ):
10+ schemas_collection = request .app .state .schemas_collection
11+ docs = schemas_collection .find ({})
12+ return [SchemaDefinition (** doc ) async for doc in docs ]
2013
2114
2215# @router.post("/add", response_model=SchemaDefinition)
Original file line number Diff line number Diff line change 1- from motor .motor_asyncio import AsyncIOMotorClient
1+ from fastapi import HTTPException
2+ from motor .motor_asyncio import AsyncIOMotorClient , AsyncIOMotorCollection
23
34from jsoned .settings import settings
45
56client : AsyncIOMotorClient | None = None
67database = None
7- schemas_collection = None
8+ _schemas_collection : AsyncIOMotorCollection | None = None
89
910
1011async def connect_to_mongo ():
11- global client , database , schemas_collection
12-
12+ global client , database , _schemas_collection
1313 client = AsyncIOMotorClient (settings .MONGO_URI )
1414 database = client .jsoned_db
15- schemas_collection = database .schemas
16-
17- # create index once on startup
18- await schemas_collection .create_index ("title" , unique = True )
15+ _schemas_collection = database .schemas
16+ await _schemas_collection .create_index ("title" , unique = True )
1917
2018
2119async def close_mongo ():
2220 if client is not None :
2321 client .close ()
22+
23+
24+ def get_schemas_collection () -> AsyncIOMotorCollection :
25+ if _schemas_collection is None :
26+ raise HTTPException (503 , "Database not available" )
27+ return _schemas_collection
Original file line number Diff line number Diff line change 33from fastapi import FastAPI
44from starlette .middleware .cors import CORSMiddleware
55
6- from jsoned .api .api_v1 .api import api_router
7- from jsoned .database import close_mongo , connect_to_mongo
6+ from jsoned .api .v1 .api import api_router
7+ from jsoned .database import close_mongo , connect_to_mongo , get_schemas_collection
88from jsoned .settings import settings
99
1010
1111@asynccontextmanager
1212async def app_init (app : FastAPI ):
1313 await connect_to_mongo ()
14+ app .state .schemas_collection = get_schemas_collection ()
1415
1516 app .include_router (api_router , prefix = settings .API_V1_STR )
1617 yield
17-
1818 await close_mongo ()
1919
2020
Original file line number Diff line number Diff line change 55
66
77class SchemaDefinition (BaseModel ):
8- id : UUID = Field (
9- ...,
10- description = "Automatically generated unique identifier for the schema entry based on `content`." ,
11- )
12-
138 title : str | None = Field (
149 None ,
1510 description = "A human-readable title given to the schema entry." ,
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ class Settings(BaseSettings):
99 "http://localhost" ,
1010 "http://localhost:3000" ,
1111 ]
12- MONGO_URI : AnyHttpUrl = "mongodb://localhost:27017"
12+ MONGO_URI : str = "mongodb://localhost:27017"
1313
1414
1515settings = Settings ()
You can’t perform that action at this time.
0 commit comments