Skip to content

Commit b8e505d

Browse files
Migrate to Beanie v2.0.0 and Replace Motor with PyMongo (Fixes #72) (#82)
* chore: upgrade Beanie to 2.0.0 and switch to pymongo's AsyncMongoClient * Update main.py --------- Co-authored-by: Nivedit Jain <nivedit@aikin.club>
1 parent ad91716 commit b8e505d

4 files changed

Lines changed: 17 additions & 25 deletions

File tree

api-server/app/main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from fastapi import FastAPI
77
from contextlib import asynccontextmanager
88
from dotenv import load_dotenv
9-
from motor.motor_asyncio import AsyncIOMotorClient
9+
from pymongo import AsyncMongoClient
1010
from fastapi.security import OAuth2PasswordBearer
1111

1212
# injecting singletons
@@ -39,8 +39,13 @@ async def lifespan(app: FastAPI):
3939
logger.info("server starting")
4040

4141
# initializing beanie
42-
client = AsyncIOMotorClient(os.getenv("MONGO_URI"))
43-
db = client[os.getenv("MONGO_DATABASE_NAME")]
42+
client = AsyncMongoClient(os.getenv("MONGO_URI"))
43+
44+
# Get the database name from environment variables.
45+
# If not set, use "default" to avoid errors when accessing client[db_name]
46+
# os.getenv can return None, which is not valid for dictionary-style access
47+
db_name = os.getenv("MONGO_DATABASE_NAME", "exosphere-api-server")
48+
db = client[db_name]
4449
await init_beanie(db, document_models=[User, Project, Satellite])
4550
logger.info("beanie dbs initialized")
4651

api-server/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
88
"bcrypt>=4.3.0",
9-
"beanie>=1.30.0",
9+
"beanie>=2.0.0",
1010
"docker-image-py>=0.1.13",
1111
"email-validator>=2.2.0",
1212
"fastapi>=0.115.14",

api-server/run.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
args = parser.parse_args()
1010

1111
load_dotenv()
12-
1312
def serve():
1413
mode = args.mode
1514
if mode == "development":

api-server/uv.lock

Lines changed: 8 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)