6 Commits
Author SHA1 Message Date
dsql 1d55efbcdd chore: ignore .claude/ dir (CLAUDE.md now lives under .claude/)
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 21:55:13 -04:00
dsql fe8f2da480 fix: annotate database property return type (AsyncIOMotorDatabase)
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 21:35:22 -04:00
dsql 9ad3458cf9 fix: align push helpers to matched_count for success-contract consistency (v0.1.3)
mongo-1: document_push_array and document_push_and_set still returned modified_count>0
while the v0.1.2 wave moved the sibling single-doc update helpers to matched_count>0.
$push always mutates so the two agree in practice (no reachable behavioral change), but
the helpers now match the documented 'True when a document matched' contract uniformly.

sibling-grep: zero consumers of either push helper.
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 20:48:08 -04:00
dsql 816c71a3f5 docs: pin install line to release, note unpinned-latest option
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 18:13:56 -04:00
dsql 6103d63b9f docs: show unpinned install line; note tag-pinning for reproducibility
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 18:07:41 -04:00
dsql 51592567e1 fix: matched_count for idempotent updates; document drop-on-missing-key (v0.1.2)
- update_document_field/update_document_operator/document_pop_array return
  matched_count > 0, so an idempotent write that matched a doc but changed nothing
  ( to the same value,  of an absent value) reports success instead of False
  (L19)
- document the bare-proxy escape hatch is mongo.collection(name) not mongo[name], and
  that get_document_hashmap/get_document_fields skip docs missing the key (nits).

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 17:58:26 -04:00
5 changed files with 58 additions and 20 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
# claude
CLAUDE.md
.claude/
# python
__pycache__/
+5 -3
View File
@@ -8,17 +8,19 @@ 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.1
mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.3
```
Direct:
```bash
pip install "mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.1"
pip install "mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.3"
```
Requires `motor` and `pymongo` (pulled transitively).
Drop the `@v0.1.3` suffix from the line above to install the latest unpinned.
## Usage
**Object (preferred)** — one client per process:
@@ -71,4 +73,4 @@ are included.
## Versioning
Tagged `vX.Y.Z`. Pin the tag in `requirements.txt`; bump deliberately.
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.
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "mongo"
version = "0.1.1"
version = "0.1.3"
description = "async mongodb wrapper over motor with a raw escape hatch"
requires-python = ">=3.10"
dependencies = [
+5 -1
View File
@@ -7,7 +7,11 @@ def __getattr__(name: str):
"""proxy bare package attribute access to the default instance (PEP 562)
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(Mongo, name):
return getattr(instance(), name)
+46 -14
View File
@@ -27,7 +27,7 @@ import logging
from typing import Any, List, Optional
from pymongo import ReturnDocument
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection, AsyncIOMotorDatabase
log = logging.getLogger(__name__)
@@ -48,7 +48,7 @@ class Mongo:
return self._db[name]
@property
def database(self):
def database(self) -> AsyncIOMotorDatabase:
"""raw motor database handle"""
return self._db
@@ -181,7 +181,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 +196,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]
@@ -299,10 +307,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 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:
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,10 +323,15 @@ 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
(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:
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
@@ -343,10 +361,15 @@ 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 (consistent with the other single-doc
update helpers); $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 +378,17 @@ 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 (consistent with the other single-doc
update helpers).
"""
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 +396,15 @@ 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 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:
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