Metadata-Version: 2.1
Name: gqlalchemy
Version: 1.2.0
Summary: GQLAlchemy is library developed with purpose of assisting writing and running queries on Memgraph.
Home-page: https://github.com/memgraph/gqlalchemy
License: Apache-2.0
Author: Jure Bajic
Author-email: jure.bajic@memgraph.com
Requires-Python: >=3.7,<4.0
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Requires-Dist: adlfs (>=2022.2.0,<2023.0.0)
Requires-Dist: dacite (>=1.6.0,<2.0.0)
Requires-Dist: docker (>=5.0.3,<6.0.0)
Requires-Dist: networkx (>=2.5.1,<3.0.0)
Requires-Dist: psutil (>=5.9.0,<6.0.0)
Requires-Dist: pyarrow (>=7.0.0,<8.0.0)
Requires-Dist: pydantic (>=1.8.2,<2.0.0)
Requires-Dist: pymgclient (==1.2.0)
Project-URL: Repository, https://github.com/memgraph/gqlalchemy
Description-Content-Type: text/markdown

# GQLAlchemy


<p>
    <a href="https://github.com/memgraph/gqlalchemy/actions"><img src="https://github.com/memgraph/gqlalchemy/workflows/Build%20and%20Test/badge.svg" /></a>
    <a href="https://github.com/memgraph/gqlalchemy/blob/main/LICENSE"><img src="https://img.shields.io/github/license/memgraph/gqlalchemy" /></a>
    <a href="https://pypi.org/project/gqlalchemy"><img src="https://img.shields.io/pypi/v/gqlalchemy" /></a>
    <a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
    <a href="https://memgraph.com/docs/gqlalchemy" alt="Documentation"><img src="https://img.shields.io/badge/documentation-GQLAlchemy-orange" /></a>
    <a href="https://github.com/memgraph/gqlalchemy/stargazers" alt="Stargazers"><img src="https://img.shields.io/github/stars/memgraph/gqlalchemy?style=social" /></a>
</p>

**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](https://pypi.org/project/pymgclient/) /
[Documentation](https://memgraph.github.io/pymgclient/) /
[GitHub](https://github.com/memgraph/pymgclient)).

## 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](https://cgold.readthedocs.io/en/latest/first-step/installation.html#).

To install `gqlalchemy`, simply run the following command:
```
pip install gqlalchemy
```

## Build & Test

The project uses [Poetry](https://python-poetry.org/) 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:

```python
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:

```python
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:

```python
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](https://memgraph.com/docs/gqlalchemy/).

The documentation can be generated by executing:
```
pip3 install python-markdown
python-markdown
```

## License

Copyright (c) 2016-2022 [Memgraph Ltd.](https://memgraph.com)

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.

