Lesson 6: Building applications

Driver setup

  • The official TypeDB Python driver package can be installed with:

    pip install typedb-driver
  • The following classes must be imported in order to use the Python driver in any codebase:

    from typedb.driver import TypeDB, Credentials, DriverOptions

Managing databases

  • The TypeDB.driver method is used to instantiate a new driver object.

    driver: TypeDBDriver = TypeDB.driver(ADDRESS, credentials, options)
  • The TypeDBDriver.databases.create method is used to create a new database.

    driver.databases.create(DB)

Transactions

  • The TypeDBSession.transaction method is used to instantiate a new transaction. The transaction type is specified using the READ, WRITE, or SCHEMA value of the TransactionType enum.

    transaction: TypeDBTransaction = driver.transaction(DB, TransactionType.READ)
  • Drivers and transaction objects are ideally always be instantiated using a context manager so that their destructors are called automatically, otherwise call `.close() manually on them

    with TypeDB.driver(ADDRESS, credentials, options) as driver:
        with session.transaction(DB, TransactionType.READ) as transaction:
            # code goes here
  • The TypeDBTransaction.commit method is used to commit a transaction.

    transaction.commit()

Executing queries

  • The TypeDBTransaction.query() method is used to submit queries.

    results: Promise[QueryAnswer] = transaction.query(QUERY)
  • Typically you submit and resolve the promise catch any errors that are thrown

    transaction.query(QUERY).resolve()

Processing results

  • Each result of a query that ends in fetch returns Documents. The top-level dictionary has fields corresponding to the fetch clause structure.

  • If you know you aren’t catching errors at query time and don’t care about the returned values (such as when doing straight inserts), you can submit without resolving or reading answers to boost performance

Further learning

Learn how to build query patterns utilising advanced elements of TypeQL syntax, and how queries are resolved by TypeDB.

Explore the comprehensive API reference for the official TypeDB GRPC drivers.

Explore the comprehensive API reference for the official TypeDB HTTP drivers.

Explore the docs for all the official TypeDB drivers, including installation guides, tutorials, and API references.