18 lines
470 B
Python
18 lines
470 B
Python
|
from pydantic import BaseModel
|
||
|
from pymongo import MongoClient
|
||
|
from typing import Generic, TypeVar
|
||
|
|
||
|
ModelType = TypeVar("ModelType", bound=BaseModel)
|
||
|
|
||
|
|
||
|
class MongoConnect:
|
||
|
def __init__(self, host="localhost", port: int = 27017, db: str = None):
|
||
|
self.host = host
|
||
|
self.port = port
|
||
|
self.db = db
|
||
|
self.client = MongoClient(host=self.host, port=self.port)
|
||
|
self.db = self.client[self.db]
|
||
|
|
||
|
def get_db(self):
|
||
|
yield self.db
|