-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrepo_adapter.py
More file actions
322 lines (263 loc) · 9.67 KB
/
repo_adapter.py
File metadata and controls
322 lines (263 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import typing as t
import sqlalchemy
import sqlalchemy.orm
from pydantic import BaseModel
from quart_sqlalchemy.sim.repo import SQLAlchemyRepository
from quart_sqlalchemy.types import ColumnExpr
from quart_sqlalchemy.types import EntityIdT
from quart_sqlalchemy.types import EntityT
from quart_sqlalchemy.types import ORMOption
from quart_sqlalchemy.types import Selectable
from quart_sqlalchemy.types import SessionT
sa = sqlalchemy
class BaseModelSchema(BaseModel):
class Config:
from_orm = True
class BaseCreateSchema(BaseModelSchema):
pass
class BaseUpdateSchema(BaseModelSchema):
pass
ModelSchemaT = t.TypeVar("ModelSchemaT", bound=BaseModelSchema)
CreateSchemaT = t.TypeVar("CreateSchemaT", bound=BaseCreateSchema)
UpdateSchemaT = t.TypeVar("UpdateSchemaT", bound=BaseUpdateSchema)
class RepositoryLegacyAdapter(t.Generic[EntityT, EntityIdT, SessionT]):
model: t.Type[EntityT]
identity: t.Type[EntityIdT]
def __init__(self, model: t.Type[EntityT], identity: t.Type[EntityIdT]):
self.model = model
self.identity = identity
self._adapted = SQLAlchemyRepository(model, identity)
def get_by(
self,
session: SessionT,
filters=None,
allow_inactive=False,
join_list=None,
order_by_clause=None,
for_update=False,
offset=None,
limit=None,
) -> t.Sequence[EntityT]:
if filters is None:
raise ValueError("Full table scans are prohibited. Please provide filters")
join_list = join_list or ()
order_by_clause = (order_by_clause, ) if order_by_clause is not None else ()
return self._adapted.select(
session,
conditions=filters,
options=[sa.orm.selectinload(getattr(self.model, attr)) for attr in join_list],
for_update=for_update,
order_by=order_by_clause,
offset=offset,
limit=limit,
include_inactive=allow_inactive,
).all()
def get_by_id(
self,
session: SessionT,
model_id=None,
allow_inactive=False,
join_list=None,
for_update=False,
) -> t.Optional[EntityT]:
if model_id is None:
raise ValueError("model_id is required")
join_list = join_list or ()
return self._adapted.get(
session,
id_=model_id,
options=[sa.orm.selectinload(getattr(self.model, attr)) for attr in join_list],
for_update=for_update,
include_inactive=allow_inactive,
)
def one(
self,
session: SessionT,
filters=None,
join_list=None,
for_update=False,
include_inactive=False,
) -> EntityT:
filters = filters or ()
join_list = join_list or ()
return self._adapted.select(
session,
conditions=filters,
options=[sa.orm.selectinload(getattr(self.model, attr)) for attr in join_list],
for_update=for_update,
include_inactive=include_inactive,
).one()
def count_by(
self,
session: SessionT,
filters=None,
group_by=None,
distinct_column=None,
):
if filters is None:
raise ValueError("Full table scans are prohibited. Please provide filters")
group_by = group_by or ()
if distinct_column:
selectables = [sa.label("count", sa.func.count(sa.func.distinct(distinct_column)))]
else:
selectables = [sa.label("count", sa.func.count(self.model.id))]
selectables.extend(group.expression for group in group_by)
result = self._adapted.select(session, selectables, conditions=filters, group_by=group_by)
return result.all()
def add(self, session: SessionT, **kwargs) -> EntityT:
return self._adapted.insert(session, kwargs)
def update(self, session: SessionT, model_id=None, **kwargs) -> EntityT:
return self._adapted.update(session, id_=model_id, values=kwargs)
def update_by(self, session: SessionT, filters=None, **kwargs) -> EntityT:
if not filters:
raise ValueError("Full table scans are prohibited. Please provide filters")
row = self._adapted.select(session, conditions=filters, limit=2).one()
return self._adapted.update(session, id_=row.id, values=kwargs)
def delete_by_id(self, session: SessionT, model_id=None) -> None:
self._adapted.delete(session, id_=model_id, include_inactive=True)
def delete_one_by(self, session: SessionT, filters=None, optional=False) -> None:
filters = filters or ()
result = self._adapted.select(session, conditions=filters, limit=1)
if optional:
row = result.one_or_none()
if row is None:
return
else:
row = result.one()
self._adapted.delete(session, id_=row.id)
def exist(self, session: SessionT, filters=None, allow_inactive=False) -> bool:
filters = filters or ()
return self._adapted.exists(
session,
conditions=filters,
include_inactive=allow_inactive,
)
def yield_by_chunk(
self, session: SessionT, chunk_size=100, join_list=None, filters=None, allow_inactive=False
):
filters = filters or ()
join_list = join_list or ()
yield from self._adapted.select(
session,
conditions=filters,
options=[
sa.orm.selectinload(getattr(self.model, attr))
for attr in join_list
],
include_inactive=allow_inactive,
yield_by_chunk=chunk_size,
)
class PydanticScalarResult(sa.ScalarResult, t.Generic[ModelSchemaT]):
pydantic_schema: t.Type[ModelSchemaT]
def __init__(self, scalar_result: t.Any, pydantic_schema: t.Type[ModelSchemaT]):
for attribute in scalar_result.__slots__:
setattr(self, attribute, getattr(scalar_result, attribute))
self.pydantic_schema = pydantic_schema
def _translate_many(self, rows):
return [self.pydantic_schema.from_orm(row) for row in rows]
def _translate_one(self, row):
if row is None:
return
return self.pydantic_schema.from_orm(row)
def all(self):
return self._translate_many(super().all())
def fetchall(self):
return self._translate_many(super().fetchall())
def fetchmany(self, *args, **kwargs):
return self._translate_many(super().fetchmany(*args, **kwargs))
def first(self):
return self._translate_one(super().first())
def one(self):
return self._translate_one(super().one())
def one_or_none(self):
return self._translate_one(super().one_or_none())
def partitions(self, *args, **kwargs):
for partition in super().partitions(*args, **kwargs):
yield self._translate_many(partition)
class PydanticRepository(
SQLAlchemyRepository[EntityT, EntityIdT, SessionT],
t.Generic[EntityT, EntityIdT, SessionT, ModelSchemaT, CreateSchemaT, UpdateSchemaT],
):
model_schema: t.Type[ModelSchemaT]
def insert(
self,
session: SessionT,
create_schema: CreateSchemaT,
sqla_model=False,
):
create_data = create_schema.dict()
result = super().insert(session, create_data)
return result if sqla_model else self.model_schema.from_orm(result)
def update(
self,
session: SessionT,
id_: EntityIdT,
update_schema: UpdateSchemaT,
sqla_model=False,
):
existing = session.query(self.model).get(id_)
if existing is None:
raise ValueError("Model not found")
update_data = update_schema.dict(exclude_unset=True)
for key, value in update_data.items():
setattr(existing, key, value)
session.add(existing)
session.flush()
session.refresh(existing)
return existing if sqla_model else self.model_schema.from_orm(existing)
def get(
self,
session: SessionT,
id_: EntityIdT,
options: t.Sequence[ORMOption] = (),
execution_options: t.Optional[t.Dict[str, t.Any]] = None,
for_update: bool = False,
include_inactive: bool = False,
sqla_model: bool = False,
):
row = super().get(
session,
id_,
options,
execution_options,
for_update,
include_inactive,
)
if row is None:
return
return row if sqla_model else self.model_schema.from_orm(row)
def select(
self,
session: SessionT,
selectables: t.Sequence[Selectable] = (),
conditions: t.Sequence[ColumnExpr] = (),
group_by: t.Sequence[t.Union[ColumnExpr, str]] = (),
order_by: t.Sequence[t.Union[ColumnExpr, str]] = (),
options: t.Sequence[ORMOption] = (),
execution_options: t.Optional[t.Dict[str, t.Any]] = None,
offset: t.Optional[int] = None,
limit: t.Optional[int] = None,
distinct: bool = False,
for_update: bool = False,
include_inactive: bool = False,
yield_by_chunk: t.Optional[int] = None,
sqla_model: bool = False,
):
result = super().select(
session,
selectables,
conditions,
group_by,
order_by,
options,
execution_options,
offset,
limit,
distinct,
for_update,
include_inactive,
yield_by_chunk,
)
if sqla_model:
return result
return PydanticScalarResult[self.model_schema](result, self.model_schema)