Start with a Client API

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:

Install Client

Install the TerminusDB JavaScript client by adding it as a dependency in your Node project.

npm install --save @terminusdb/terminusdb-client

Connect with Client

A Client object enables connection to TerminusCMS (or TerminusDB.) To create a client object:

  • 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

const TerminusDBClient = require("@terminusdb/terminusdb-client");

// TODO: change the team name 
const team = "TEAM_NAME";
const client = new TerminusDBClient.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)

Use a Client

Common uses of a Client include Connecting to an existing database and creating a new database.

Code: Connect to a database

Connect to an existing database using the example below.

Change ExampleDatabase with a database that you created.

client.db('ExampleDatabase');

Code: Create a database

Create a new database using the example below.

const createNewDB = async () => {
  try {

      await client.createDatabase('ExampleDatabase', {
          label: "ExampleDatabase",
          comment: "Created new ExampleDatabase",
      });

      console.log("Database created Successfully!")

  } catch (err) {
      console.error(err)
  }
};

createNewDB();

Use the document interface

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.

Data: Player roster

name   | position
------ | --------
George | Center Back
Doug   | Full Back
Karen  | Center Forward

Code: Create a schema

Create a schema object with properties name and position. The object is uniquely identified by name.

const schema = { "@type" : "Class",
                 "@id"   : "Player",
                 "@key"  : { "@type": "Lexical", "@fields": ["name"] },
                 name    : "xsd:string",
                 position: "xsd:string" };

Code: Add a schema

Add the schema object to the database.

Add the schema object to a document using addDocument which returns a Promise.

await client.addDocument(schema, { graph_type: "schema" })

Code: Add documents

Once a schema is added, add documents corresponding to the schema.

Add documents to the schema using addDocument which returns a Promise.

const objects = [
    {
        "@type" : "Player",
        name    : "George",
        position: "Center Back",
    },
    {
        "@type" : "Player",
        name    : "Doug",
        position: "Full Back",
    },
    {
        "@type" : "Player",
        name    : "Karen",
        position: "Center Forward",
    }
];

await client.addDocument(objects);

Code: Get documents

Get a list of documents or specific documents added to the schema

Get a list of documents using getDocument as_list. Results, stored in document, are shown further below.

const getDocs = async () => {
  const documents = await client.getDocument({ as_list: "true" });
  console.log("All Documents", documents)
}
[
  {
    '@id'   : 'Player/Doug',
    '@type' : 'Player',
    name    : 'Doug',
    position: 'Full Back'
  },
  {
    '@id'   : 'Player/George',
    '@type' : 'Player',
    name    : 'George',
    position: 'Center Back'
  },
  {
    '@id'   : 'Player/Karen',
    '@type' : 'Player',
    name    : 'Karen',
    position: 'Center Forward'
  }
]

Code: Query documents

Get a list of documents that matches the query

Get a list of documents using getDocument as_list. Results, stored in document, are shown further below.

const queryDocuments = async () => {
  const query = {
      "type": "Player",
      "query": { "position": "Full Back" },
     }
  const result = await client.queryDocument(query,{"as_list":true});
  console.log("Query Documents",result)
}
[{"@type" : "Player",
  "name" : "Doug",
  "position" : "Full Back"}]

Code: Query documents using WOQL

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

const queryDocuments = async () => {
  const { WOQL } = TerminusDBClient;
    const query = WOQL.triple(
      "v:Player",
      "position",
      WOQL.string("Full Back")
    ).triple("v:Player", "position", "v:position");
  const results = await client.query(query);
  console.log("Query Documents using WOQL: ",results.bindings);
}
[
  {
    Player: 'Player/Doug',
    position: { '@type': 'xsd:string', '@value': 'Full Back' }
  }
]

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.

const team_schema = [
        {
            "@type" : "Class",
            "@id" : "Team",
            "@key" : { 
            "@type": "Lexical", 
            "@fields": ["name"] 
            },
            name : "xsd:string",
            players: { "@type" : "Set",
                       "@class" : "Player" },
        }
];

Code: Add a schema

Add the schema object to the database.

Add the schema object to a document using addDocument that returns a Promise.

await client.addDocument(team_schema, { graph_type: "schema" })

Code: Add documents

Add documents corresponding to the Team schema.

const team_instance = [
        {
            "@type" : "Team",
            name    : "Wildcats",
            players: ["Player/Doug", "Player/Karen"],
        },
        {
            "@type" : "Team",
            name    : "Donkeys",
            players: ["Player/George"],
        }
    ]

await client.addDocument(team_instance)

Code: Get all the teams

Get a list of Team documents.

const getTeams = async () => {
  const query = {
      "type": "Team",
  };
  const result = await client.queryDocument(query, {"as_list":true});
  console.log("Teams ",result)
}

Further Reading

Client reference

JavaScript and Python client reference guides.

Tutorials

More JavaScript and Python client tutorials.

Last updated