The official TerminusDB Python client library for data science and backend applications. Integrate graph database capabilities into your Python workflows.
Connect to a Terminus server at the given URI with an API key.
Parameters
team
str
Name of the team, default to be "admin"
db
optional, str
Name of the database connected
remote_auth
optional, dict
Remote Auth setting
key
optional, str
API key for connecting, default to be "root"
user
optional, str
Name of the user, default to be "admin"
use_token
bool
Use token to connect. If both `jwt_token` and `api_token` is not provided (None), then it will use the ENV variable TERMINUSDB_ACCESS_TOKEN to connect as the API token
jwt_token
optional, str
The Bearer JWT token to connect. Default to be None.
Create a TerminusDB database by posting
a terminus:Database document to the Terminus Server.
Parameters
dbid
str
Unique identifier of the database.
team
str, optional
ID of the Team in which to create the DB (defaults to 'admin')
label
str, optional
Database name.
description
str, optional
Database description.
prefixes
dict, optional
Optional dict containing ``"@base"`` and ``"@schema"`` keys.
@base (str)
IRI to use when ``doc:`` prefixes are expanded. Defaults to ``terminusdb:///data``.
@schema (str)
IRI to use when ``scm:`` prefixes are expanded. Defaults to ``terminusdb:///schema``.
include_schema
bool
If ``True``, a main schema graph will be created, otherwise only a main instance graph will be created.
Graph type, either GraphType.INSTANCE or GraphType.SCHEMA.
skip
int
The starting posiion of the returning results, default to be 0
count
int or None
The maximum number of returned result, if None (default) it will return all of the avalible result.
as_list
bool
If the result returned as list rather than an iterator.
get_data_version
bool
If the version of the document(s) should be obtained. If True, the method return the result and the version as a tuple.
kwargs
Additional boolean flags for retriving. Currently avaliable: "prefixed", "unfold"
Returns
iterable
Stream of dictionaries
get_available_roles()
Get the available roles for the current authenticated user
Returns
dict or None if failed
get_class_frame(class_name)
Get the frame of the class of class_name. Provide information about all the avaliable properties of that class.
Parameters
class_name
str
Name of the class
Returns
dict
Dictionary containing information
get_commit_history(max_history)
Get the whole commit history.
Commit history - Commit id, author of the commit, commit message and the commit time, in the current branch from the current commit, ordered backwards in time, will be returned in a dictionary in the follow format:
```
{ "commit_id":
{ "author": "commit_author",
"message": "commit_message",
"timestamp: <datetime object of the timestamp>"
}
}
```
Parameters
max_history
int, optional
maximum number of commit that would return, counting backwards from your current commit. Default is set to 500. It needs to be nop-negative, if input is 0 it will still give the last commit.
Returns
list
get_database()
Returns metadata (id, organization, label, comment) about the requested database
Parameters
----------
dbid : str
The id of the database
team : str
The organization of the database (default self.team)
Returns
dict
get_databases()
Returns a list of database metadata records for all databases the user has access to
Graph type, either GraphType.INSTANCE or GraphType.SCHEMA.
full_replace
bool
If True then the whole graph will be replaced. WARNING: you should also supply the context object as the first element in the list of documents if using this option.
commit_msg
str
Commit message.
last_data_version
str
Last version before the update, used to check if the document has been changed unknowingly
compress
str or int
If it is an integer, size of the data larger than this (in bytes) will be compress with gzip in the request (assume encoding as UTF-8, 0 = always compress). If it is `never` it will never compress the data.
raw_json
bool
Update as raw json
Returns
list
list of ids of the inseted docuemnts
insert_triples(graph_type, content, commit_msg)
Inserts into the specified graph with the triples encoded in turtle format.
Parameters
graph_type
GraphType
Graph type, either GraphType.INSTANCE or GraphType.SCHEMA.
content
Valid set of triples in Turtle or Trig format.
commit_msg
str
Commit message.
list_databases()
Returns a list of database ids for all databases the user has access to
Returns
list of dicts
log()
Get commit history of a database
Parameters
----------
team : str, optional
The team from which the database is. Defaults to the class property.
db : str, optional
The database. Defaults to the class property.
start : int, optional
Commit index to start from. Defaults to 0.
count : int, optional
Amount of commits to get. Defaults to -1 which gets all.
Returns
list
List of the following commit objects:
```
{
"@id":"InitialCommit/hpl18q42dbnab4vzq8me4bg1xn8p2a0",
"@type":"InitialCommit",
"author":"system",
"identifier":"hpl18q42dbnab4vzq8me4bg1xn8p2a0",
"message":"create initial schema",
"schema":"layer_data:Layer_4234adfe377fa9563a17ad764ac37f5dcb14de13668ea725ef0748248229a91b",
"timestamp":1660919664.9129035
}
```
ok()
Check whether the TerminusDB server is still OK.
Status is not OK when this function returns false
or throws an exception (mostly ConnectTimeout)
Returns
bool
optimize(path)
Optimize the specified path.
Parameters
path
string
Path to optimize, for instance admin/database/_meta for the repo graph.
Example: Python
>>> client = Client("http://127.0.0.1:6363/")>>> client.optimize('admin/database')# optimise database branch (here main)>>> client.optimize('admin/database/_meta')# optimise the repository graph (actually creates a squashed flat layer)>>> client.optimize('admin/database/local/_commits')# commit graph is optimised
patch(before, patch)
Apply the patch object to the before object and return an after object. Note that this change does not commit changes to the graph.
Graph type, either GraphType.INSTANCE or GraphType.SCHEMA.
commit_msg
str
Commit message.
last_data_version
str
Last version before the update, used to check if the document has been changed unknowingly
compress
str or int
If it is an integer, size of the data larger than this (in bytes) will be compress with gzip in the request (assume encoding as UTF-8, 0 = always compress). If it is `never` it will never compress the data.
create
bool
Create the document if it does not yet exist.
raw_json
bool
Update as raw json
reset(commit, soft, use_path)
Reset the current branch HEAD to the specified commit path. If `soft` is not True, it will be a hard reset, meaning reset to that commit in the backend and newer commit will be wipped out. If `soft` is True, the client will only reference to that commit and can be reset to the newest commit when done.
Parameters
commit
string
Commit id or path to the commit (if use_path is True), for instance '234980523ffaf93' or 'admin/database/local/commit/234980523ffaf93'. If not provided, it will reset to the newest commit (useful when need to go back after a soft reset).
soft
bool
Flag indicating if the reset if soft, that is referencing to a previous commit instead of resetting to a previous commit in the backend and wipping newer commits.
use_path
bool
Wheather or not the commit given is an id or path. Default using id and use_path is False.
Updates the specified document(s). Add the document if not existed.
Parameters
document
dict or list of dict
Document(s) to be updated.
graph_type
GraphType
Graph type, either GraphType.INSTANCE or GraphType.SCHEMA.
commit_msg
str
Commit message.
last_data_version
str
Last version before the update, used to check if the document has been changed unknowingly
compress
str or int
If it is an integer, size of the data larger than this (in bytes) will be compress with gzip in the request (assume encoding as UTF-8, 0 = always compress). If it is `never` it will never compress the data.
update_triples(graph_type, content, commit_msg)
Updates the contents of the specified graph with the triples encoded in turtle format.
Replaces the entire graph contents
Parameters
graph_type
GraphType
Graph type, either GraphType.INSTANCE or GraphType.SCHEMA.