Working with EdgeDB from python

from contextlib import contextmanager
import datetime
import edgedb


@contextmanager
def sync_con():
    # Establish a connection to an existing database
    # named "test" as an "edgedb" user.
    con = edgedb.connect('edgedb://edgedb@localhost/test')
    try:
        yield con
    finally:
        con.close()


def create():
    qry = '''
        START TRANSACTION;
        CREATE MIGRATION users TO {
            type User {
                required property name -> str;
                property dob -> local_date;
                multi link friends -> User;
            }
        };
        COMMIT MIGRATION users;
        COMMIT;
    '''
    with sync_con() as con:
        con.execute(qry)


def fill_db():
    qry = '''
        START TRANSACTION;
        CREATE MIGRATION users TO {
            type User {
                required property name -> str;
                property dob -> local_date;
                multi link friends -> User;
            }
        };
        COMMIT MIGRATION users;
        COMMIT;
    '''
    con.fetchall('''
        INSERT User {
            name := <str>$name,
            dob := <local_date>$dob
        }
    ''', name='Bob', dob=datetime.date(1984, 3, 1))


# def sync_select


def main():
    # with sync_con() as con:
    #     pass
    # fill_db()
    create()

    return
    # Select User objects.
    user_set = conn.fetchall(
        'SELECT User {name, dob} FILTER .name = <str>$name',
        name='Bob')

    # *user_set* now contains
    # Set{Object{name := 'Bob',
    #            dob := datetime.date(1984, 3, 1)}}
    print(user_set)

    # Close the connection.
    conn.close()

if __name__ == '__main__':
    main()