C++ driver

The C++ driver was developed by Vaticle to enable TypeDB support for C++ software and developers.

The GitHub repository with the driver’s source code and release notes.

See the driver’s downloadable packages.

Install

Prerequisites

See the Version Compatibility table to check what versions of TypeDB and C++ driver are compatible.

The driver is built against the C++ 17 standard. For Linux: the minimum version of glibc is 2.25.0.

To install TypeDB C++ driver:

  1. Download the latest release for you architecture (x86_64/arm64) and OS (Linux/macOS/Win).

  2. Include the header file typedb_driver.hpp from the include directory. See an example of CMakeLists.txt below.

    CMakeLists.txt example
    cmake_minimum_required(VERSION 3.27)
    project(cpp_driver_test)
    
    set(CMAKE_CXX_STANDARD 17)
    link_directories(${PROJECT_SOURCE_DIR}/lib)
    include_directories(${PROJECT_SOURCE_DIR}/include)
    
    add_executable(cpp_driver_test typedb_test.cpp)
    IF (WIN32)
        target_link_libraries(cpp_driver_test ${PROJECT_SOURCE_DIR}/lib/typedb-driver-cpp.if.lib)
    ELSE()
        target_link_libraries(cpp_driver_test typedb-driver-cpp)
    ENDIF()

Quickstart

See below a short example of C++ code that connects to a local TypeDB Core, creates a database named access-management-db, defines a schema, inserts some data, and then reads it.

#include <iostream>
#include <typedb_driver.hpp>

int main() {
    const std::string dbName = "access-management-db";
    const std::string ServerAddr = "127.0.0.1:1729";
    try {
        TypeDB::Driver driver = TypeDB::Driver::coreDriver(ServerAddr);
        if (driver.databases.contains(dbName)) {
            driver.databases.get(dbName).deleteDatabase();
        }
        driver.databases.create(dbName);
        TypeDB::Options options;
        {
            auto session = driver.session(dbName, TypeDB::SessionType::SCHEMA, options);
            auto tx = session.transaction(TypeDB::TransactionType::WRITE, options);
            tx.query.define("define person sub entity;", options).get();
            tx.query.define("define name sub attribute, value string; person owns name;", options).get();
            tx.commit();
        }
        {
            auto session = driver.session(dbName, TypeDB::SessionType::DATA, options);
            {
                auto tx = session.transaction(TypeDB::TransactionType::WRITE, options);
                (void) tx.query.insert("insert $p isa person, has name 'Alice';", options);
                (void) tx.query.insert("insert $p isa person, has name 'Bob';", options);
                tx.commit();
            }
            {
                auto tx = session.transaction(TypeDB::TransactionType::READ, options);
                TypeDB::ConceptMapIterable result = tx.query.get("match $p isa person, has name $n; get $n;", options);
                for (TypeDB::ConceptMap& cm : result) {
                    std::unique_ptr<TypeDB::Concept> t = cm.get("n");
                    std::cout << t->asAttribute()->getType()->getLabel() << " : " << (t->asAttribute()->getValue()->asString()) << std::endl;
                }
                tx.close();
            }
        }
    } catch (TypeDB::DriverException e ) {
        std::cout << "Caught TypeDB::DriverException: " << e.code() << "\n" << e.message()  << std::endl;
        return 2;
    }
    return 0;
}

For more information on methods used above and other capabilities, see the API reference link below.

Learn more

Driver API reference for the C++ language.

Any questions about the driver after reading the documentation? Feel free to ask on our Discord community server.

Version Compatibility

C++ driver Protocol encoding version TypeDB Core TypeDB Cloud C++

2.26.5

3

2.26.3

2.26.0

17

Provide Feedback