Skip to content

Commit 8f8af8a

Browse files
author
matmoncon
committed
feat: add type for projections
1 parent 896f3fa commit 8f8af8a

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

pyneo4j_ogm/core/node.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
)
3737
from pyneo4j_ogm.fields.settings import NodeModelSettings, RelationshipModelSettings
3838
from pyneo4j_ogm.logger import logger
39-
from pyneo4j_ogm.queries.types import MultiHopFilters, NodeFilters, QueryOptions
39+
from pyneo4j_ogm.queries.types import (
40+
MultiHopFilters,
41+
NodeFilters,
42+
Projection,
43+
QueryOptions,
44+
)
4045

4146
if TYPE_CHECKING:
4247
from pyneo4j_ogm.fields.relationship_property import RelationshipProperty
@@ -271,7 +276,7 @@ async def refresh(self) -> None:
271276
async def find_connected_nodes(
272277
self,
273278
filters: MultiHopFilters,
274-
projections: Optional[Dict[str, str]] = None,
279+
projections: Optional[Projection] = None,
275280
options: Optional[QueryOptions] = None,
276281
auto_fetch_nodes: bool = False,
277282
auto_fetch_models: Optional[List[Union[str, Type["NodeModel"]]]] = None,
@@ -281,7 +286,7 @@ async def find_connected_nodes(
281286
282287
Args:
283288
filters (MultiHopFilters): The filters to apply to the query.
284-
projections (Dict[str, str], optional): The properties to project from the node. The keys define
289+
projections (Projection, optional): The properties to project from the node. The keys define
285290
the new keys in the projection and the value defines the model property to be projected. A invalid
286291
or empty projection will result in the whole model instance being returned. Defaults to `None`.
287292
options (QueryOptions, optional): The options to apply to the query. Defaults to `None`.
@@ -414,7 +419,7 @@ async def find_connected_nodes(
414419
async def find_one(
415420
cls: Type[T],
416421
filters: NodeFilters,
417-
projections: Optional[Dict[str, str]] = None,
422+
projections: Optional[Projection] = None,
418423
auto_fetch_nodes: bool = False,
419424
auto_fetch_models: Optional[List[Union[str, Type["NodeModel"]]]] = None,
420425
raise_on_empty: bool = False,
@@ -425,7 +430,7 @@ async def find_one(
425430
426431
Args:
427432
filters (NodeFilters): The filters to apply to the query.
428-
projections (Dict[str, str], optional): The properties to project from the node. The keys define
433+
projections (Projection, optional): The properties to project from the node. The keys define
429434
the new keys in the projection and the value defines the model property to be projected. A invalid
430435
or empty projection will result in the whole model instance being returned. Defaults to `None`.
431436
auto_fetch_nodes (bool, optional): Whether to automatically fetch connected nodes. Takes priority over the
@@ -542,7 +547,7 @@ async def find_one(
542547
async def find_many(
543548
cls: Type[T],
544549
filters: Optional[NodeFilters] = None,
545-
projections: Optional[Dict[str, str]] = None,
550+
projections: Optional[Projection] = None,
546551
options: Optional[QueryOptions] = None,
547552
auto_fetch_nodes: bool = False,
548553
auto_fetch_models: Optional[List[Union[str, Type["NodeModel"]]]] = None,
@@ -553,7 +558,7 @@ async def find_many(
553558
554559
Args:
555560
filters (NodeFilters, optional): The filters to apply to the query. Defaults to `None`.
556-
projections (Dict[str, str], optional): The properties to project from the node. The keys define
561+
projections (Projection, optional): The properties to project from the node. The keys define
557562
the new keys in the projection and the value defines the model property to be projected. A invalid
558563
or empty projection will result in the whole model instance being returned. Defaults to `None`.
559564
options (QueryOptions, optional): The options to apply to the query. Defaults to `None`.

pyneo4j_ogm/core/relationship.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pyneo4j_ogm.fields.settings import RelationshipModelSettings
2424
from pyneo4j_ogm.logger import logger
2525
from pyneo4j_ogm.queries.types import (
26+
Projection,
2627
QueryOptions,
2728
RelationshipFilters,
2829
RelationshipMatchDirection,
@@ -267,7 +268,7 @@ async def end_node(self) -> Type[NodeModel]:
267268
async def find_one(
268269
cls: Type[T],
269270
filters: RelationshipFilters,
270-
projections: Optional[Dict[str, str]] = None,
271+
projections: Optional[Projection] = None,
271272
raise_on_empty: bool = False,
272273
) -> Optional[Union[T, Dict[str, Any]]]:
273274
"""
@@ -276,7 +277,7 @@ async def find_one(
276277
277278
Args:
278279
filters (RelationshipFilters): Expressions applied to the query.
279-
projections (Dict[str, str], optional): The properties to project from the relationship. The keys define
280+
projections (Projection, optional): The properties to project from the relationship. The keys define
280281
the new keys in the projection and the value defines the model property to be projected. A invalid
281282
or empty projection will result in the whole model instance being returned. Defaults to `None`.
282283
raise_on_empty (bool, optional): Whether to raise a `NoResultFound` if no match is found.
@@ -343,7 +344,7 @@ async def find_one(
343344
async def find_many(
344345
cls: Type[T],
345346
filters: Optional[RelationshipFilters] = None,
346-
projections: Optional[Dict[str, str]] = None,
347+
projections: Optional[Projection] = None,
347348
options: Optional[QueryOptions] = None,
348349
) -> List[Union[T, Dict[str, Any]]]:
349350
"""
@@ -353,7 +354,7 @@ async def find_many(
353354
Args:
354355
filters (RelationshipFilters | None, optional): Expressions applied to the query. Defaults to
355356
`None`.
356-
projections (Dict[str, str], optional): The properties to project from the relationship. The keys define
357+
projections (Projection, optional): The properties to project from the relationship. The keys define
357358
the new keys in the projection and the value defines the model property to be projected. A invalid
358359
or empty projection will result in the whole model instance being returned. Defaults to `None`.
359360
options (QueryOptions | None, optional): Options for modifying the query result. Defaults to `None`.

pyneo4j_ogm/fields/relationship_property.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242
from pyneo4j_ogm.queries.query_builder import QueryBuilder
4343
from pyneo4j_ogm.queries.types import (
44+
Projection,
4445
QueryOptions,
4546
RelationshipFilters,
4647
RelationshipPropertyFilters,
@@ -249,7 +250,7 @@ async def relationships(
249250
self,
250251
node: T,
251252
filters: Optional[RelationshipFilters] = None,
252-
projections: Optional[Dict[str, str]] = None,
253+
projections: Optional[Projection] = None,
253254
options: Optional[QueryOptions] = None,
254255
) -> List[U]:
255256
"""
@@ -260,7 +261,7 @@ async def relationships(
260261
node (T): The node to which to get the relationship.
261262
filters (RelationshipFilters | None, optional): Expressions applied to the query. Defaults to
262263
`None`.
263-
projections (Dict[str, str], optional): The properties to project from the node. The keys define
264+
projections (Projection, optional): The properties to project from the node. The keys define
264265
the new keys in the projection and the value defines the model property to be projected. A invalid
265266
or empty projection will result in the whole model instance being returned. Defaults to `None`.
266267
options (QueryOptions, optional): The options to apply to the query. Defaults to `None`.
@@ -681,7 +682,7 @@ async def replace(self, old_node: T, new_node: T) -> List[U]:
681682
async def find_connected_nodes(
682683
self,
683684
filters: Optional[RelationshipPropertyFilters] = None,
684-
projections: Optional[Dict[str, str]] = None,
685+
projections: Optional[Projection] = None,
685686
options: Optional[QueryOptions] = None,
686687
auto_fetch_nodes: bool = False,
687688
auto_fetch_models: Optional[List[Union[str, Type["NodeModel"]]]] = None,
@@ -691,7 +692,7 @@ async def find_connected_nodes(
691692
692693
Args:
693694
filters (RelationshipPropertyFilters | None, optional): Expressions applied to the query. Defaults to `None`.
694-
projections (Dict[str, str], optional): The properties to project from the node. A invalid or empty
695+
projections (Projection, optional): The properties to project from the node. A invalid or empty
695696
projection will result in the whole model instances being returned. Defaults to `None`.
696697
options (QueryOptions | None, optional): Options for modifying the query result. Defaults to `None`.
697698
auto_fetch_nodes (bool, optional): Whether to automatically fetch connected nodes. Takes priority over the

pyneo4j_ogm/queries/query_builder.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pyneo4j_ogm.queries.types import (
1212
MultiHopFilters,
1313
NodeFilters,
14+
Projection,
1415
QueryOptions,
1516
RelationshipFilters,
1617
RelationshipMatchDirection,
@@ -367,15 +368,13 @@ def relationship_match(
367368
case _:
368369
raise InvalidRelationshipDirection(direction)
369370

370-
def build_projections(
371-
self, projections: Dict[str, Union[str, Literal["$elementId"], Literal["$id"]]], ref: str = "n"
372-
) -> None:
371+
def build_projections(self, projections: Projection, ref: str = "n") -> None:
373372
"""
374373
Builds a projection which only returns the node properties defined in the projection.
375374
376375
Args:
377376
ref (str): The reference to the node. Defaults to `'n'`.
378-
projections (Dict[str, str]): The projections to build.
377+
projections (Projection): The projections to build.
379378
"""
380379
if not isinstance(projections, dict):
381380
return

pyneo4j_ogm/queries/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,7 @@ class QueryOptions(TypedDict, total=False):
196196
skip: Optional[int]
197197
sort: Optional[Union[List[str], str]]
198198
order: Optional[QueryOptionsOrder]
199+
200+
201+
# Interface for a projection
202+
Projection = Dict[str, Union[str, Literal["$elementId"], Literal["$id"]]]

0 commit comments

Comments
 (0)