A step-by-step guide with examples to install and get started with a JavaScript or Python client.
This guide demonstrates the basic use of the Client library to connect to TerminusCMS with a JavaScript or Python client. Refer to the JavaScript Client or Python Client references guides for detailed documentation. The code discussed on this page is also available in full:
Copy the JavaScript/Python code snippet generated in the step above.
Provide the URL to a database server.
Define a Client
Define and initialize a Client, and connect to a database using the example below.
Code: Define and initialize a Client
Use the package @terminusdb/terminusdb-client
constTerminusDBClient=require("@terminusdb/terminusdb-client");// TODO: change the team name constteam="TEAM_NAME";constclient=newTerminusDBClient.WOQLClient(`https://cloud.terminusdb.com/${team}/`, { user:"user@email.com", organization: team});//set the key as an environment variable.client.setApiKey(process.env.TERMINUSDB_ACCESS_TOKEN)
from terminusdb_client import Clientteam ="logistics"# My team name.endpoint =f"https://cloud.terminusdb.com/{team}/"client =Client(endpoint)# Set the key as an environment variable called:# TERMINUSDB_ACCESS_TOKEN in your shell firstclient.connect(team=team, use_token=True)
To use the TerminusCMS document interface, create a schema and add documents to the schema. Refer to Documents for an overview of the document interface. After creating or connecting to a database, create a schema to add and retrieve data. A simple player roster is used as an example. Using basic Player roster data, the steps below are demonstrated.
Get a specific document using query_document. Results, stored in matches, are shown further below.
documents = client.get_all_documents()# documents comes back as a iterable that can be convert into a listprint("All documents")print(list(documents))print("=============")# getting a specific document by idplayer_doug = client.get_document("Player/Doug")print("Specific document")print(player_doug)
Get a specific document using query_document. Results, stored in matches, are shown further below.
matches = client.query_document({"@type" : "Player","position": "Full Back"})# matches comes back as a iterable that can be convert into a listprint(list(matches))
Query documents using Web Object Query Language (WOQL) to get the same result given by the above example. You can find more about WOQL here.
Get documents using using WOQL.triple() to create a WOQL query then execute that query using client.query(). Results, stored in results, are shown further below
Get documents using WOQLQuery() and WOQLQuery().triple() to create a WOQL query then execute that query using client.query(). Results, stored in results, are shown further below.
from terminusdb_client import WOQLQueryfrom terminusdb_client.query_syntax import*query =triple("v:Player", "position", string("Full Back")).triple("v:Player", "position", "v:position")results = client.query(query)print("Specific document using WOQL")# results['bindings'] holds the query output as a listprint(results['bindings'])
The following code segments demonstrate creating relationships or links between documents
Code: Create schema for team
The Team object is uniquely identified by the property name. The players property consists of a set of Player classes that create a link between theTeam and Player schema.