Lesson 6: Building applications
|
This documentation is not current - it is for TypeDB 2.x, which is no longer supported. View the documentation for TypeDB 3.x: |
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, SessionType, TransactionType from typedb.api.connection.credential import TypeDBCredential
Managing databases
-
The
TypeDB.core_drivermethod is used to instantiate a new driver object.driver: TypeDBDriver = TypeDB.core_driver(ADDRESS) -
The
TypeDBDriver.databasesproperty is used to access database management controls. -
The
TypeDBDriver.databases.createmethod is used to create a new database.driver.databases.create(DATABASE)
Sessions and transactions
-
The
TypeDBDriver.sessionmethod is used to instantiate a new session object. The session type is specified using theDATAandSCHEMAvalues of theSessionTypeenum.session: TypeDBSession = driver.session(DATABASE, SessionType.DATA) -
Schema sessions are locking: only one can be opened at any time per database, and data writes cannot be performed in any active session while a schema session is open.
-
The
TypeDBSession.transactionmethod is used to instantiate a new transaction object. The transaction type is specified using theREADandWRITEvalue of theTransactionTypeenum.transaction: TypeDBTransaction = session.transaction(TransactionType.READ) -
Driver, session, and transaction objects should always be instantiated using a context manager so that their destructors are called automatically.
with TypeDB.core_driver(ADDRESS) as driver: with driver.session(DATABASE, SessionType.DATA) as session: with session.transaction(TransactionType.READ) as transaction: # code goes here -
The
TypeDBTransaction.commitmethod is used to commit a transaction.transaction.commit()
Executing queries
-
The
TypeDBTransaction.queryproperty is used to access query execution methods. -
The
TypeDBTransaction.query.fetchmethod is used to execute Fetch queries. It returns an iterator of results.results: Iterator[Dict] = transaction.query.fetch(QUERY) -
The
TypeDBTransaction.query.insertmethod is used to execute Insert queries.transaction.query.insert(QUERY) -
The
TypeDBTransaction.query.deletemethod is used to execute Delete queries.transaction.query.delete(QUERY) -
The
TypeDBTransaction.query.updatemethod is used to execute Update queries.transaction.query.update(QUERY) -
The
TypeDBTransaction.query.definemethod is used to execute Define queries.transaction.query.define(QUERY)
Processing results
-
Each result of a Fetch query is returned as a JSON-style nested dictionary. The top-level dictionary has fields corresponding to the variables in the
fetchclause. The second-level dictionary has fields corresponding to the attributes of those variables, in addition to atypefield containing the type of the variable. -
Every attribute retrieved as part of a result has the following format:
{ 'type': { 'value_type': <attribute value type>, 'root': 'attribute', 'label': <attribute type label> }, 'value': <attribute value> } -
Every type retrieved as part of a result has the following format:
{ 'root': <'entity' or 'relation'>, 'label': <entity or relation type label> }