Debian Codegraph @

beanie/odm/operators/find/logical.py

raw
1 from abc import ABC
2 from collections.abc import Mapping
3 from typing import Any
4
5 from beanie.odm.operators.find import BaseFindOperator
6
7
8 class BaseFindLogicalOperator(BaseFindOperator, ABC): ...
9
10
11 class LogicalOperatorForListOfExpressions(BaseFindLogicalOperator):
12 # todo: handle query return typing
13 operator: str = ""
14
15 def __init__(
16 self,
17 *expressions: (
18 BaseFindOperator | dict[str, Any] | Mapping[str, Any] | bool
19 ),
20 ):
21 self.expressions = list(expressions)
22
23 @property
24 def query(self):
25 if not self.expressions:
26 raise AttributeError("At least one expression must be provided")
27 if len(self.expressions) == 1:
28 return self.expressions[0]
29 return {self.operator: self.expressions}
30
31
32 class Or(LogicalOperatorForListOfExpressions):
33 """
34 `$or` query operator
35
36 Example:
37
38 ```python
39 class Product(Document):
40 price: float
41 category: str
42
43 Or(Product.price<10, Product.category=="Sweets")
44 ```
45
46 Will return query object like
47
48 ```python
49 {"$or": [{"price": {"$lt": 10}}, {"category": "Sweets"}]}
50 ```
51
52 MongoDB doc:
53 <https://docs.mongodb.com/manual/reference/operator/query/or/>
54 """
55
56 operator = "$or"
57
58
59 class And(LogicalOperatorForListOfExpressions):
60 """
61 `$and` query operator
62
63 Example:
64
65 ```python
66 class Product(Document):
67 price: float
68 category: str
69
70 And(Product.price<10, Product.category=="Sweets")
71 ```
72
73 Will return query object like
74
75 ```python
76 {"$and": [{"price": {"$lt": 10}}, {"category": "Sweets"}]}
77 ```
78
79 MongoDB doc:
80 <https://docs.mongodb.com/manual/reference/operator/query/and/>
81 """
82
83 operator = "$and"
84
85
86 class Nor(BaseFindLogicalOperator):
87 """
88 `$nor` query operator
89
90 Example:
91
92 ```python
93 class Product(Document):
94 price: float
95 category: str
96
97 Nor(Product.price<10, Product.category=="Sweets")
98 ```
99
100 Will return query object like
101
102 ```python
103 {"$nor": [{"price": {"$lt": 10}}, {"category": "Sweets"}]}
104 ```
105
106 MongoDB doc:
107 <https://docs.mongodb.com/manual/reference/operator/query/nor/>
108 """
109
110 def __init__(
111 self,
112 *expressions: (
113 BaseFindOperator | dict[str, Any] | Mapping[str, Any] | bool
114 ),
115 ):
116 self.expressions = list(expressions)
117
118 @property
119 def query(self):
120 return {"$nor": self.expressions}
121
122
123 class Not(BaseFindLogicalOperator):
124 """
125 `$not` query operator
126
127 Example:
128
129 ```python
130 class Product(Document):
131 price: float
132 category: str
133
134 Not(Product.price<10)
135 ```
136
137 Will return query object like
138
139 ```python
140 {"$not": {"price": {"$lt": 10}}}
141 ```
142
143 MongoDB doc:
144 <https://docs.mongodb.com/manual/reference/operator/query/not/>
145 """
146
147 def __init__(self, expression: Mapping[str, Any]):
148 self.expression = expression
149
150 @property
151 def query(self):
152 if len(self.expression) == 1:
153 expression_key = list(self.expression.keys())[0]
154 if expression_key.startswith("$"):
155 raise AttributeError(
156 "Not operator can not be used with operators"
157 )
158 value = self.expression[expression_key]
159 if isinstance(value, dict):
160 internal_key = list(value.keys())[0]
161 if internal_key.startswith("$"):
162 return {expression_key: {"$not": value}}
163
164 return {expression_key: {"$not": {"$eq": value}}}
165 raise AttributeError(
166 "Not operator can only be used with one expression"
167 )