Skip to content

Commit a94b3fe

Browse files
author
matmoncon
committed
docs: update code comments
1 parent cb567dd commit a94b3fe

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

README.md

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ from uuid import UUID, uuid4
6262

6363
class Developer(NodeModel):
6464
"""
65-
This class represents a `Developer` node inside the graph. All interactions with nodes of
66-
this type will be handled by this class.
65+
This class represents a `Developer` node inside the graph. All interaction
66+
with nodes of this type will be handled by this class.
6767
"""
6868
uid: WithOptions(UUID, unique=True) = Field(default_factory=uuid4)
6969
name: str
@@ -86,8 +86,8 @@ class Developer(NodeModel):
8686

8787
class Coffee(NodeModel):
8888
"""
89-
This class represents a node with the labels `Beverage` and `Hot`. Notice that the labels
90-
of this model are explicitly defined in the `Settings` class.
89+
This class represents a node with the labels `Beverage` and `Hot`. Notice
90+
that the labels of this model are explicitly defined in the `Settings` class.
9191
"""
9292
flavor: str
9393
sugar: bool
@@ -106,12 +106,13 @@ class Coffee(NodeModel):
106106

107107
class Consumed(RelationshipModel):
108108
"""
109-
Unlike the models above, this class represents a relationship between two nodes. In this case,
110-
it represents the relationship between the `Developer` and `Coffee` models. Like with node-models,
111-
the `Settings` class allows us to define some settings for this relationship.
109+
Unlike the models above, this class represents a relationship between two
110+
nodes. In this case, it represents the relationship between the `Developer`
111+
and `Coffee` models. Like with node-models, the `Settings` class allows us to
112+
define some settings for this relationship.
112113
113-
Note that the relationship itself does not define it's start- and end-nodes, making it reusable
114-
for other models as well.
114+
Note that the relationship itself does not define it's start- and end-nodes,
115+
making it reusable for other models as well.
115116
"""
116117
liked: bool
117118

@@ -122,7 +123,7 @@ class Consumed(RelationshipModel):
122123
The models above are pretty straight forward. They are basically just `Pydantic` models with some sugar on top, though there are some special things to note:
123124

124125
- We are defining some model-specific settings inside the `Settings` class. These settings are used by `pyneo4j-ogm` to determine how to handle the model. For example, the `labels` setting of the `Coffee` model tells `pyneo4j-ogm` that this model should have the labels `Beverage` and `Hot` inside the graph. The `type` setting of the `Consumed` model tells `pyneo4j-ogm` that this relationship should have the type `CHUGGED` inside the graph.
125-
- We are defining a `post_hook` for the `coffee` relationship of the `Developer` model. This hook will be called whenever a `Coffee` node is connected to a `Developer` node via the `coffee` relationship.
126+
- We are defining a `post_hook` for the `coffee` relationship of the `Developer` model. This hook will be called whenever a `Coffee` node is connected to a `Developer` node via the `coffee` relationship-property.
126127
- We are defining a `uniqueness constraint` for the `uid` field of the `Developer` model. This will create a uniqueness constraint inside the graph for the `uid` field of the `Developer` model. This means that there can only be one `Developer` node with a specific `uid` inside the graph.
127128

128129
Now that we have our models defined, we can initialize a `Pyneo4jClient` instance that will be used to interact with the database. The client will handle most of the heavy lifting for us and our models, so let's initialize a new one and connect to the database:
@@ -133,14 +134,13 @@ from pyneo4j_ogm import Pyneo4jClient
133134
async def main():
134135
# We initialize a new `Pyneo4jClient` instance and connect to the database.
135136
client = Pyneo4jClient()
136-
await client.connect(### Logging <a name="logging"></a>
137-
138-
You can control the log level and whether to log to the console or not by setting the `PYNEO4J_OGM_LOG_LEVEL` and `PYNEO4J_OGM_ENABLE_LOGGING` as environment variables. The available levels are the same as provided by the build-in `logging` module. The default log level is `WARNING` and logging to the console is enabled by default.
139-
uri="<connection-uri-to-database>", auth=("<username>", "<password>"))
137+
await client.connect(uri="<connection-uri-to-database>", auth=("<username>", "<password>"))
140138

141-
# To use our models for running queries later on, we have to register them with the client.
142-
# **Note**: You only have to register the models that you want to use for queries and you can
143-
# even skip this step if you want to use the `Pyneo4jClient` instance for running raw queries.
139+
# To use our models for running queries later on, we have to register
140+
# them with the client.
141+
# **Note**: You only have to register the models that you want to use
142+
# for queries and you can even skip this step if you want to use the
143+
# `Pyneo4jClient` instance for running raw queries.
144144
await client.register_models([Developer, Coffee, Consumed])
145145
```
146146

@@ -159,8 +159,8 @@ async def main():
159159
cappuccino = Coffee(flavor="Cappuccino", milk=True, sugar=False)
160160
await cappuccino.create()
161161

162-
# Here we create a new relationship between `john` and his `cappuccino`. Additionally, we
163-
# set the `liked` property of the relationship to `True`.
162+
# Here we create a new relationship between `john` and his `cappuccino`.
163+
# Additionally, we set the `liked` property of the relationship to `True`.
164164
await john.coffee.connect(cappuccino, {"liked": True}) # Will print `John chugged another one!`
165165
```
166166

@@ -184,8 +184,8 @@ from uuid import UUID, uuid4
184184

185185
class Developer(NodeModel):
186186
"""
187-
This class represents a `Developer` node inside the graph. All interactions with nodes of
188-
this type will be handled by this class.
187+
This class represents a `Developer` node inside the graph. All interaction
188+
with nodes of this type will be handled by this class.
189189
"""
190190
uid: WithOptions(UUID, unique=True) = Field(default_factory=uuid4)
191191
name: str
@@ -196,40 +196,45 @@ class Developer(NodeModel):
196196
relationship_model="Consumed",
197197
direction=RelationshipPropertyDirection.OUTGOING,
198198
cardinality=RelationshipPropertyCardinality.ZERO_OR_MORE,
199-
allow_multiple=False,
199+
allow_multiple=True,
200200
)
201201

202202
class Settings:
203+
# Hooks are available for all methods that interact with the database.
203204
post_hooks = {
204205
"coffee.connect": lambda self, *args, **kwargs: print(f"{self.name} chugged another one!")
205206
}
206207

207208

208209
class Coffee(NodeModel):
209210
"""
210-
This class represents a node with the labels `Beverage` and `Hot`. Notice that the labels
211-
of this model are explicitly defined in the `Settings` class.
211+
This class represents a node with the labels `Beverage` and `Hot`. Notice
212+
that the labels of this model are explicitly defined in the `Settings` class.
212213
"""
213214
flavor: str
214215
sugar: bool
215216
milk: bool
216217

217218
developers: RelationshipProperty["Developer", "Consumed"] = RelationshipProperty(
218-
target_model="Developer",
219+
target_model=Developer,
219220
relationship_model="Consumed",
220221
direction=RelationshipPropertyDirection.INCOMING,
221222
cardinality=RelationshipPropertyCardinality.ZERO_OR_MORE,
222-
allow_multiple=False,
223+
allow_multiple=True,
223224
)
224225

225226
class Settings:
226227
labels = {"Beverage", "Hot"}
227228

228229
class Consumed(RelationshipModel):
229230
"""
230-
Unlike the models above, this class represents a relationship between two nodes. In this case,
231-
it represents the relationship between the `Developer` and `Coffee` models. Like with node-models,
232-
the `Settings` class allows us to define some settings for this relationship.
231+
Unlike the models above, this class represents a relationship between two
232+
nodes. In this case, it represents the relationship between the `Developer`
233+
and `Coffee` models. Like with node-models, the `Settings` class allows us to
234+
define some settings for this relationship.
235+
236+
Note that the relationship itself does not define it's start- and end-nodes,
237+
making it reusable for other models as well.
233238
"""
234239
liked: bool
235240

@@ -242,9 +247,11 @@ async def main():
242247
client = Pyneo4jClient()
243248
await client.connect(uri="<connection-uri-to-database>", auth=("<username>", "<password>"))
244249

245-
# To use our models for running queries later on, we have to register them with the client.
246-
# **Note**: You only have to register the models that you want to use for queries and you can
247-
# even skip this step if you want to use the `Pyneo4jClient` instance for running raw queries.
250+
# To use our models for running queries later on, we have to register
251+
# them with the client.
252+
# **Note**: You only have to register the models that you want to use
253+
# for queries and you can even skip this step if you want to use the
254+
# `Pyneo4jClient` instance for running raw queries.
248255
await client.register_models([Developer, Coffee, Consumed])
249256

250257
# We create a new `Developer` node and the `Coffee` he is going to drink.
@@ -254,8 +261,8 @@ async def main():
254261
cappuccino = Coffee(flavor="Cappuccino", milk=True, sugar=False)
255262
await cappuccino.create()
256263

257-
# Here we create a new relationship between `john` and his `cappuccino`. Additionally, we
258-
# set the `liked` property of the relationship to `True`.
264+
# Here we create a new relationship between `john` and his `cappuccino`.
265+
# Additionally, we set the `liked` property of the relationship to `True`.
259266
await john.coffee.connect(cappuccino, {"liked": True}) # Will print `John chugged another one!`
260267

261268
# Be a good boy and close your connections after you are done.

0 commit comments

Comments
 (0)