File size: 1,298 Bytes
35dc52d
 
49e9f9d
35dc52d
49e9f9d
 
35dc52d
 
 
 
 
 
 
 
49e9f9d
 
 
 
 
 
 
35dc52d
 
 
 
 
 
 
49e9f9d
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from pymongo.errors import ConfigurationError

from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase

from ..core.config import settings

# Default DB name used when the connection string doesn't carry one.
# Atlas users routinely paste a URL of the form
# `mongodb+srv://.../?retryWrites=true&w=majority` (no `/dbname` segment),
# which makes `get_default_database()` raise. Falling back to this name
# matches what the local-dev URL uses (`mongodb://localhost:27017/neighbouraid`)
# and keeps deploys forgiving.
_DEFAULT_DB_NAME = "neighbouraid"

_client: AsyncIOMotorClient = None
_db: AsyncIOMotorDatabase = None


async def connect():
    global _client, _db
    _client = AsyncIOMotorClient(settings.MONGO_URL)
    try:
        _db = _client.get_default_database()
    except ConfigurationError:
        # Atlas SRV strings often omit the database segment. Picking up
        # `neighbouraid` here keeps the user from having to learn the
        # exact connection-string syntax just to deploy.
        _db = _client[_DEFAULT_DB_NAME]
    await _db.alerts.create_index([("location", "2dsphere")])
    await _db.users.create_index("email", unique=True)


async def disconnect():
    if _client:
        _client.close()


def get_db() -> AsyncIOMotorDatabase:
    return _db