Skip to main content

GQLAlchemy is library developed with purpose of assisting writing and running queries on Memgraph.

Project description

GQLAlchemy

Code style: black

GQLAlchemy is a fully open-source Python library and Object Graph Mapper (OGM) - a link between graph database objects and Python objects.

An Object Graph Mapper or OGM provides a developer-friendly workflow that allows for writing object-oriented notation to communicate with graph databases. Instead of writing Cypher queries, you will be able to write object-oriented code, which the OGM will automatically translate into Cypher queries.

GQLAlchemy is built on top of Memgraph's low-level Python client pymgclient (PyPI / Documentation / GitHub).

Installation

Before you install gqlalchemy, make sure that you have cmake installed by running:

cmake --version

You can install cmake by following the official instructions.

To install gqlalchemy, simply run the following command:

pip install gqlalchemy

Build & Test

The project uses Poetry to build the GQLAlchemy Python library. To build and run tests, execute the following command: poetry install

Before starting the tests, make sure you have an active Memgraph instance running. Execute the following command: poetry run pytest .

GQLAlchemy example

When working with the gqlalchemy, a Python developer can connect to the database and execute a MATCH Cypher query using the following syntax:

from gqlalchemy import Memgraph

memgraph = Memgraph("127.0.0.1", 7687)
memgraph.execute("CREATE (:Node)-[:Connection]->(:Node)")
results = memgraph.execute_and_fetch("""
    MATCH (from:Node)-[:Connection]->(to:Node)
    RETURN from, to;
""")

for result in results:
    print(result['from'])
    print(result['to'])

Query builder example

As we can see, the example above can be error-prone, because we do not have abstractions for creating a database connection and MATCH query.

Now, rewrite the exact same query by using the functionality of GQLAlchemy's query builder:

from gqlalchemy import match, Memgraph

memgraph = Memgraph()

results = (
    match()
    .node("Node", variable="from")
    .to("Connection")
    .node("Node", variable="to")
    .return_()
    .execute()
)

for result in results:
    print(result["from"])
    print(result["to"])

An example using the Node and Relationship classes:

from gqlalchemy import Memgraph, Node, Relationship, match, Field

memgraph = Memgraph("127.0.0.1", 7687)


class User(Node):
    id: int = Field(index=True, exist=True, unique=True, db=memgraph)


class Follows(Relationship, type="FOLLOWS"):
    pass


u1 = User(id=1).save(memgraph)
u2 = User(id=2).save(memgraph)
r = Follows(_start_node_id=u1._id, _end_node_id=u2._id).save(memgraph)

result = list(
    match(memgraph.new_connection())
    .node(variable="a")
    .to(variable="r")
    .node(variable="b")
    .where("a.id", "=", u1.id)
    .or_where("b.id", "=", u2.id)
    .return_()
    .execute()
)[0]

print(result["a"])
print(result["b"])
print(result["r"])

Development (how to build)

poetry run flake8 .
poetry run black .
poetry run pytest . -k "not slow"

Documentation

The GQLAlchemy documentation is available on memgraph.com/docs/gqlalchemy.

The documentation can be generated by executing:

pip3 install python-markdown
python-markdown

License

Copyright (c) 2016-2022 Memgraph Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

GQLAlchemy-1.2.0.tar.gz (37.6 kB view hashes)

Uploaded Source

Built Distribution

GQLAlchemy-1.2.0-py3-none-any.whl (42.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page