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, SessionType, TransactionType from typedb.api.connection.credential import TypeDBCredential
Managing databases
-
The
TypeDB.core_driver
method is used to instantiate a new driver object.driver: TypeDBDriver = TypeDB.core_driver(ADDRESS)
-
The
TypeDBDriver.databases
property is used to access database management controls. -
The
TypeDBDriver.databases.create
method is used to create a new database.driver.databases.create(DATABASE)
Sessions and transactions
-
The
TypeDBDriver.session
method is used to instantiate a new session object. The session type is specified using theDATA
andSCHEMA
values of theSessionType
enum.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.transaction
method is used to instantiate a new transaction object. The transaction type is specified using theREAD
andWRITE
value of theTransactionType
enum.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.commit
method is used to commit a transaction.transaction.commit()
Executing queries
-
The
TypeDBTransaction.query
property is used to access query execution methods. -
The
TypeDBTransaction.query.fetch
method is used to execute Fetch queries. It returns an iterator of results.results: Iterator[Dict] = transaction.query.fetch(QUERY)
-
The
TypeDBTransaction.query.insert
method is used to execute Insert queries.transaction.query.insert(QUERY)
-
The
TypeDBTransaction.query.delete
method is used to execute Delete queries.transaction.query.delete(QUERY)
-
The
TypeDBTransaction.query.update
method is used to execute Update queries.transaction.query.update(QUERY)
-
The
TypeDBTransaction.query.define
method 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
fetch
clause. The second-level dictionary has fields corresponding to the attributes of those variables, in addition to atype
field 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> }