Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d55efbcdd | ||
|
|
fe8f2da480 | ||
|
|
9ad3458cf9 | ||
|
|
816c71a3f5 | ||
|
|
6103d63b9f | ||
|
|
51592567e1 | ||
|
|
3ceef9c4a8 |
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
# claude
|
# claude
|
||||||
CLAUDE.md
|
.claude/
|
||||||
|
|
||||||
# python
|
# python
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
@@ -8,17 +8,19 @@ 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.3
|
||||||
```
|
```
|
||||||
|
|
||||||
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.3"
|
||||||
```
|
```
|
||||||
|
|
||||||
Requires `motor` and `pymongo` (pulled transitively).
|
Requires `motor` and `pymongo` (pulled transitively).
|
||||||
|
|
||||||
|
Drop the `@v0.1.3` suffix from the line above to install the latest unpinned.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
**Object (preferred)** — one client per process:
|
**Object (preferred)** — one client per process:
|
||||||
@@ -71,4 +73,4 @@ are included.
|
|||||||
|
|
||||||
## Versioning
|
## 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
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "mongo"
|
name = "mongo"
|
||||||
version = "0.1.0"
|
version = "0.1.3"
|
||||||
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 = [
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ 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(Mongo, name):
|
if not name.startswith("_") and hasattr(Mongo, name):
|
||||||
return getattr(instance(), name)
|
return getattr(instance(), name)
|
||||||
|
|||||||
+60
-20
@@ -27,7 +27,7 @@ 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
|
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection, AsyncIOMotorDatabase
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ class Mongo:
|
|||||||
return self._db[name]
|
return self._db[name]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def database(self):
|
def database(self) -> AsyncIOMotorDatabase:
|
||||||
"""raw motor database handle"""
|
"""raw motor database handle"""
|
||||||
return self._db
|
return self._db
|
||||||
|
|
||||||
@@ -181,7 +181,11 @@ class Mongo:
|
|||||||
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}
|
||||||
@@ -192,7 +196,11 @@ class Mongo:
|
|||||||
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]
|
||||||
@@ -208,6 +216,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 +287,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}")
|
||||||
@@ -291,10 +307,15 @@ class Mongo:
|
|||||||
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.modified_count > 0
|
return response.matched_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
|
||||||
@@ -302,10 +323,15 @@ class Mongo:
|
|||||||
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.modified_count > 0
|
return response.matched_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
|
||||||
@@ -335,10 +361,15 @@ class Mongo:
|
|||||||
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.modified_count > 0
|
return response.matched_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
|
||||||
@@ -347,13 +378,17 @@ class Mongo:
|
|||||||
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.modified_count > 0
|
return response.matched_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
|
||||||
@@ -361,24 +396,29 @@ class Mongo:
|
|||||||
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.modified_count > 0
|
return response.matched_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, 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 +427,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:
|
||||||
|
|||||||
Reference in New Issue
Block a user