Skip to content

Commit 7cc252a

Browse files
committed
Updates documentation on usage of enums.
1 parent 9ee5de4 commit 7cc252a

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

docs/how-to-guides/ogm.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,57 @@ To check which constraints have been created, run:
395395
print(db.get_constraints())
396396
```
397397

398+
## Using enums
399+
400+
Memgraph's built-in [enum data type](https://memgraph.com/docs/fundamentals/data-types#enum) can be utilized on your GQLAlchemy OGM models. GQLAlchemy's enum implementation extends Python's [enum support](https://docs.python.org/3.11/library/enum.html).
401+
402+
First, create an enum.
403+
404+
```python
405+
from enum import Enum
406+
407+
class SubscriptionType(Enum):
408+
FREE = 1
409+
BASIC = 2
410+
EXTENDED = 3
411+
```
412+
413+
Then, use the defined enum class in your model definition. Using the `Field` class, set the `enum` attribute to `True`. This will indicate that GQLAlchemy should treat the property value stored as a Memgraph enum. If the enum does not exist in the database, it will be created.
414+
415+
```python
416+
class User(Node):
417+
id: str = Field(index=True, db=db)
418+
username: str
419+
subscription: SubscriptionType = Field(enum=True, db=db)
420+
```
421+
422+
Enum types may be defined for properties on Nodes and Relationships.
423+
424+
!!! info
425+
If the `Field` class specification on the property isn't specified, or if `enum` is explicitly set to `False`, GQLAlchemy will use the `value` of the enum member when serializing to a Cypher query. A corresponding enum will not be created in the database.
426+
427+
This functionality allows for flexiblity when using the Python `Enum` class, and would, for instance, respect an overridden `__getattribute__` method to customize the value passed to Cypher.
428+
398429
## Full code example
399430

400431
The above mentioned examples can be merged into a working code example which you can run. Here is the code:
401432

402433
```python
403434
from gqlalchemy import Memgraph, Node, Relationship, Field
404435
from typing import Optional
436+
from enum import Enum
405437

406438
db = Memgraph()
407439

440+
class SubscriptionType(Enum):
441+
FREE = 1
442+
BASIC = 2
443+
EXTENDED = 3
444+
408445
class User(Node):
409446
id: str = Field(index=True, db=db)
410447
username: str = Field(exists=True, db=db)
448+
subscription: SubscriptionType = Field(enum=True, db=db)
411449

412450
class Streamer(User):
413451
id: str
@@ -423,8 +461,8 @@ class ChatsWith(Relationship, type="CHATS_WITH"):
423461
class Speaks(Relationship, type="SPEAKS"):
424462
since: Optional[str]
425463

426-
john = User(id="1", username="John").save(db)
427-
jane = Streamer(id="2", username="janedoe", followers=111).save(db)
464+
john = User(id="1", username="John", subscription=SubscriptionType(1)).save(db)
465+
jane = Streamer(id="2", username="janedoe", subscription=SubscriptionType(3), followers=111).save(db)
428466
language = Language(name="en").save(db)
429467

430468
ChatsWith(
@@ -449,7 +487,7 @@ try:
449487
streamer = Streamer(id="3").load(db=db)
450488
except:
451489
print("Creating new Streamer node in the database.")
452-
streamer = Streamer(id="3", username="anne", followers=222).save(db=db)
490+
streamer = Streamer(id="3", username="anne", subscription=SubscriptionType(2), followers=222).save(db=db)
453491

454492
try:
455493
speaks = Speaks(_start_node_id=streamer._id, _end_node_id=language._id).load(db)

0 commit comments

Comments
 (0)