Skip to content

Commit d1b6872

Browse files
author
matmoncon
committed
docs: add comments to db client
1 parent b8c479d commit d1b6872

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

pyneo4j_ogm/core/client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Pyneo4j database client class for running operations on the database.
33
"""
4+
45
import os
56
from enum import Enum
67
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union, cast
@@ -123,6 +124,7 @@ async def connect(
123124
logger.debug("Connecting to database %s", self.uri)
124125
self._driver = AsyncGraphDatabase.driver(uri=self.uri, *args, **kwargs)
125126

127+
# Get server info to check the Neo4j version since versions prior to 5 are not tested yet
126128
logger.debug("Checking if neo4j version is supported")
127129
server_info = await self._driver.get_server_info()
128130

@@ -150,6 +152,9 @@ async def register_models(self, models: List[Type[Union[NodeModel, RelationshipM
150152

151153
for model in models:
152154
if issubclass(model, (NodeModel, RelationshipModel)):
155+
logger.debug("Found valid mode %s, registering with client", model.__name__)
156+
157+
# If the model is a valid model, add it to the set of models stored by the client
153158
self.models.add(model)
154159
setattr(model, "_client", self)
155160

@@ -161,6 +166,7 @@ async def register_models(self, models: List[Type[Union[NodeModel, RelationshipM
161166
else getattr(model._settings, "type")
162167
)
163168

169+
# Check if we need to create any constraints
164170
if not self._skip_constraints:
165171
if getattr(get_field_type(property_definition), "_unique", False):
166172
await self.create_uniqueness_constraint(
@@ -170,6 +176,7 @@ async def register_models(self, models: List[Type[Union[NodeModel, RelationshipM
170176
labels_or_type=labels_or_type,
171177
)
172178

179+
# Check if we need to create any indexes
173180
if not self._skip_indexes:
174181
if getattr(get_field_type(property_definition), "_range_index", False):
175182
await self.create_range_index(
@@ -231,11 +238,13 @@ async def cypher(
231238

232239
logger.debug("Checking for open transaction")
233240
if getattr(self, "_session", None) is None or getattr(self, "_transaction", None) is None:
241+
# Begin a new transaction if none is open
234242
await self._begin_transaction()
235243

236244
try:
237245
parameters = parameters if parameters is not None else {}
238246

247+
# Run the query and get the results and result keys used in the query
239248
logger.debug("Running query \n%s \nwith parameters %s", query, parameters)
240249
result_data = await cast(AsyncTransaction, self._transaction).run(
241250
query=cast(LiteralString, query), parameters=parameters
@@ -245,6 +254,8 @@ async def cypher(
245254
meta = list(result_data.keys())
246255

247256
if resolve_models:
257+
# If model resolution has been enabled, try to resolve the query results
258+
# If this fails, the raw result will be returned instead
248259
logger.debug("`resolve_models` is set to True, trying to resolve query results")
249260
for list_index, result_list in enumerate(results):
250261
for result_index, result in enumerate(result_list):
@@ -254,13 +265,16 @@ async def cypher(
254265
results[list_index][result_index] = resolved
255266

256267
if self._batch_enabled is False:
268+
# If batching is enabled, we don't want to commit the transaction yet as
269+
# we might have more queries to run
257270
logger.debug("No batching enabled, committing transaction")
258271
await self._commit_transaction()
259272

260273
return results, meta
261274
except Exception as exc:
262275
logger.error("Error running query %s", exc)
263276
if self._batch_enabled is False:
277+
# The same goes for rolling back the transaction when batching is enabled
264278
await self._rollback_transaction()
265279

266280
raise exc

0 commit comments

Comments
 (0)