TypeDBInterface
- class typedb_utils.typedb_interface.TypeDBInterface(address, database_name, schema_path=None, data_path=None, force_database=False, force_data=False, username='admin', password='password')
Class used to interact with TypeDB 3 databases.
Connect to a TypeDB server and initialise the database.
- Parameters:
address (
str) – TypeDB server address.database_name (
str) – database name.schema_path (
Union[list[str],str,None]) – list of paths to schema files (.tql).data_path (
Union[list[str],str,None]) – list of paths to data files (.tql).force_database (
bool) – delete and recreate the database if it exists.force_data (
bool) – clear all data before loading data_path files.username (
str) – TypeDB username (default: ‘admin’).password (
str) – TypeDB password (default: ‘password’).
- Returns:
None.
Methods
__init__(address, database_name[, ...])Connect to a TypeDB server and initialise the database.
connect_driver(address[, username, password])Connect to TypeDB server.
create_database(database_name[, force])Create database.
database_exists([database_name])Check if database exists.
database_query(session_type, ...)Execute a query against the database.
Delete all data from the database (entities and relations).
delete_attribute_from_thing(thing, key, ...)Delete an attribute from a thing matched by a key attribute.
delete_attributes_from_thing(thing, key, ...)Delete multiple attributes from a thing matched by a key attribute.
Delete data event hook.
delete_database([database_name])Delete database.
delete_from_database(query)Perform delete query.
delete_thing(thing, key, key_value)Delete a thing from the database by its key attribute.
fetch_attribute_from_thing(thing, ...)Fetch attribute values from a thing matched by one or more key attributes.
fetch_attribute_from_thing_raw(thing, ...)Fetch raw normalised attribute dicts from a thing matched by key attributes.
fetch_database(query)Perform fetch query.
get_aggregate_database(query)Perform get aggregate query.
get_database(query)Perform get query.
insert_attribute_in_thing(thing, key, ...)Insert an attribute into a thing matched by a key attribute.
insert_attributes_in_thing(thing, key, ...)Insert multiple attributes into a thing matched by a key attribute.
Insert data event hook.
insert_database(query)Perform insert query.
insert_entity(entity[, attribute_list])Insert an entity into the database with optional attributes.
insert_relationship(relationship, related_dict)Insert a relationship between existing things.
load_data(data_path)Load a .tql data file into the database.
load_schema(schema_path)Load a .tql schema file into the database.
update_attribute_in_thing(thing, key, ...)Update an attribute in a thing by deleting the old value and inserting the new one.
update_attributes_in_thing(thing, key, ...)Update multiple attributes in a thing.
update_database(query)Perform update query.
write_database_file(query_type, file_path)Write a .tql file to the database.
- __init__(address, database_name, schema_path=None, data_path=None, force_database=False, force_data=False, username='admin', password='password')
Connect to a TypeDB server and initialise the database.
- Parameters:
address (
str) – TypeDB server address.database_name (
str) – database name.schema_path (
Union[list[str],str,None]) – list of paths to schema files (.tql).data_path (
Union[list[str],str,None]) – list of paths to data files (.tql).force_database (
bool) – delete and recreate the database if it exists.force_data (
bool) – clear all data before loading data_path files.username (
str) – TypeDB username (default: ‘admin’).password (
str) – TypeDB password (default: ‘password’).
- Returns:
None.
- connect_driver(address, username='admin', password='password')
Connect to TypeDB server.
- create_database(database_name, force=False)
Create database.
- database_exists(database_name=None)
Check if database exists.
- database_query(session_type, transaction_type, query_type, query)
Execute a query against the database.
- Parameters:
session_type (
str) – ‘schema’ or ‘data’ (used to select transaction type).transaction_type (
str) – ‘read’ or ‘write’.query_type (
Literal['define','insert','delete','fetch','get','get_aggregate','update']) – one of define/insert/delete/fetch/get/get_aggregate/update.query (
str) – TypeQL query string.
- Return type:
Union[Literal[True],list[dict[str,Any]],None,int,float]- Returns:
query result.
- Raises:
ValueError – If a provided query/session/transaction type is unsupported.
TypeDBQueryError – If query execution fails in the database driver.
- delete_all_data()
Delete all data from the database (entities and relations).
- Return type:
- Returns:
None.
- delete_attribute_from_thing(thing, key, key_value, attr)
Delete an attribute from a thing matched by a key attribute.
- delete_attributes_from_thing(thing, key, key_value, attr_list)
Delete multiple attributes from a thing matched by a key attribute.
- delete_data_event()
Delete data event hook.
Override in subclasses to react when delete operations occur.
- Returns:
None.
- delete_database(database_name=None)
Delete database.
- delete_from_database(query)
Perform delete query.
- delete_thing(thing, key, key_value)
Delete a thing from the database by its key attribute.
- fetch_attribute_from_thing(thing, key_attr_list, attr)
Fetch attribute values from a thing matched by one or more key attributes.
- fetch_attribute_from_thing_raw(thing, key_attr_list, attr)
Fetch raw normalised attribute dicts from a thing matched by key attributes.
- fetch_database(query)
Perform fetch query.
- Parameters:
query (
str) – TypeQL fetch query.- Return type:
- Returns:
list of result dicts.
- get_aggregate_database(query)
Perform get aggregate query.
- get_database(query)
Perform get query.
- Parameters:
query (
str) – TypeQL get query.- Return type:
- Returns:
list of result dicts.
- insert_attribute_in_thing(thing, key, key_value, attr, attr_value)
Insert an attribute into a thing matched by a key attribute.
- insert_attributes_in_thing(thing, key, key_value, attribute_list)
Insert multiple attributes into a thing matched by a key attribute.
- insert_data_event()
Insert data event hook.
Override in subclasses to react when insert operations occur.
- Returns:
None.
- insert_database(query)
Perform insert query.
- insert_entity(entity, attribute_list=None)
Insert an entity into the database with optional attributes.
- insert_relationship(relationship, related_dict, attribute_list=None)
Insert a relationship between existing things.
- Parameters:
- Return type:
Literal[True]- Returns:
True on success.
- load_data(data_path)
Load a .tql data file into the database.
Files may contain multiple statements (insert or match…insert blocks). Each statement is executed as a separate transaction so that later statements can match entities created by earlier ones.
- Parameters:
data_path (
str) – path to .tql file.- Return type:
- Returns:
None.
- Raises:
TypeDBQueryError – If any statement fails during execution.
- load_schema(schema_path)
Load a .tql schema file into the database.
- update_attribute_in_thing(thing, key, key_value, attr, attr_value)
Update an attribute in a thing by deleting the old value and inserting the new one.
- update_attributes_in_thing(thing, key, key_value, attribute_list)
Update multiple attributes in a thing.
- update_database(query)
Perform update query.