fix: rename create_collection indexes->index + doc notes (mongo-3/4/5)
- mongo-3: create_collection param renamed indexes -> index (it seeds ONE index; a key-spec builds one compound index, not two). update keyword call sites indexes= -> index=. - mongo-4: docstring notes an instance is single-use after close() (pymongo 4.x close is irreversible; connect() only pings). - mongo-5: docstring notes construction may raise InvalidURI for a malformed URI. - README: create_collection index note. bump v0.1.5 -> v0.1.6 Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
@@ -8,18 +8,18 @@ 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.5
|
mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.6
|
||||||
```
|
```
|
||||||
|
|
||||||
Direct:
|
Direct:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install "mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.5"
|
pip install "mongo @ git+ssh://git@git.rethinkstudios.io/rethink-public/mongo.git@v0.1.6"
|
||||||
```
|
```
|
||||||
|
|
||||||
Requires `motor` and `pymongo` (pulled transitively).
|
Requires `motor` and `pymongo` (pulled transitively).
|
||||||
|
|
||||||
Drop the `@v0.1.5` suffix from the line above to install the latest unpinned.
|
Drop the `@v0.1.6` suffix from the line above to install the latest unpinned.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@@ -87,6 +87,9 @@ are included.
|
|||||||
|
|
||||||
- `from mongo import func` won't see the proxy (resolved at import, before `init`).
|
- `from mongo import func` won't see the proxy (resolved at import, before `init`).
|
||||||
Use `import mongo` then `mongo.func(...)`.
|
Use `import mongo` then `mongo.func(...)`.
|
||||||
|
- `create_collection(collection, index=...)` seeds **one** index (the param is `index`,
|
||||||
|
renamed from `indexes` in v0.1.6). A key-spec like `[("a",1),("b",1)]` builds one
|
||||||
|
compound index, not two — call `create_index` per index for multiples.
|
||||||
- `find_one_and_update` returns the **after** image by default (`return_after=True`).
|
- `find_one_and_update` returns the **after** image by default (`return_after=True`).
|
||||||
- `bulk_write` takes pymongo ops the caller builds:
|
- `bulk_write` takes pymongo ops the caller builds:
|
||||||
```python
|
```python
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "mongo"
|
name = "mongo"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
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 = [
|
||||||
|
|||||||
+19
-5
@@ -53,6 +53,10 @@ class MongoDB:
|
|||||||
"""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):
|
||||||
|
# motor builds the client eagerly here (no network I/O yet). a syntactically
|
||||||
|
# malformed URI (e.g. "mongodb://") makes the driver raise InvalidURI at
|
||||||
|
# construction, before connect() — a deploy-time value, so it surfaces at startup
|
||||||
|
# either way and there is nothing to clean up.
|
||||||
self._client = AsyncIOMotorClient(connection_string)
|
self._client = AsyncIOMotorClient(connection_string)
|
||||||
self._db = self._client[database]
|
self._db = self._client[database]
|
||||||
|
|
||||||
@@ -62,6 +66,10 @@ class MongoDB:
|
|||||||
motor connects lazily, so this is optional — call it to fail early on a bad
|
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
|
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.
|
connect()). raises on a bad connection, unlike the swallowing wrapped methods.
|
||||||
|
|
||||||
|
an instance is single-use: pymongo 4.x `close()` is irreversible, and connect()
|
||||||
|
only pings (it does not rebuild the client). do not reuse an instance after
|
||||||
|
close()/`async with` exit — construct a fresh MongoDB.
|
||||||
"""
|
"""
|
||||||
await self._client.admin.command("ping")
|
await self._client.admin.command("ping")
|
||||||
return self
|
return self
|
||||||
@@ -109,13 +117,19 @@ class MongoDB:
|
|||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# collection / index management
|
# collection / index management
|
||||||
|
|
||||||
async def create_collection(self, collection: str, indexes=None) -> bool:
|
async def create_collection(self, collection: str, index=None) -> bool:
|
||||||
"""create a collection, optionally seeding an index"""
|
"""create a collection, optionally seeding ONE index
|
||||||
|
|
||||||
|
`index` is a single index key-spec (e.g. `[("a", 1), ("b", 1)]` builds one
|
||||||
|
compound index, NOT two). for multiple indexes call `create_index` per index.
|
||||||
|
renamed from `indexes` in v0.1.6 to reflect that it seeds a single index; pass it
|
||||||
|
positionally, or update a keyword call site from `indexes=` to `index=`.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
await self._db.create_collection(collection)
|
await self._db.create_collection(collection)
|
||||||
if indexes:
|
if index:
|
||||||
await self._db[collection].create_index(indexes)
|
await self._db[collection].create_index(index)
|
||||||
log.info(f"created indexes for {collection}")
|
log.info(f"created index for {collection}")
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.create_collection() for {collection}")
|
log.exception(f"db.create_collection() for {collection}")
|
||||||
|
|||||||
Reference in New Issue
Block a user