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.
- ppzmongo-0.0.1/PKG-INFO +15 -0
- ppzmongo-0.0.1/README.md +5 -0
- ppzmongo-0.0.1/ppzmongo/__init__.py +76 -0
- ppzmongo-0.0.1/ppzmongo/util.py +7 -0
- ppzmongo-0.0.1/ppzmongo.egg-info/PKG-INFO +15 -0
- ppzmongo-0.0.1/ppzmongo.egg-info/SOURCES.txt +9 -0
- ppzmongo-0.0.1/ppzmongo.egg-info/dependency_links.txt +1 -0
- ppzmongo-0.0.1/ppzmongo.egg-info/requires.txt +2 -0
- ppzmongo-0.0.1/ppzmongo.egg-info/top_level.txt +1 -0
- ppzmongo-0.0.1/pyproject.toml +21 -0
- ppzmongo-0.0.1/setup.cfg +4 -0
ppzmongo-0.0.1/PKG-INFO
ADDED
|
@@ -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
|
+
```
|
ppzmongo-0.0.1/README.md
ADDED
|
@@ -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,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 @@
|
|
|
1
|
+
|
|
@@ -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*"]
|
ppzmongo-0.0.1/setup.cfg
ADDED