Skip to content

Commit 576bfe6

Browse files
committed
Move and fix interface docblocks to protocols
1 parent 8035625 commit 576bfe6

File tree

4 files changed

+207
-129
lines changed

4 files changed

+207
-129
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ The classes provide some common use methods:
200200
* `delete`: Delete a model
201201
* `find`: Search for a list of models (basically an adapter for SELECT queries)
202202
* `paginated_find`: Search for a list of models, with pagination support
203+
* `cursor_paginated_find`: Search for a list of models, with cursor based pagination support
203204

204205
### Session lifecycle in repositories
205206

sqlalchemy_bind_manager/_repository/async_.py

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ async def _get_session(self, commit: bool = True) -> AsyncIterator[AsyncSession]
6565
yield self._external_session
6666

6767
async def save(self, instance: MODEL) -> MODEL:
68-
"""Persist a model.
69-
70-
:param instance: A mapped object instance to be persisted
71-
:return: The model instance after being persisted
72-
"""
7368
async with self._get_session() as session:
7469
session.add(instance)
7570
return instance
@@ -78,24 +73,11 @@ async def save_many(
7873
self,
7974
instances: Iterable[MODEL],
8075
) -> Iterable[MODEL]:
81-
"""Persist many models in a single database get_session.
82-
83-
:param instances: A list of mapped objects to be persisted
84-
:type instances: Iterable
85-
:return: The model instances after being persisted
86-
"""
8776
async with self._get_session() as session:
8877
session.add_all(instances)
8978
return instances
9079

9180
async def get(self, identifier: PRIMARY_KEY) -> MODEL:
92-
"""Get a model by primary key.
93-
94-
:param identifier: The primary key
95-
:return: A model instance
96-
:raises ModelNotFound: No model has been found using the primary key
97-
"""
98-
# TODO: implement get_many()
9981
async with self._get_session(commit=False) as session:
10082
model = await session.get(self._model, identifier)
10183
if model is None:
@@ -106,11 +88,6 @@ async def delete(
10688
self,
10789
entity: Union[MODEL, PRIMARY_KEY],
10890
) -> None:
109-
"""Deletes a model.
110-
111-
:param entity: The model instance or the primary key
112-
:type entity: Union[MODEL, PRIMARY_KEY]
113-
"""
11491
# TODO: delete without loading the model
11592
if isinstance(entity, self._model):
11693
obj = entity
@@ -124,20 +101,6 @@ async def find(
124101
search_params: Union[None, Mapping[str, Any]] = None,
125102
order_by: Union[None, Iterable[Union[str, Tuple[str, SortDirection]]]] = None,
126103
) -> List[MODEL]:
127-
"""Find models using filters
128-
129-
E.g.
130-
find(search_params={"name": "John"}) finds all models with name = John
131-
132-
:param order_by:
133-
:param search_params: A dictionary containing equality filters
134-
:param limit: Number of models to retrieve
135-
:type limit: int
136-
:param offset: Number of models to skip
137-
:type offset: int
138-
:return: A collection of models
139-
:rtype: List
140-
"""
141104
stmt = self._find_query(search_params, order_by)
142105

143106
async with self._get_session() as session:
@@ -147,25 +110,10 @@ async def find(
147110
async def paginated_find(
148111
self,
149112
items_per_page: int,
150-
page: int,
113+
page: int = 1,
151114
search_params: Union[None, Mapping[str, Any]] = None,
152115
order_by: Union[None, Iterable[Union[str, Tuple[str, SortDirection]]]] = None,
153116
) -> PaginatedResult[MODEL]:
154-
"""Find models using filters and pagination
155-
156-
E.g.
157-
find(name="John") finds all models with name = John
158-
159-
:param items_per_page: Number of models to retrieve
160-
:type items_per_page: int
161-
:param page: Page to retrieve
162-
:type page: int
163-
:param search_params: A dictionary containing equality filters
164-
:param order_by:
165-
:return: A collection of models
166-
:rtype: List
167-
"""
168-
169117
find_stmt = self._find_query(search_params, order_by)
170118
paginated_stmt = self._paginate_query_by_page(find_stmt, page, items_per_page)
171119

@@ -191,23 +139,6 @@ async def cursor_paginated_find(
191139
is_before_cursor: bool = False,
192140
search_params: Union[None, Mapping[str, Any]] = None,
193141
) -> CursorPaginatedResult[MODEL]:
194-
"""Find models using filters and cursor based pagination
195-
196-
E.g.
197-
find(name="John") finds all models with name = John
198-
199-
:param items_per_page: Number of models to retrieve
200-
:type items_per_page: int
201-
:param order_by: Model property to use for ordering and before/after evaluation
202-
:type order_by: str
203-
:param before: Identifier of the last node to skip
204-
:type before: Union[int, str]
205-
:param after: Identifier of the last node to skip
206-
:type after: Union[int, str]
207-
:param search_params: A dictionary containing equality filters
208-
:return: A collection of models
209-
:rtype: List
210-
"""
211142
find_stmt = self._find_query(search_params)
212143
paginated_stmt = self._cursor_paginated_query(
213144
find_stmt,

sqlalchemy_bind_manager/_repository/sync.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ def __init__(
4141
session: Union[Session, None] = None,
4242
model_class: Union[Type[MODEL], None] = None,
4343
) -> None:
44-
"""
45-
:param bind: A configured instance of SQLAlchemyBind
46-
:type bind: Union[SQLAlchemyBind, None]
47-
:param session: An externally managed session
48-
:type session: Union[Session, None]
49-
:param model_class: A mapped SQLAlchemy model
50-
:type model_class: Union[Type[MODEL], None]
51-
"""
5244
super().__init__(model_class=model_class)
5345
if not (bool(bind) ^ bool(session)):
5446
raise InvalidConfig("Either `bind` or `session` have to be used, not both")
@@ -65,46 +57,23 @@ def _get_session(self, commit: bool = True) -> Iterator[Session]:
6557
yield self._external_session
6658

6759
def save(self, instance: MODEL) -> MODEL:
68-
"""Persist a model.
69-
70-
:param instance: A mapped object instance to be persisted
71-
:return: The model instance after being persisted
72-
"""
7360
with self._get_session() as session:
7461
session.add(instance)
7562
return instance
7663

7764
def save_many(self, instances: Iterable[MODEL]) -> Iterable[MODEL]:
78-
"""Persist many models in a single database get_session.
79-
80-
:param instances: A list of mapped objects to be persisted
81-
:type instances: Iterable
82-
:return: The model instances after being persisted
83-
"""
8465
with self._get_session() as session:
8566
session.add_all(instances)
8667
return instances
8768

8869
def get(self, identifier: PRIMARY_KEY) -> MODEL:
89-
"""Get a model by primary key.
90-
91-
:param identifier: The primary key
92-
:return: A model instance
93-
:raises ModelNotFound: No model has been found using the primary key
94-
"""
95-
# TODO: implement get_many()
9670
with self._get_session(commit=False) as session:
9771
model = session.get(self._model, identifier)
9872
if model is None:
9973
raise ModelNotFound("No rows found for provided primary key.")
10074
return model
10175

10276
def delete(self, entity: Union[MODEL, PRIMARY_KEY]) -> None:
103-
"""Deletes a model.
104-
105-
:param entity: The model instance or the primary key
106-
:type entity: Union[MODEL, PRIMARY_KEY]
107-
"""
10877
# TODO: delete without loading the model
10978
if isinstance(entity, self._model):
11079
obj = entity
@@ -118,16 +87,6 @@ def find(
11887
search_params: Union[None, Mapping[str, Any]] = None,
11988
order_by: Union[None, Iterable[Union[str, Tuple[str, SortDirection]]]] = None,
12089
) -> List[MODEL]:
121-
"""Find models using filters
122-
123-
E.g.
124-
find(name="John") finds all models with name = John
125-
126-
:param search_params: A dictionary containing equality filters
127-
:param order_by:
128-
:return: A collection of models
129-
:rtype: List
130-
"""
13190
stmt = self._find_query(search_params, order_by)
13291

13392
with self._get_session() as session:
@@ -137,25 +96,10 @@ def find(
13796
def paginated_find(
13897
self,
13998
items_per_page: int,
140-
page: int,
99+
page: int = 1,
141100
search_params: Union[None, Mapping[str, Any]] = None,
142101
order_by: Union[None, Iterable[Union[str, Tuple[str, SortDirection]]]] = None,
143102
) -> PaginatedResult[MODEL]:
144-
"""Find models using filters and page based pagination
145-
146-
E.g.
147-
find(name="John") finds all models with name = John
148-
149-
:param items_per_page: Number of models to retrieve
150-
:type items_per_page: int
151-
:param page: Page to retrieve
152-
:type page: int
153-
:param search_params: A dictionary containing equality filters
154-
:param order_by:
155-
:return: A collection of models
156-
:rtype: List
157-
"""
158-
159103
find_stmt = self._find_query(search_params, order_by)
160104
paginated_stmt = self._paginate_query_by_page(find_stmt, page, items_per_page)
161105

0 commit comments

Comments
 (0)