|
|
|
@@ -2,20 +2,35 @@
|
|
|
|
|
async mongodb wrapper over motor
|
|
|
|
|
|
|
|
|
|
object (preferred), one client per process:
|
|
|
|
|
from mongo import Mongo
|
|
|
|
|
bot.db = Mongo(conn_string, database)
|
|
|
|
|
from mongo import MongoDB
|
|
|
|
|
bot.db = MongoDB(conn_string, database)
|
|
|
|
|
await bot.db.connect() # optional: ping to fail-early
|
|
|
|
|
await bot.db.get_documents("users", {"active": True})
|
|
|
|
|
bot.db.close() # on shutdown (sync)
|
|
|
|
|
|
|
|
|
|
# async with (guaranteed cleanup):
|
|
|
|
|
async with MongoDB(conn_string, database) as db:
|
|
|
|
|
await db.get_documents("users", {})
|
|
|
|
|
|
|
|
|
|
`Mongo` is a back-compat alias of `MongoDB`; old `Mongo(...)` call sites still work.
|
|
|
|
|
|
|
|
|
|
module proxy (back-compat), arm once then call bare:
|
|
|
|
|
import mongo # not `from mongo import ...`
|
|
|
|
|
mongo.init(conn_string, database)
|
|
|
|
|
await mongo.get_documents("users", {"active": True})
|
|
|
|
|
|
|
|
|
|
errors:
|
|
|
|
|
wrapped methods log and swallow, returning a safe default
|
|
|
|
|
(False / [] / {} / 0 / None). .collection(name) / db[name] return
|
|
|
|
|
the raw motor collection: full driver surface, raises, nothing swallowed.
|
|
|
|
|
wrapped methods log and swallow, returning a safe default (False / [] / {} / 0 /
|
|
|
|
|
None). .collection(name) / db[name] return the raw motor collection: full driver
|
|
|
|
|
surface, raises, nothing swallowed. mongo SWALLOWS by design — the one deliberate
|
|
|
|
|
difference from the fail-loud redis/psql/mysql trio, kept so existing consumers'
|
|
|
|
|
branch-on-result control flow doesn't break.
|
|
|
|
|
|
|
|
|
|
naming consistency with the trio (all additive — old names still work):
|
|
|
|
|
- class is `MongoDB` (was `Mongo`, kept as alias)
|
|
|
|
|
- `connect()` / `async with` like the trio (motor connects lazily, so connect()
|
|
|
|
|
just pings to validate early)
|
|
|
|
|
- `exists()` aliases `check_document_exists()`; `delete()` aliases `delete_document()`
|
|
|
|
|
|
|
|
|
|
notes:
|
|
|
|
|
- the proxy needs `import mongo`; `from mongo import func` resolves before init
|
|
|
|
@@ -27,17 +42,52 @@ import logging
|
|
|
|
|
from typing import Any, List, Optional
|
|
|
|
|
|
|
|
|
|
from pymongo import ReturnDocument
|
|
|
|
|
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection
|
|
|
|
|
from pymongo.errors import BulkWriteError
|
|
|
|
|
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection, AsyncIOMotorDatabase
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Mongo:
|
|
|
|
|
class MongoDB:
|
|
|
|
|
"""async mongodb wrapper; one client per process, attach to bot as bot.db"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, connection_string: str, database: str):
|
|
|
|
|
# motor builds the client eagerly (no I/O yet); a bad URI raises InvalidURI
|
|
|
|
|
# here with nothing built. an invalid db name raises below, after the client
|
|
|
|
|
# exists — close it before propagating so it doesn't outlive the raise.
|
|
|
|
|
self._client = AsyncIOMotorClient(connection_string)
|
|
|
|
|
self._db = self._client[database]
|
|
|
|
|
try:
|
|
|
|
|
self._db = self._client[database]
|
|
|
|
|
except BaseException:
|
|
|
|
|
self._client.close()
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
async def connect(self) -> "MongoDB":
|
|
|
|
|
"""validate the connection with a ping and return self
|
|
|
|
|
|
|
|
|
|
optional — motor connects lazily, so this just fails early on a bad
|
|
|
|
|
URI/credentials (parallel to the trio's connect()). raises, unlike the
|
|
|
|
|
swallowing wrapped methods.
|
|
|
|
|
|
|
|
|
|
an instance is single-use: pymongo 4.x `close()` is irreversible and
|
|
|
|
|
connect() doesn't rebuild the client. construct a fresh MongoDB instead of
|
|
|
|
|
reusing one after close()/`async with` exit.
|
|
|
|
|
"""
|
|
|
|
|
await self._client.admin.command("ping")
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
async def __aenter__(self) -> "MongoDB":
|
|
|
|
|
try:
|
|
|
|
|
return await self.connect()
|
|
|
|
|
except BaseException:
|
|
|
|
|
# __aexit__ is not called when __aenter__ raises, so a failed ping here
|
|
|
|
|
# would otherwise leak the motor client built in __init__. close it
|
|
|
|
|
# before propagating.
|
|
|
|
|
self.close()
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
async def __aexit__(self, exc_type, exc, tb) -> None:
|
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, collection: str) -> AsyncIOMotorCollection:
|
|
|
|
|
"""raw collection access via subscript: bot.db['users'].aggregate(...)"""
|
|
|
|
@@ -48,7 +98,7 @@ class Mongo:
|
|
|
|
|
return self._db[name]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def database(self):
|
|
|
|
|
def database(self) -> AsyncIOMotorDatabase:
|
|
|
|
|
"""raw motor database handle"""
|
|
|
|
|
return self._db
|
|
|
|
|
|
|
|
|
@@ -68,13 +118,17 @@ class Mongo:
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
# collection / index management
|
|
|
|
|
|
|
|
|
|
async def create_collection(self, collection: str, indexes=None) -> bool:
|
|
|
|
|
"""create a collection, optionally seeding an index"""
|
|
|
|
|
async def create_collection(self, collection: str, index=None) -> bool:
|
|
|
|
|
"""create a collection, optionally seeding ONE index
|
|
|
|
|
|
|
|
|
|
`index` is a single index key-spec (e.g. `[("a", 1), ("b", 1)]` builds one
|
|
|
|
|
compound index, NOT two); call `create_index` per index for multiples.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
await self._db.create_collection(collection)
|
|
|
|
|
if indexes:
|
|
|
|
|
await self._db[collection].create_index(indexes)
|
|
|
|
|
log.info(f"created indexes for {collection}")
|
|
|
|
|
if index:
|
|
|
|
|
await self._db[collection].create_index(index)
|
|
|
|
|
log.info(f"created index for {collection}")
|
|
|
|
|
return True
|
|
|
|
|
except Exception:
|
|
|
|
|
log.exception(f"db.create_collection() for {collection}")
|
|
|
|
@@ -130,10 +184,19 @@ class Mongo:
|
|
|
|
|
async def create_documents(
|
|
|
|
|
self, collection: str, documents: List[dict], ordered: bool = True
|
|
|
|
|
) -> int:
|
|
|
|
|
"""insert many documents, returning the inserted count"""
|
|
|
|
|
"""insert many documents, returning the inserted count
|
|
|
|
|
|
|
|
|
|
on a partial failure (e.g. ordered=False with one duplicate key) the driver
|
|
|
|
|
raises BulkWriteError AFTER inserting the rest; count comes from
|
|
|
|
|
`e.details['nInserted']` so a caller retrying on a wrongly-reported 0 doesn't
|
|
|
|
|
re-insert documents that already landed.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
result = await self._db[collection].insert_many(documents, ordered=ordered)
|
|
|
|
|
return len(result.inserted_ids)
|
|
|
|
|
except BulkWriteError as e:
|
|
|
|
|
log.exception(f"db.create_documents() on {collection}")
|
|
|
|
|
return e.details.get("nInserted", 0)
|
|
|
|
|
except Exception:
|
|
|
|
|
log.exception(f"db.create_documents() on {collection}")
|
|
|
|
|
return 0
|
|
|
|
@@ -181,7 +244,11 @@ class Mongo:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
async def get_document_hashmap(self, collection: str, target: dict, key: str) -> dict:
|
|
|
|
|
"""return matching documents keyed into a dict by the given field"""
|
|
|
|
|
"""return matching documents keyed into a dict by the given field
|
|
|
|
|
|
|
|
|
|
documents missing `key` are skipped (not in the result); a later document
|
|
|
|
|
with a duplicate key value overwrites an earlier one.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
cursor = self._db[collection].find(target)
|
|
|
|
|
return {doc[key]: doc async for doc in cursor if key in doc}
|
|
|
|
@@ -192,7 +259,11 @@ class Mongo:
|
|
|
|
|
async def get_document_fields(
|
|
|
|
|
self, collection: str, target: dict, key: str, fields: Optional[dict] = None
|
|
|
|
|
) -> List:
|
|
|
|
|
"""return a flat list of one field's value across matching documents"""
|
|
|
|
|
"""return a flat list of one field's value across matching documents
|
|
|
|
|
|
|
|
|
|
documents missing `key` are skipped, so the list length may be smaller than
|
|
|
|
|
the match count.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
cursor = self._db[collection].find(target, fields)
|
|
|
|
|
return [doc[key] async for doc in cursor if key in doc]
|
|
|
|
@@ -216,6 +287,10 @@ class Mongo:
|
|
|
|
|
log.exception(f"db.check_document_exists() on {collection}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
async def exists(self, collection: str, target: dict) -> bool:
|
|
|
|
|
"""trio-consistent alias of check_document_exists"""
|
|
|
|
|
return await self.check_document_exists(collection, target)
|
|
|
|
|
|
|
|
|
|
async def count_value_in_array(self, collection: str, array_field: str, value: str) -> int:
|
|
|
|
|
"""count documents whose array field contains value"""
|
|
|
|
|
try:
|
|
|
|
@@ -299,10 +374,15 @@ class Mongo:
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
async def update_document_field(self, collection: str, target: dict, updates: dict) -> bool:
|
|
|
|
|
"""$set one or more fields on a single document"""
|
|
|
|
|
"""$set one or more fields on a single document
|
|
|
|
|
|
|
|
|
|
returns True when a document matched, even if `$set` to an identical value
|
|
|
|
|
leaves `modified_count=0` — uses `matched_count` so an idempotent no-op isn't
|
|
|
|
|
misread as a failure.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
response = await self._db[collection].update_one(target, {"$set": updates})
|
|
|
|
|
return response.modified_count > 0
|
|
|
|
|
return response.matched_count > 0
|
|
|
|
|
except Exception:
|
|
|
|
|
log.exception(f"db.update_document_field() on {collection}")
|
|
|
|
|
return False
|
|
|
|
@@ -310,21 +390,30 @@ class Mongo:
|
|
|
|
|
async def update_document_operator(
|
|
|
|
|
self, collection: str, target: dict, update_query: dict
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""apply raw update operators ($set/$inc/$unset/...) to a single document"""
|
|
|
|
|
"""apply raw update operators ($set/$inc/$unset/...) to a single document
|
|
|
|
|
|
|
|
|
|
returns True when a document matched, even if the operators changed nothing —
|
|
|
|
|
uses `matched_count` so an idempotent write isn't misread as a failure.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
response = await self._db[collection].update_one(target, update_query)
|
|
|
|
|
return response.modified_count > 0
|
|
|
|
|
return response.matched_count > 0
|
|
|
|
|
except Exception:
|
|
|
|
|
log.exception(f"db.update_document_operator() on {collection}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
async def upsert_document_field(self, collection: str, target: dict, updates: dict) -> bool:
|
|
|
|
|
"""$set fields on a single document, creating it if absent"""
|
|
|
|
|
"""$set fields on a single document, creating it if absent
|
|
|
|
|
|
|
|
|
|
returns True when a document matched or was upserted, even if `$set` to an
|
|
|
|
|
identical value leaves `modified_count=0` — mirrors update_document_field's
|
|
|
|
|
matched_count-over-modified_count contract.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
response = await self._db[collection].update_one(
|
|
|
|
|
target, {"$set": updates}, upsert=True
|
|
|
|
|
)
|
|
|
|
|
return bool(response.modified_count or response.upserted_id)
|
|
|
|
|
return bool(response.matched_count or response.upserted_id)
|
|
|
|
|
except Exception:
|
|
|
|
|
log.exception(f"db.upsert_document_field() on {collection}")
|
|
|
|
|
return False
|
|
|
|
@@ -343,10 +432,14 @@ class Mongo:
|
|
|
|
|
async def document_push_array(
|
|
|
|
|
self, collection: str, target: dict, array: str, value: Any
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""$push a value onto an array field"""
|
|
|
|
|
"""$push a value onto an array field
|
|
|
|
|
|
|
|
|
|
returns True when a document matched; $push always mutates, so matched_count
|
|
|
|
|
and modified_count agree here in practice.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
response = await self._db[collection].update_one(target, {"$push": {array: value}})
|
|
|
|
|
return response.modified_count > 0
|
|
|
|
|
return response.matched_count > 0
|
|
|
|
|
except Exception:
|
|
|
|
|
log.exception(f"db.document_push_array() on {collection}")
|
|
|
|
|
return False
|
|
|
|
@@ -355,13 +448,16 @@ class Mongo:
|
|
|
|
|
self, collection: str, target: dict, array: str, value: Any,
|
|
|
|
|
field_to_set: str, set_value: Any,
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""$push to an array and $set a field in one update"""
|
|
|
|
|
"""$push to an array and $set a field in one update
|
|
|
|
|
|
|
|
|
|
returns True when a document matched (matched_count, not modified_count).
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
response = await self._db[collection].update_one(
|
|
|
|
|
target,
|
|
|
|
|
{"$push": {array: value}, "$set": {field_to_set: set_value}},
|
|
|
|
|
)
|
|
|
|
|
return response.modified_count > 0
|
|
|
|
|
return response.matched_count > 0
|
|
|
|
|
except Exception:
|
|
|
|
|
log.exception(f"db.document_push_and_set() on {collection}")
|
|
|
|
|
return False
|
|
|
|
@@ -369,10 +465,14 @@ class Mongo:
|
|
|
|
|
async def document_pop_array(
|
|
|
|
|
self, collection: str, target: dict, array: str, value: Any
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""$pull a value from an array field"""
|
|
|
|
|
"""$pull a value from an array field
|
|
|
|
|
|
|
|
|
|
returns True when a document matched, even if the value was absent and
|
|
|
|
|
nothing was pulled — uses `matched_count`, not `modified_count`.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
response = await self._db[collection].update_one(target, {"$pull": {array: value}})
|
|
|
|
|
return response.modified_count > 0
|
|
|
|
|
return response.matched_count > 0
|
|
|
|
|
except Exception:
|
|
|
|
|
log.exception(f"db.document_pop_array() on {collection}")
|
|
|
|
|
return False
|
|
|
|
@@ -431,6 +531,10 @@ class Mongo:
|
|
|
|
|
log.exception(f"db.delete_document() on {collection}")
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
async def delete(self, collection: str, target: dict) -> int:
|
|
|
|
|
"""trio-consistent alias of delete_document (single-document delete)"""
|
|
|
|
|
return await self.delete_document(collection, target)
|
|
|
|
|
|
|
|
|
|
async def delete_documents(self, collection: str, target: dict) -> int:
|
|
|
|
|
"""delete all matching documents, returning deleted count"""
|
|
|
|
|
try:
|
|
|
|
@@ -444,10 +548,9 @@ class Mongo:
|
|
|
|
|
# bulk / checks
|
|
|
|
|
|
|
|
|
|
async def bulk_write(self, collection: str, operations: list, ordered: bool = True) -> dict:
|
|
|
|
|
"""
|
|
|
|
|
run a batch of pymongo write ops (InsertOne/UpdateOne/DeleteOne/...)
|
|
|
|
|
"""run a batch of pymongo write ops (InsertOne/UpdateOne/DeleteOne/...)
|
|
|
|
|
|
|
|
|
|
caller builds the ops from pymongo, e.g.:
|
|
|
|
|
caller builds the ops, e.g.:
|
|
|
|
|
from pymongo import UpdateOne
|
|
|
|
|
ops = [UpdateOne({'_id': i}, {'$set': {...}}, upsert=True) for i in ids]
|
|
|
|
|
returns a summary dict of counts, or an empty dict on failure
|
|
|
|
@@ -468,8 +571,7 @@ class Mongo:
|
|
|
|
|
async def document_check_multi(
|
|
|
|
|
self, collection: str, target: dict, checks: List[str], value: str
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
return whether a doc matching target has value in any of the given arrays
|
|
|
|
|
"""return whether a doc matching target has value in any of the given arrays
|
|
|
|
|
|
|
|
|
|
target is a normal filter dict (merged into the query), not a bare _id
|
|
|
|
|
"""
|
|
|
|
@@ -484,25 +586,27 @@ class Mongo:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# back-compat alias: the class was historically `Mongo`; existing `Mongo(...)` call
|
|
|
|
|
# sites keep working unchanged
|
|
|
|
|
Mongo = MongoDB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
# backwards-compat module proxy
|
|
|
|
|
# - lets legacy call sites keep using `await mongo.get_documents(...)`
|
|
|
|
|
# without an object, after a one-time `mongo.init(conn, db)`
|
|
|
|
|
# - works with `import mongo; mongo.func(...)` (resolved at call time)
|
|
|
|
|
# - does NOT work with `from mongo import func` (resolved at import,
|
|
|
|
|
# before init runs) — switch those sites to `import mongo`
|
|
|
|
|
# backwards-compat module proxy: `import mongo; mongo.init(conn, db)` then
|
|
|
|
|
# bare `await mongo.func(...)`. needs `import mongo` — `from mongo import func`
|
|
|
|
|
# resolves at import time, before init() runs.
|
|
|
|
|
|
|
|
|
|
_default: Optional[Mongo] = None
|
|
|
|
|
_default: Optional[MongoDB] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init(connection_string: str, database: str) -> Mongo:
|
|
|
|
|
def init(connection_string: str, database: str) -> MongoDB:
|
|
|
|
|
"""arm the module-level default instance and return it"""
|
|
|
|
|
global _default
|
|
|
|
|
_default = Mongo(connection_string, database)
|
|
|
|
|
_default = MongoDB(connection_string, database)
|
|
|
|
|
return _default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def instance() -> Mongo:
|
|
|
|
|
def instance() -> MongoDB:
|
|
|
|
|
"""return the default instance, raising if init() has not run"""
|
|
|
|
|
if _default is None:
|
|
|
|
|
raise RuntimeError("mongo not initialized; call mongo.init(conn, db) first")
|
|
|
|
|