Search code examples
memgraphdb

How to use on-disk storage with Memgraph and GQLAlchemy?


I am working on a project where I need to use Memgraph along with GQLAlchemy. I understand that Memgraph has a feature for on-disk storage, which allows users to store data on the disk rather than in-memory. I order to use this feature I must use GQLAlchemy.

How do I configure Memgraph to enable on-disk storage? Are there any specific settings or parameters to be aware of while working with on-disk storage in Memgraph?


Solution

  • As an in-memory graph database, Memgraph utilizes the GQLAlchemy library to offer an on-disk storage alternative for sizable properties that are not required for graph algorithms. This approach is beneficial when nodes or relationships contain metadata that is unnecessary for the graph algorithms executed in Memgraph but can be retrieved later. Just do the following:

    Import the necessary libraries and connect to the running Memgraph and SQL database instances:

    from gqlalchemy import Memgraph, SQLitePropertyDatabase, Node, Field
    from typing import Optional
    
    graphdb = Memgraph()
    SQLitePropertyDatabase('path-to-my-db.db', graphdb)
    

    graphdb creates a connection to an in-memory graph database, and SQLitePropertyDatabase attaches to graphdb in its constructor.

    Create the User class, which maps to a node object in the graph database.

    class User(Node):
        id: int = Field(unique=True, exists=True, index=True, db=graphdb)
        huge_string: Optional[str] = Field(on_disk=True)
    

    The id property is a required integer that creates uniqueness and existence constraints inside Memgraph. Notice that the id property is also indexed on the label User. The huge_string property is optional, and because the on_disk argument is set to True, it will be saved into the SQLite database.

    Create a large string, which won't be saved into the graph database, but rather into the SQLite database.

    my_secret = "I LOVE DUCKS" * 1000
    john = User(id=5, huge_string=my_secret).save(db)
    john2 = User(id=5).load(db)