neo4j-python-driver icon indicating copy to clipboard operation
neo4j-python-driver copied to clipboard

Introduce API Redesign

Open bigmontz opened this issue 3 years ago • 1 comments

  • [x] Query API
  • [x] Execute API
  • [x] Automatic Access Mode
  • [x] Async
  • [x] Docs

bigmontz avatar Jul 20 '22 11:07 bigmontz

Usage example:

from importlib.metadata import metadata
import neo4j
import asyncio

from neo4j.api import CLUSTER_READERS_ACCESS, CLUSTER_WRITERS_ACCESS


async def main():
    driver = neo4j.AsyncGraphDatabase.driver(
        'bolt://localhost', auth=neo4j.basic_auth("neo4j", "pass"))

    async with driver.session() as session:
        async def txfunc(tx):
            query_result = await tx.query('RETURN $n as n', n=1)
            print(query_result.records)
            print(query_result.summary.database)

        await session.execute(txfunc)

    async with driver.session() as session:
        async def txfunc(tx):
            records, summary = await tx.query(
                'RETURN $n as n',
                n=2)
            print(records)
            print(summary.database)

        await session.execute(txfunc)

    async with driver.session() as session:
        records, summary = await session.query(
                'RETURN $n as n',
                n=3,
                skip_records=True,
                cluster_member_access=CLUSTER_WRITERS_ACCESS)
        print(records)
        print(summary.database)

    async with driver.session() as session:
        records, summary = await session.query(
                'RETURN $n as n',
                n=4)
        print(records)
        print(summary.database)

    async def txfunc(tx, n):
        records, summary = await tx.query(
                'RETURN $n as n',
                n=n)
        print(records)
        print(summary.database)

    # configuration are optional
    await driver.execute(
        lambda tx: txfunc(tx, n=5),
        database="neo4j",
        cluster_member_access=CLUSTER_WRITERS_ACCESS,
        timeout=10,
        metadata={"service": "python driver playground"}
    )

    records, summary = await driver.query(
      'RETURN $n as n',
      n=6,
      cluster_member_access=CLUSTER_READERS_ACCESS)

    print(records)
    print(summary.database)

    await driver.close()


asyncio.run(main())

bigmontz avatar Jul 21 '22 10:07 bigmontz

ADR (internal link) was abandoned.

driver.execute_query (https://github.com/neo4j/neo4j-python-driver/pull/833) is it's spiritual successor.

robsdedude avatar Nov 01 '22 16:11 robsdedude