ppzmongo 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: ppzmongo
3
+ Version: 0.0.1
4
+ Summary: PPz's mongo helper
5
+ License-Expression: Unlicense
6
+ Requires-Python: >=3.13
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: pydantic>=2.13.4
9
+ Requires-Dist: pymongo>=4.17.0
10
+
11
+ # ppz-mongo.py
12
+
13
+ ``` bash
14
+ uv add ppz-mongo
15
+ ```
@@ -0,0 +1,5 @@
1
+ # ppz-mongo.py
2
+
3
+ ``` bash
4
+ uv add ppz-mongo
5
+ ```
@@ -0,0 +1,76 @@
1
+ from bson import ObjectId
2
+ from dataclasses import dataclass
3
+ from abc import ABC, abstractmethod
4
+ from datetime import datetime
5
+
6
+ from pymongo import ReturnDocument
7
+ from pymongo.asynchronous.collection import AsyncCollection
8
+ from pydantic import BaseModel, Field
9
+
10
+ from .util import now, dd
11
+
12
+ class NotFound(Exception):
13
+ def __init__(self):
14
+ super().__init__('Not Found')
15
+
16
+ class Document(BaseModel):
17
+ update_at: list[datetime] = Field(default_factory=lambda: [now()])
18
+
19
+ @dataclass
20
+ class Data[D: Document]:
21
+ id: ObjectId
22
+ doc: D
23
+
24
+ class Collection[D: Document](ABC):
25
+ _name: str
26
+ _Doc: type[D]
27
+ def __init__(self, name: str, Document: type[D]):
28
+ self._name = name
29
+ self._Doc = Document
30
+
31
+ @abstractmethod
32
+ def _get(self) -> AsyncCollection[dd]:
33
+ pass
34
+
35
+ async def insert_one(self, doc: dd):
36
+ self._Doc(**doc)
37
+ result = await self._get().insert_one(doc)
38
+ return result.inserted_id
39
+
40
+ async def find_one(self, filter: dd) -> None | Data[D]:
41
+ result = await self._get().find_one(filter)
42
+ if result is None:
43
+ return None
44
+ id: ObjectId = result['_id']
45
+ doc = self._Doc(**result)
46
+ return Data(id, doc)
47
+
48
+ async def fbi(self, id: str | ObjectId):
49
+ data = await self.find_one({ '_id': id })
50
+ if data is None:
51
+ raise NotFound()
52
+ return data.doc
53
+
54
+ async def find_all(self):
55
+ list = await self._get().find().to_list()
56
+ return [Data(item['_id'], self._Doc(**item)) for item in list]
57
+
58
+ async def update_one(self, filter: dd, update: dd) -> None | Data[D]:
59
+ if update.get('$push') is None:
60
+ update['$push'] = {}
61
+ if update['$push'].get('update_at') is not None: # type: ignore
62
+ raise Exception('update update_at mannually')
63
+ update['$push']['update_at'] = now()
64
+
65
+ updated = await self._get().find_one_and_update(filter, update,
66
+ return_document=ReturnDocument.AFTER
67
+ )
68
+ if updated is None:
69
+ return None
70
+ return Data(updated['_id'], self._Doc(**updated))
71
+
72
+ async def ubi(self, id: str | ObjectId, update: dd) -> D:
73
+ data = await self.update_one({ '_id': id }, update)
74
+ if data is None:
75
+ raise NotFound()
76
+ return data.doc
@@ -0,0 +1,7 @@
1
+ from datetime import datetime, timezone
2
+ from typing import Any
3
+
4
+ type dd = dict[str, Any]
5
+
6
+ def now():
7
+ return datetime.now(timezone.utc)
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: ppzmongo
3
+ Version: 0.0.1
4
+ Summary: PPz's mongo helper
5
+ License-Expression: Unlicense
6
+ Requires-Python: >=3.13
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: pydantic>=2.13.4
9
+ Requires-Dist: pymongo>=4.17.0
10
+
11
+ # ppz-mongo.py
12
+
13
+ ``` bash
14
+ uv add ppz-mongo
15
+ ```
@@ -0,0 +1,9 @@
1
+ README.md
2
+ pyproject.toml
3
+ ppzmongo/__init__.py
4
+ ppzmongo/util.py
5
+ ppzmongo.egg-info/PKG-INFO
6
+ ppzmongo.egg-info/SOURCES.txt
7
+ ppzmongo.egg-info/dependency_links.txt
8
+ ppzmongo.egg-info/requires.txt
9
+ ppzmongo.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ pydantic>=2.13.4
2
+ pymongo>=4.17.0
@@ -0,0 +1 @@
1
+ ppzmongo
@@ -0,0 +1,21 @@
1
+ [project]
2
+ name = "ppzmongo"
3
+ version = "0.0.1"
4
+ description = "PPz's mongo helper"
5
+ readme = "README.md"
6
+ requires-python = ">=3.13"
7
+ license = "Unlicense"
8
+ dependencies = [
9
+ "pydantic>=2.13.4",
10
+ "pymongo>=4.17.0",
11
+ ]
12
+
13
+ [tool.pyright]
14
+ typeCheckingMode = "strict"
15
+
16
+ [build-system]
17
+ requires = ["setuptools"]
18
+ build-backend = "setuptools.build_meta"
19
+
20
+ [tool.setuptools.packages.find]
21
+ include = ["ppzmongo*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+