|
|
@@ -1,49 +1,24 @@
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
async mongodb wrapper over motor
|
|
|
|
async mongodb wrapper over motor, with a raw escape hatch for anything not wrapped
|
|
|
|
|
|
|
|
|
|
|
|
object (preferred), one client per process:
|
|
|
|
|
|
|
|
from mongo import MongoDB
|
|
|
|
from mongo import MongoDB
|
|
|
|
bot.db = MongoDB(conn_string, database)
|
|
|
|
db = MongoDB(conn_string, database)
|
|
|
|
await bot.db.connect() # optional: ping to fail-early
|
|
|
|
await db.get_documents("users", {"active": True})
|
|
|
|
await bot.db.get_documents("users", {"active": True})
|
|
|
|
|
|
|
|
bot.db.close() # on shutdown (sync)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# async with (guaranteed cleanup):
|
|
|
|
`Mongo` is a back-compat alias of `MongoDB`. A module-proxy path also exists
|
|
|
|
async with MongoDB(conn_string, database) as db:
|
|
|
|
(`mongo.init(...)` then bare `mongo.func(...)`) - see README for both usage patterns.
|
|
|
|
await db.get_documents("users", {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
`Mongo` remains a back-compat alias of `MongoDB` — existing `Mongo(...)` call sites keep
|
|
|
|
errors: wrapped methods log and swallow, returning a safe default (False / [] / {} / 0 /
|
|
|
|
working unchanged.
|
|
|
|
None). .collection(name) / db[name] return the raw motor collection: full driver surface,
|
|
|
|
|
|
|
|
raises, nothing swallowed. mongo SWALLOWS by design, unlike the fail-loud redis/psql/mysql
|
|
|
|
module proxy (back-compat), arm once then call bare:
|
|
|
|
trio - kept so existing consumers' branch-on-result control flow doesn't break.
|
|
|
|
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.
|
|
|
|
|
|
|
|
NOTE: mongo SWALLOWS by design — this is the one deliberate difference from the
|
|
|
|
|
|
|
|
newer datastore trio (redis/psql/mysql), which is fail-loud. mongo's contract is
|
|
|
|
|
|
|
|
kept as-is 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
|
|
|
|
|
|
|
|
- find_one_and_update returns the after-image by default
|
|
|
|
|
|
|
|
- bulk_write takes caller-built pymongo ops (UpdateOne/DeleteOne/...)
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
from typing import Any, List, Optional
|
|
|
|
from typing import Any, List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
from pymongo import ReturnDocument
|
|
|
|
from pymongo import ReturnDocument
|
|
|
|
|
|
|
|
from pymongo.errors import BulkWriteError
|
|
|
|
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection, AsyncIOMotorDatabase
|
|
|
|
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection, AsyncIOMotorDatabase
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
@@ -53,23 +28,20 @@ class MongoDB:
|
|
|
|
"""async mongodb wrapper; one client per process, attach to bot as bot.db"""
|
|
|
|
"""async mongodb wrapper; one client per process, attach to bot as bot.db"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, connection_string: str, database: str):
|
|
|
|
def __init__(self, connection_string: str, database: str):
|
|
|
|
# motor builds the client eagerly here (no network I/O yet). a syntactically
|
|
|
|
|
|
|
|
# malformed URI (e.g. "mongodb://") makes the driver raise InvalidURI at
|
|
|
|
|
|
|
|
# construction, before connect() — a deploy-time value, so it surfaces at startup
|
|
|
|
|
|
|
|
# either way and there is nothing to clean up.
|
|
|
|
|
|
|
|
self._client = AsyncIOMotorClient(connection_string)
|
|
|
|
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":
|
|
|
|
async def connect(self) -> "MongoDB":
|
|
|
|
"""validate the connection with a ping and return self
|
|
|
|
"""validate the connection with a ping and return self; raises, unlike the
|
|
|
|
|
|
|
|
swallowing wrapped methods
|
|
|
|
|
|
|
|
|
|
|
|
motor connects lazily, so this is optional — call it to fail early on a bad
|
|
|
|
an instance is single-use: pymongo 4.x `close()` is irreversible and
|
|
|
|
URI/credentials rather than on the first real op (parallel to the trio's
|
|
|
|
connect() doesn't rebuild the client. construct a fresh MongoDB instead of
|
|
|
|
connect()). raises on a bad connection, unlike the swallowing wrapped methods.
|
|
|
|
reusing one after close()/`async with` exit.
|
|
|
|
|
|
|
|
|
|
|
|
an instance is single-use: pymongo 4.x `close()` is irreversible, and connect()
|
|
|
|
|
|
|
|
only pings (it does not rebuild the client). do not reuse an instance after
|
|
|
|
|
|
|
|
close()/`async with` exit — construct a fresh MongoDB.
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
await self._client.admin.command("ping")
|
|
|
|
await self._client.admin.command("ping")
|
|
|
|
return self
|
|
|
|
return self
|
|
|
@@ -78,10 +50,8 @@ class MongoDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self.connect()
|
|
|
|
return await self.connect()
|
|
|
|
except BaseException:
|
|
|
|
except BaseException:
|
|
|
|
# connect()/ping failing here would otherwise leak the motor client (built
|
|
|
|
# __aexit__ isn't called when __aenter__ raises; close here so the
|
|
|
|
# eagerly in __init__ with a background topology monitor + pool) — __aexit__
|
|
|
|
# client built in __init__ doesn't leak
|
|
|
|
# is not called when __aenter__ raises. close it before propagating so a
|
|
|
|
|
|
|
|
# retry loop against a flapping server doesn't accumulate live clients.
|
|
|
|
|
|
|
|
self.close()
|
|
|
|
self.close()
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
@@ -111,7 +81,7 @@ class MongoDB:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def close(self) -> None:
|
|
|
|
def close(self) -> None:
|
|
|
|
"""close the client pool on shutdown (sync — motor's close() is synchronous)"""
|
|
|
|
"""close the client pool on shutdown (sync - motor's close() is synchronous)"""
|
|
|
|
self._client.close()
|
|
|
|
self._client.close()
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
@@ -120,10 +90,8 @@ class MongoDB:
|
|
|
|
async def create_collection(self, collection: str, index=None) -> bool:
|
|
|
|
async def create_collection(self, collection: str, index=None) -> bool:
|
|
|
|
"""create a collection, optionally seeding ONE index
|
|
|
|
"""create a collection, optionally seeding ONE index
|
|
|
|
|
|
|
|
|
|
|
|
`index` is a single index key-spec (e.g. `[("a", 1), ("b", 1)]` builds one
|
|
|
|
`index` is a single key-spec (e.g. `[("a", 1), ("b", 1)]` builds one compound
|
|
|
|
compound index, NOT two). for multiple indexes call `create_index` per index.
|
|
|
|
index, not two) - call create_index per index for multiples
|
|
|
|
renamed from `indexes` in v0.1.6 to reflect that it seeds a single index; pass it
|
|
|
|
|
|
|
|
positionally, or update a keyword call site from `indexes=` to `index=`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
await self._db.create_collection(collection)
|
|
|
|
await self._db.create_collection(collection)
|
|
|
@@ -185,10 +153,19 @@ class MongoDB:
|
|
|
|
async def create_documents(
|
|
|
|
async def create_documents(
|
|
|
|
self, collection: str, documents: List[dict], ordered: bool = True
|
|
|
|
self, collection: str, documents: List[dict], ordered: bool = True
|
|
|
|
) -> int:
|
|
|
|
) -> 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:
|
|
|
|
try:
|
|
|
|
result = await self._db[collection].insert_many(documents, ordered=ordered)
|
|
|
|
result = await self._db[collection].insert_many(documents, ordered=ordered)
|
|
|
|
return len(result.inserted_ids)
|
|
|
|
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:
|
|
|
|
except Exception:
|
|
|
|
log.exception(f"db.create_documents() on {collection}")
|
|
|
|
log.exception(f"db.create_documents() on {collection}")
|
|
|
|
return 0
|
|
|
|
return 0
|
|
|
@@ -368,9 +345,9 @@ class MongoDB:
|
|
|
|
async def update_document_field(self, collection: str, target: dict, updates: dict) -> bool:
|
|
|
|
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 the write changed nothing (a
|
|
|
|
returns True when a document matched, even if `$set` to an identical value
|
|
|
|
`$set` to the identical value leaves `modified_count=0`); use `matched_count`
|
|
|
|
leaves `modified_count=0` - uses `matched_count` so an idempotent no-op isn't
|
|
|
|
so an idempotent no-op on an existing doc isn't misread as a failure.
|
|
|
|
misread as a failure.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = await self._db[collection].update_one(target, {"$set": updates})
|
|
|
|
response = await self._db[collection].update_one(target, {"$set": updates})
|
|
|
@@ -384,9 +361,8 @@ class MongoDB:
|
|
|
|
) -> bool:
|
|
|
|
) -> 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
|
|
|
|
returns True when a document matched, even if the operators changed nothing -
|
|
|
|
(e.g. `$set` to an identical value) — use `matched_count` so an idempotent
|
|
|
|
uses `matched_count` so an idempotent write isn't misread as a failure.
|
|
|
|
write on an existing doc isn't misread as a failure.
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = await self._db[collection].update_one(target, update_query)
|
|
|
|
response = await self._db[collection].update_one(target, update_query)
|
|
|
@@ -396,12 +372,17 @@ class MongoDB:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
async def upsert_document_field(self, collection: str, target: dict, updates: dict) -> bool:
|
|
|
|
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:
|
|
|
|
try:
|
|
|
|
response = await self._db[collection].update_one(
|
|
|
|
response = await self._db[collection].update_one(
|
|
|
|
target, {"$set": updates}, upsert=True
|
|
|
|
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:
|
|
|
|
except Exception:
|
|
|
|
log.exception(f"db.upsert_document_field() on {collection}")
|
|
|
|
log.exception(f"db.upsert_document_field() on {collection}")
|
|
|
|
return False
|
|
|
|
return False
|
|
|
@@ -422,9 +403,8 @@ class MongoDB:
|
|
|
|
) -> bool:
|
|
|
|
) -> bool:
|
|
|
|
"""$push a value onto an array field
|
|
|
|
"""$push a value onto an array field
|
|
|
|
|
|
|
|
|
|
|
|
returns True when a document matched (consistent with the other single-doc
|
|
|
|
returns True when a document matched; $push always mutates, so matched_count
|
|
|
|
update helpers); $push always mutates, so matched_count and modified_count
|
|
|
|
and modified_count agree here in practice.
|
|
|
|
agree here in practice.
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = await self._db[collection].update_one(target, {"$push": {array: value}})
|
|
|
|
response = await self._db[collection].update_one(target, {"$push": {array: value}})
|
|
|
@@ -439,8 +419,7 @@ class MongoDB:
|
|
|
|
) -> bool:
|
|
|
|
) -> 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 (consistent with the other single-doc
|
|
|
|
returns True when a document matched (matched_count, not modified_count).
|
|
|
|
update helpers).
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = await self._db[collection].update_one(
|
|
|
|
response = await self._db[collection].update_one(
|
|
|
@@ -457,9 +436,8 @@ class MongoDB:
|
|
|
|
) -> bool:
|
|
|
|
) -> bool:
|
|
|
|
"""$pull a value from an array field
|
|
|
|
"""$pull a value from an array field
|
|
|
|
|
|
|
|
|
|
|
|
returns True when a document matched, even if nothing was pulled (the value
|
|
|
|
returns True when a document matched, even if the value was absent and
|
|
|
|
was absent) — use `matched_count` so a no-op pull on an existing doc isn't
|
|
|
|
nothing was pulled - uses `matched_count`, not `modified_count`.
|
|
|
|
misread as a failure.
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = await self._db[collection].update_one(target, {"$pull": {array: value}})
|
|
|
|
response = await self._db[collection].update_one(target, {"$pull": {array: value}})
|
|
|
@@ -539,10 +517,9 @@ class MongoDB:
|
|
|
|
# bulk / checks
|
|
|
|
# bulk / checks
|
|
|
|
|
|
|
|
|
|
|
|
async def bulk_write(self, collection: str, operations: list, ordered: bool = True) -> dict:
|
|
|
|
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
|
|
|
|
from pymongo import UpdateOne
|
|
|
|
ops = [UpdateOne({'_id': i}, {'$set': {...}}, upsert=True) for i in ids]
|
|
|
|
ops = [UpdateOne({'_id': i}, {'$set': {...}}, upsert=True) for i in ids]
|
|
|
|
returns a summary dict of counts, or an empty dict on failure
|
|
|
|
returns a summary dict of counts, or an empty dict on failure
|
|
|
@@ -563,8 +540,7 @@ class MongoDB:
|
|
|
|
async def document_check_multi(
|
|
|
|
async def document_check_multi(
|
|
|
|
self, collection: str, target: dict, checks: List[str], value: str
|
|
|
|
self, collection: str, target: dict, checks: List[str], value: str
|
|
|
|
) -> bool:
|
|
|
|
) -> 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
|
|
|
|
target is a normal filter dict (merged into the query), not a bare _id
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@@ -585,12 +561,9 @@ Mongo = MongoDB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# backwards-compat module proxy
|
|
|
|
# backwards-compat module proxy: `import mongo; mongo.init(conn, db)` then
|
|
|
|
# - lets legacy call sites keep using `await mongo.get_documents(...)`
|
|
|
|
# bare `await mongo.func(...)`. needs `import mongo` - `from mongo import func`
|
|
|
|
# without an object, after a one-time `mongo.init(conn, db)`
|
|
|
|
# resolves at import time, before init() runs.
|
|
|
|
# - 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`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_default: Optional[MongoDB] = None
|
|
|
|
_default: Optional[MongoDB] = None
|
|
|
|
|
|
|
|
|
|
|
|