fix: add check_document_exists; rename do_upsert -> upsert

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, but the talos caller relies on it. renamed the do_upsert param to upsert on update_document/find_one_and_update/find_one_and_replace, standardizing on motor's name (the talos caller already passes upsert=). param rename is breaking; patch is fine under the no-consumer policy. bump to v0.1.1.

Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
disqualifier 2026-06-29 03:18:13 -04:00
parent eabfe0cbc3
commit e5e0cebbcd
4 changed files with 37 additions and 9 deletions

View File

@ -8,13 +8,13 @@ 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.0 mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.1
``` ```
Direct: Direct:
```bash ```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). Requires `motor` and `pymongo` (pulled transitively).

20
ledger.md Normal file
View File

@ -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.

View File

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "mongo" name = "mongo"
version = "0.1.0" version = "0.1.1"
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

@ -208,6 +208,14 @@ class Mongo:
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 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:
@ -271,11 +279,11 @@ class Mongo:
# update # update
async def update_document( 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: ) -> 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=do_upsert) response = await self._db[collection].replace_one(target, document, upsert=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}")
@ -371,14 +379,14 @@ class Mongo:
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, do_upsert: bool = False, fields: Optional[dict] = None, return_after: bool = True, 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=do_upsert, upsert=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:
@ -387,14 +395,14 @@ class Mongo:
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, do_upsert: bool = False, fields: Optional[dict] = None, return_after: bool = True, 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=do_upsert, upsert=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: