Skip to content

edgeql-queries

Simple EdgeQL in Python.

Tests Styles Coverage Code Style WPS Linter License Package version


Warning

This library is deprecated, prefer the official code generators from EdgeDB instead.

Introduction

edgeql-queries is a library that allows you to store your EdgeQL queries in separate files and then execute them like normal Python functions. This way you can control versions of the queries code, as with any other languages, but use it in Python applications.

Requirements

edgeql-queries requires only the EdgeDB driver for Python.

Installation

You can install edgeql-queries using pip:

pip install edgeql-queries

Or if you are using poetry:

poetry install edgeql-queries

Example

There is a more complex example based on the EdgeDB tutorial in the example folder in the repository. You can look there to see more features.

Here is a simplified version:

  1. Let's assume that we have the following schema in our already configured database:

    module default {
        type Person {
            required property first_name -> str;
            required property last_name -> str;
        }
    }
    

  2. We'll write our queries in the queries.edgeql:

    # name: select-users-by-last-name
    # Select all users that have same last name.
    SELECT Person {
        first_name,
    }
    FILTER .last_name = <str>$last_name
    
    # name: select-user-by-id!
    # Select single user's last name by it's id.
    SELECT Person {
        last_name
    }
    FILTER .id = <uuid>$user_id
    
    # name: create-keanu-reeves*
    # Create new user.
    INSERT Person {
        first_name := "Keanu",
        last_name := "Reeves",
    }
    

  3. Finally, we'll write our Python code:

    import edgedb
    import edgeql_queries
    
    queries = edgeql_queries.from_path("./queries.edgeql", async_driver=False)
    
    conn = edgedb.connect()
    
    # create Keanu
    queries.create_keanu_reeves(conn)
    
    # query all Keanu from database
    keanu_set = queries.select_users_by_last_name(conn, last_name="Reeves")
    
    for keanu in keanu_set:
        keanu_from_db = queries.select_user_by_id(conn, user_id=keanu.id)
        print(f"{keanu.first_name} {keanu_from_db.last_name}: {keanu_from_db.id}")
    

Credits

This project is inspired by aiosql project and is based on it's source code.

License

This project is licensed under the terms of the FreeBSD license.