mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-11-02 21:24:34 +08:00
Implement MongoDB database adapter (#29)
* Implement MongoDB adapter using motor * Add mongo container to build pipeline * Tidy up dependencies * Update documentation for MongoDB * Export MongoDB adapter from db package * Pass black format * Update README
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
from fastapi_users.db.base import BaseUserDatabase # noqa: F401
|
||||
from fastapi_users.db.mongodb import MongoDBUserDatabase # noqa: F401
|
||||
from fastapi_users.db.sqlalchemy import ( # noqa: F401
|
||||
SQLAlchemyBaseUserTable,
|
||||
SQLAlchemyUserDatabase,
|
||||
|
||||
40
fastapi_users/db/mongodb.py
Normal file
40
fastapi_users/db/mongodb.py
Normal file
@ -0,0 +1,40 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from motor.motor_asyncio import AsyncIOMotorCollection
|
||||
|
||||
from fastapi_users.db.base import BaseUserDatabase
|
||||
from fastapi_users.models import BaseUserDB
|
||||
|
||||
|
||||
class MongoDBUserDatabase(BaseUserDatabase):
|
||||
"""
|
||||
Database adapter for MongoDB.
|
||||
|
||||
:param collection: Collection instance from `motor`.
|
||||
"""
|
||||
|
||||
collection: AsyncIOMotorCollection
|
||||
|
||||
def __init__(self, collection: AsyncIOMotorCollection):
|
||||
self.collection = collection
|
||||
self.collection.create_index("id", unique=True)
|
||||
self.collection.create_index("email", unique=True)
|
||||
|
||||
async def list(self) -> List[BaseUserDB]:
|
||||
return [BaseUserDB(**user) async for user in self.collection.find()]
|
||||
|
||||
async def get(self, id: str) -> Optional[BaseUserDB]:
|
||||
user = await self.collection.find_one({"id": id})
|
||||
return BaseUserDB(**user) if user else None
|
||||
|
||||
async def get_by_email(self, email: str) -> Optional[BaseUserDB]:
|
||||
user = await self.collection.find_one({"email": email})
|
||||
return BaseUserDB(**user) if user else None
|
||||
|
||||
async def create(self, user: BaseUserDB) -> BaseUserDB:
|
||||
await self.collection.insert_one(user.dict())
|
||||
return user
|
||||
|
||||
async def update(self, user: BaseUserDB) -> BaseUserDB:
|
||||
await self.collection.replace_one({"id": user.id}, user.dict())
|
||||
return user
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import List, cast
|
||||
from typing import List, Optional, cast
|
||||
|
||||
from databases import Database
|
||||
from sqlalchemy import Boolean, Column, String, Table
|
||||
@ -38,11 +38,11 @@ class SQLAlchemyUserDatabase(BaseUserDatabase):
|
||||
query = self.users.select()
|
||||
return cast(List[BaseUserDB], await self.database.fetch_all(query))
|
||||
|
||||
async def get(self, id: str) -> BaseUserDB:
|
||||
async def get(self, id: str) -> Optional[BaseUserDB]:
|
||||
query = self.users.select().where(self.users.c.id == id)
|
||||
return cast(BaseUserDB, await self.database.fetch_one(query))
|
||||
|
||||
async def get_by_email(self, email: str) -> BaseUserDB:
|
||||
async def get_by_email(self, email: str) -> Optional[BaseUserDB]:
|
||||
query = self.users.select().where(self.users.c.email == email)
|
||||
return cast(BaseUserDB, await self.database.fetch_one(query))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user