Source code for chaise.structs

 1"""
 2Various data structures used by chaise.
 3"""
 4
 5import dataclasses
 6import enum
 7import functools
 8import operator
 9
10import chaise  # Be careful using this, for circular import reasons
11
12
[docs] 13@dataclasses.dataclass 14class AllDocs_DocRef: 15 """ 16 A document reference returned by :meth:`~chaise.Database.iter_all_docs` 17 """ 18 19 #: The document ID 20 docid: str 21 22 #: The revision of the document 23 rev: str 24 25 _db: "chaise.Database" 26 _doc: object | None 27
[docs] 28 async def doc(self): 29 """ 30 Actually get the document. Since docid+rev is roughly immutable, caches 31 it. 32 33 (Might be pre-loaded by the producing function.) 34 """ 35 if self._doc is None: 36 # docid + revision is immutable(ish), so it's safe to cache 37 # (It can still be deleted/vacuumed, but that's fine. probably.) 38 self._doc = await self._db.get(self.docid, rev=self.rev) 39 return self._doc
40 41
[docs] 42class AscDesc(enum.StrEnum): 43 ASC = "asc" 44 DESC = "desc"
45 46
[docs] 47@dataclasses.dataclass 48class IndexDef: 49 """ 50 Index Definition. 51 """ 52 53 #: Fields and their direction. 54 fields: dict[str, AscDesc]
55 56
[docs] 57@dataclasses.dataclass 58class Index: 59 """ 60 Return of :meth:`~chaise.Database.iter_indexes` 61 """ 62 63 #: ID of the design document the index belongs to. 64 ddoc: str 65 #: Name of the index. 66 name: str 67 #: Partitioned (:const:`True`) or global (:const:`False`) index. 68 partitioned: bool 69 #: Type of the index. Currently ``"json"`` is the only supported type. 70 type: str 71 #: Definition of the index, containing the indexed fields and the sort 72 #: order: ascending or descending. 73 def_: IndexDef 74
[docs] 75 @classmethod 76 def from_dict(cls, data): 77 return cls( 78 ddoc=data["ddoc"].removeprefix("_design/"), 79 name=data["name"], 80 partitioned=data["partitioned"], 81 type=data["type"], 82 def_=IndexDef( 83 fields={ 84 k: AscDesc(v) 85 for k, v in functools.reduce( 86 operator.or_, data["def"]["fields"], {} 87 ).items() 88 }, 89 ), 90 )