Compare commits

..

No commits in common. "main" and "v0.1.0" have entirely different histories.
main ... v0.1.0

5 changed files with 39 additions and 155 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
# claude # claude
.claude/ CLAUDE.md
# python # python
__pycache__/ __pycache__/

View File

@ -8,39 +8,29 @@ helpers for the common paths, with a raw escape hatch for everything else.
`requirements.txt`: `requirements.txt`:
``` ```
mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.4 mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.0
``` ```
Direct: Direct:
```bash ```bash
pip install "mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.4" pip install "mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.0"
``` ```
Requires `motor` and `pymongo` (pulled transitively). Requires `motor` and `pymongo` (pulled transitively).
Drop the `@v0.1.4` suffix from the line above to install the latest unpinned.
## Usage ## Usage
**Object (preferred)** — one client per process: **Object (preferred)** — one client per process:
```python ```python
from mongo import MongoDB from mongo import Mongo
db = MongoDB(conn_string, database) # attach as bot.db / app.db db = Mongo(conn_string, database) # attach as bot.db / app.db
await db.connect() # optional: ping to fail-early on a bad URI
users = await db.get_documents("users", {"active": True}) users = await db.get_documents("users", {"active": True})
db.close() # on shutdown (sync) db.close() # on shutdown (sync)
# or with guaranteed cleanup:
async with MongoDB(conn_string, database) as db:
await db.get_documents("users", {"active": True})
``` ```
The class is `MongoDB`; **`Mongo` remains a back-compat alias** (`Mongo = MongoDB`), so
existing `Mongo(...)` call sites keep working unchanged.
**Module proxy (back-compat)** — arm once, then call bare: **Module proxy (back-compat)** — arm once, then call bare:
```python ```python
@ -52,26 +42,10 @@ users = await mongo.get_documents("users", {"active": True})
Both styles share one client. The proxy exists so legacy call sites keep working Both styles share one client. The proxy exists so legacy call sites keep working
after a one-line `init()`; new code should use the object. after a one-line `init()`; new code should use the object.
## Naming consistency with the datastore trio
mongo predates the `redis`/`psql`/`mysql` trio; this version makes it surface-consistent
**without breaking anything** (all additive, every old name preserved):
- class is **`MongoDB`** (with `Mongo` kept as an 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()` — old names still work, the trio-consistent names are now available
The one deliberate difference that remains: **mongo swallows** (see below) where the trio
is **fail-loud**. That's intentional — flipping it would break existing consumers' branch-
on-result control flow.
## Error contract ## Error contract
- **Wrapped methods** log-and-swallow exceptions and return a safe default - **Wrapped methods** log-and-swallow exceptions and return a safe default
(`False` / `[]` / `{}` / `0` / `None`). Branch on the result. (`connect()` is the (`False` / `[]` / `{}` / `0` / `None`). Branch on the result.
exception — it raises on a bad connection so you can fail-early.)
- **`db.collection(name)`** (or `db[name]`) returns the raw motor collection: - **`db.collection(name)`** (or `db[name]`) returns the raw motor collection:
full driver surface, no swallowing, **raises**. Use it for anything not wrapped full driver surface, no swallowing, **raises**. Use it for anything not wrapped
(`find_one_and_*` beyond what's exposed, change streams, complex bulk ops). (`find_one_and_*` beyond what's exposed, change streams, complex bulk ops).
@ -97,4 +71,4 @@ are included.
## Versioning ## Versioning
Releases are tagged `vX.Y.Z`. The install line above pins a release; drop the `@vX.Y.Z` suffix to install the latest unpinned. Pin deliberately for reproducible installs. Tagged `vX.Y.Z`. Pin the tag in `requirements.txt`; bump deliberately.

View File

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "mongo" name = "mongo"
version = "0.1.4" version = "0.1.0"
description = "async mongodb wrapper over motor with a raw escape hatch" description = "async mongodb wrapper over motor with a raw escape hatch"
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ dependencies = [

View File

@ -1,18 +1,14 @@
from .mongo import MongoDB, Mongo, init, instance from .mongo import Mongo, init, instance
__all__ = ["MongoDB", "Mongo", "init", "instance"] __all__ = ["Mongo", "init", "instance"]
def __getattr__(name: str): def __getattr__(name: str):
"""proxy bare package attribute access to the default instance (PEP 562) """proxy bare package attribute access to the default instance (PEP 562)
lets `import mongo; await mongo.get_documents(...)` work after init(). lets `import mongo; await mongo.get_documents(...)` work after init().
`from mongo import func` still won't see this (resolved before init). `from mongo import func` still won't see this (resolved before init)
note: the bare-proxy raw escape hatch is `mongo.collection(name)`, not
`mongo[name]` module-level subscripting isn't a thing in python, so the proxy
can only forward named attributes. on a Mongo instance both `db[name]` and
`db.collection(name)` work.
""" """
if not name.startswith("_") and hasattr(MongoDB, name): if not name.startswith("_") and hasattr(Mongo, name):
return getattr(instance(), name) return getattr(instance(), name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}") raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

View File

@ -2,19 +2,11 @@
async mongodb wrapper over motor async mongodb wrapper over motor
object (preferred), one client per process: object (preferred), one client per process:
from mongo import MongoDB from mongo import Mongo
bot.db = MongoDB(conn_string, database) bot.db = Mongo(conn_string, database)
await bot.db.connect() # optional: ping to fail-early
await bot.db.get_documents("users", {"active": True}) await bot.db.get_documents("users", {"active": True})
bot.db.close() # on shutdown (sync) bot.db.close() # on shutdown (sync)
# async with (guaranteed cleanup):
async with MongoDB(conn_string, database) as db:
await db.get_documents("users", {})
`Mongo` remains a back-compat alias of `MongoDB` existing `Mongo(...)` call sites keep
working unchanged.
module proxy (back-compat), arm once then call bare: module proxy (back-compat), arm once then call bare:
import mongo # not `from mongo import ...` import mongo # not `from mongo import ...`
mongo.init(conn_string, database) mongo.init(conn_string, database)
@ -24,15 +16,6 @@ errors:
wrapped methods log and swallow, returning a safe default wrapped methods log and swallow, returning a safe default
(False / [] / {} / 0 / None). .collection(name) / db[name] return (False / [] / {} / 0 / None). .collection(name) / db[name] return
the raw motor collection: full driver surface, raises, nothing swallowed. 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: notes:
- the proxy needs `import mongo`; `from mongo import func` resolves before init - the proxy needs `import mongo`; `from mongo import func` resolves before init
@ -44,34 +27,18 @@ import logging
from typing import Any, List, Optional from typing import Any, List, Optional
from pymongo import ReturnDocument from pymongo import ReturnDocument
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection, AsyncIOMotorDatabase from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class MongoDB: class Mongo:
"""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):
self._client = AsyncIOMotorClient(connection_string) self._client = AsyncIOMotorClient(connection_string)
self._db = self._client[database] self._db = self._client[database]
async def connect(self) -> "MongoDB":
"""validate the connection with a ping and return self
motor connects lazily, so this is optional call it to fail early on a bad
URI/credentials rather than on the first real op (parallel to the trio's
connect()). raises on a bad connection, unlike the swallowing wrapped methods.
"""
await self._client.admin.command("ping")
return self
async def __aenter__(self) -> "MongoDB":
return await self.connect()
async def __aexit__(self, exc_type, exc, tb) -> None:
self.close()
def __getitem__(self, collection: str) -> AsyncIOMotorCollection: def __getitem__(self, collection: str) -> AsyncIOMotorCollection:
"""raw collection access via subscript: bot.db['users'].aggregate(...)""" """raw collection access via subscript: bot.db['users'].aggregate(...)"""
return self._db[collection] return self._db[collection]
@ -81,7 +48,7 @@ class MongoDB:
return self._db[name] return self._db[name]
@property @property
def database(self) -> AsyncIOMotorDatabase: def database(self):
"""raw motor database handle""" """raw motor database handle"""
return self._db return self._db
@ -214,11 +181,7 @@ class MongoDB:
return [] return []
async def get_document_hashmap(self, collection: str, target: dict, key: str) -> dict: 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: try:
cursor = self._db[collection].find(target) cursor = self._db[collection].find(target)
return {doc[key]: doc async for doc in cursor if key in doc} return {doc[key]: doc async for doc in cursor if key in doc}
@ -229,11 +192,7 @@ class MongoDB:
async def get_document_fields( async def get_document_fields(
self, collection: str, target: dict, key: str, fields: Optional[dict] = None self, collection: str, target: dict, key: str, fields: Optional[dict] = None
) -> List: ) -> 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: try:
cursor = self._db[collection].find(target, fields) cursor = self._db[collection].find(target, fields)
return [doc[key] async for doc in cursor if key in doc] return [doc[key] async for doc in cursor if key in doc]
@ -249,18 +208,6 @@ class MongoDB:
log.exception(f"db.count_documents() on {collection}") log.exception(f"db.count_documents() on {collection}")
return 0 return 0
async def check_document_exists(self, collection: str, target: dict) -> bool:
"""return whether at least one document in collection matches target"""
try:
return await self._db[collection].count_documents(target, limit=1) > 0
except Exception:
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: async def count_value_in_array(self, collection: str, array_field: str, value: str) -> int:
"""count documents whose array field contains value""" """count documents whose array field contains value"""
try: try:
@ -324,11 +271,11 @@ class MongoDB:
# update # update
async def update_document( async def update_document(
self, collection: str, target: dict, document: dict, upsert: bool = False self, collection: str, target: dict, document: dict, do_upsert: bool = False
) -> bool: ) -> bool:
"""replace a whole document, optionally upserting""" """replace a whole document, optionally upserting"""
try: try:
response = await self._db[collection].replace_one(target, document, upsert=upsert) response = await self._db[collection].replace_one(target, document, upsert=do_upsert)
return bool(response.matched_count or response.upserted_id) return bool(response.matched_count or response.upserted_id)
except Exception: except Exception:
log.exception(f"db.update_document() on {collection}") log.exception(f"db.update_document() on {collection}")
@ -344,15 +291,10 @@ class MongoDB:
return 0 return 0
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
`$set` to the identical value leaves `modified_count=0`); use `matched_count`
so an idempotent no-op on an existing doc isn't 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})
return response.matched_count > 0 return response.modified_count > 0
except Exception: except Exception:
log.exception(f"db.update_document_field() on {collection}") log.exception(f"db.update_document_field() on {collection}")
return False return False
@ -360,15 +302,10 @@ class MongoDB:
async def update_document_operator( async def update_document_operator(
self, collection: str, target: dict, update_query: dict self, collection: str, target: dict, update_query: dict
) -> 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
(e.g. `$set` to an identical value) use `matched_count` so an idempotent
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)
return response.matched_count > 0 return response.modified_count > 0
except Exception: except Exception:
log.exception(f"db.update_document_operator() on {collection}") log.exception(f"db.update_document_operator() on {collection}")
return False return False
@ -398,15 +335,10 @@ class MongoDB:
async def document_push_array( async def document_push_array(
self, collection: str, target: dict, array: str, value: Any self, collection: str, target: dict, array: str, value: Any
) -> 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
update helpers); $push always mutates, so matched_count and modified_count
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}})
return response.matched_count > 0 return response.modified_count > 0
except Exception: except Exception:
log.exception(f"db.document_push_array() on {collection}") log.exception(f"db.document_push_array() on {collection}")
return False return False
@ -415,17 +347,13 @@ class MongoDB:
self, collection: str, target: dict, array: str, value: Any, self, collection: str, target: dict, array: str, value: Any,
field_to_set: str, set_value: Any, field_to_set: str, set_value: Any,
) -> 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
update helpers).
"""
try: try:
response = await self._db[collection].update_one( response = await self._db[collection].update_one(
target, target,
{"$push": {array: value}, "$set": {field_to_set: set_value}}, {"$push": {array: value}, "$set": {field_to_set: set_value}},
) )
return response.matched_count > 0 return response.modified_count > 0
except Exception: except Exception:
log.exception(f"db.document_push_and_set() on {collection}") log.exception(f"db.document_push_and_set() on {collection}")
return False return False
@ -433,29 +361,24 @@ class MongoDB:
async def document_pop_array( async def document_pop_array(
self, collection: str, target: dict, array: str, value: Any self, collection: str, target: dict, array: str, value: Any
) -> 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
was absent) use `matched_count` so a no-op pull on an existing doc isn't
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}})
return response.matched_count > 0 return response.modified_count > 0
except Exception: except Exception:
log.exception(f"db.document_pop_array() on {collection}") log.exception(f"db.document_pop_array() on {collection}")
return False return False
async def find_one_and_update( async def find_one_and_update(
self, collection: str, target: dict, update: dict, self, collection: str, target: dict, update: dict,
return_after: bool = True, upsert: bool = False, fields: Optional[dict] = None, return_after: bool = True, do_upsert: bool = False, fields: Optional[dict] = None,
) -> Optional[dict]: ) -> Optional[dict]:
"""atomically update one doc and return it (after-image by default)""" """atomically update one doc and return it (after-image by default)"""
try: try:
return await self._db[collection].find_one_and_update( return await self._db[collection].find_one_and_update(
target, update, target, update,
projection=fields, projection=fields,
upsert=upsert, upsert=do_upsert,
return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE, return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE,
) )
except Exception: except Exception:
@ -464,14 +387,14 @@ class MongoDB:
async def find_one_and_replace( async def find_one_and_replace(
self, collection: str, target: dict, document: dict, self, collection: str, target: dict, document: dict,
return_after: bool = True, upsert: bool = False, fields: Optional[dict] = None, return_after: bool = True, do_upsert: bool = False, fields: Optional[dict] = None,
) -> Optional[dict]: ) -> Optional[dict]:
"""atomically replace one doc and return it (after-image by default)""" """atomically replace one doc and return it (after-image by default)"""
try: try:
return await self._db[collection].find_one_and_replace( return await self._db[collection].find_one_and_replace(
target, document, target, document,
projection=fields, projection=fields,
upsert=upsert, upsert=do_upsert,
return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE, return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE,
) )
except Exception: except Exception:
@ -500,10 +423,6 @@ class MongoDB:
log.exception(f"db.delete_document() on {collection}") log.exception(f"db.delete_document() on {collection}")
return 0 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: async def delete_documents(self, collection: str, target: dict) -> int:
"""delete all matching documents, returning deleted count""" """delete all matching documents, returning deleted count"""
try: try:
@ -557,11 +476,6 @@ class MongoDB:
return False return False
# back-compat alias: the class was historically `Mongo`; existing `Mongo(...)` call
# sites keep working unchanged
Mongo = MongoDB
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# backwards-compat module proxy # backwards-compat module proxy
# - lets legacy call sites keep using `await mongo.get_documents(...)` # - lets legacy call sites keep using `await mongo.get_documents(...)`
@ -570,17 +484,17 @@ Mongo = MongoDB
# - does NOT work with `from mongo import func` (resolved at import, # - does NOT work with `from mongo import func` (resolved at import,
# before init runs) — switch those sites to `import mongo` # before init runs) — switch those sites to `import mongo`
_default: Optional[MongoDB] = None _default: Optional[Mongo] = None
def init(connection_string: str, database: str) -> MongoDB: def init(connection_string: str, database: str) -> Mongo:
"""arm the module-level default instance and return it""" """arm the module-level default instance and return it"""
global _default global _default
_default = MongoDB(connection_string, database) _default = Mongo(connection_string, database)
return _default return _default
def instance() -> MongoDB: def instance() -> Mongo:
"""return the default instance, raising if init() has not run""" """return the default instance, raising if init() has not run"""
if _default is None: if _default is None:
raise RuntimeError("mongo not initialized; call mongo.init(conn, db) first") raise RuntimeError("mongo not initialized; call mongo.init(conn, db) first")