diff --git a/README.md b/README.md index f9280f5..5504fa4 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ helpers for the common paths, with a raw escape hatch for everything else. `requirements.txt`: ``` -mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.0 +mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.1 ``` Direct: ```bash -pip install "mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.0" +pip install "mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.1" ``` Requires `motor` and `pymongo` (pulled transitively). diff --git a/ledger.md b/ledger.md new file mode 100644 index 0000000..f436b0d --- /dev/null +++ b/ledger.md @@ -0,0 +1,20 @@ +# mongo — ledger + +## v0.1.1 + +- **fidelity fix (MEDIUM):** added `check_document_exists(collection, target) -> bool` + (`count_documents(target, limit=1) > 0`). It existed only in the talos origin and was + not carried over; the talos caller (`local.py:157`) relies on it. +- **param rename (breaking):** `update_document` / `find_one_and_update` / + `find_one_and_replace` now take `upsert=` (was `do_upsert=`), standardizing on the + motor name. Consumers passing `do_upsert=` must switch to `upsert=` on migration; the + talos caller already uses `upsert=` and is now correct as-is. +- README install line corrected to the `git+ssh` form (was `git+https`). +- `close()` is synchronous (motor's close is sync) — not awaited. +- `get_document_hashmap` / `get_document_fields` skip docs missing the key (was an + unguarded `doc[key]` that swallowed a KeyError to `{}`/`[]`). + +## v0.1.0 + +- initial: async MongoDB wrapper over motor; wrapped methods log-and-swallow to safe + defaults; raw `.collection()` / `db[name]` escape hatch raises. diff --git a/pyproject.toml b/pyproject.toml index 7975d6c..369f81d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "mongo" -version = "0.1.0" +version = "0.1.1" description = "async mongodb wrapper over motor with a raw escape hatch" requires-python = ">=3.10" dependencies = [ diff --git a/src/mongo/mongo.py b/src/mongo/mongo.py index 212aa7b..4a77873 100644 --- a/src/mongo/mongo.py +++ b/src/mongo/mongo.py @@ -208,6 +208,14 @@ class Mongo: log.exception(f"db.count_documents() on {collection}") 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 count_value_in_array(self, collection: str, array_field: str, value: str) -> int: """count documents whose array field contains value""" try: @@ -271,11 +279,11 @@ class Mongo: # update async def update_document( - self, collection: str, target: dict, document: dict, do_upsert: bool = False + self, collection: str, target: dict, document: dict, upsert: bool = False ) -> bool: """replace a whole document, optionally upserting""" try: - response = await self._db[collection].replace_one(target, document, upsert=do_upsert) + response = await self._db[collection].replace_one(target, document, upsert=upsert) return bool(response.matched_count or response.upserted_id) except Exception: log.exception(f"db.update_document() on {collection}") @@ -371,14 +379,14 @@ class Mongo: async def find_one_and_update( self, collection: str, target: dict, update: dict, - return_after: bool = True, do_upsert: bool = False, fields: Optional[dict] = None, + return_after: bool = True, upsert: bool = False, fields: Optional[dict] = None, ) -> Optional[dict]: """atomically update one doc and return it (after-image by default)""" try: return await self._db[collection].find_one_and_update( target, update, projection=fields, - upsert=do_upsert, + upsert=upsert, return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE, ) except Exception: @@ -387,14 +395,14 @@ class Mongo: async def find_one_and_replace( self, collection: str, target: dict, document: dict, - return_after: bool = True, do_upsert: bool = False, fields: Optional[dict] = None, + return_after: bool = True, upsert: bool = False, fields: Optional[dict] = None, ) -> Optional[dict]: """atomically replace one doc and return it (after-image by default)""" try: return await self._db[collection].find_one_and_replace( target, document, projection=fields, - upsert=do_upsert, + upsert=upsert, return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE, ) except Exception: