C++ driver
The C++ driver was developed by the TypeDB team to enable TypeDB support for C++ software and developers.
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:
-
Download the latest release for you architecture (
x86_64
/arm64
) and OS (Linux/macOS/Win). -
Place the
.dylib
file in thelib
directory inside your project folder and all the header files in theinclude
directory. -
Configure the build and include the header file
typedb_driver.hpp
from theinclude
directory. See an example ofCMakeLists.txt
below:CMakeLists.txt examplecmake_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()
Where cpp_driver_test
is the name of your project/target and typedb_test.cpp
is the source file.
Quickstart
See below a short example of C++ code that connects to a local TypeDB server,
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.
Troubleshooting
dyld error: library not loaded
If you encounter the following error:
dyld[72868]: Library not loaded: bazel-out/darwin-opt/bin/cpp/libtypedb-driver-cpp.dylib
Use the following command to set the path for your TypeDB C++ driver library to the lib
directory inside your project:
export DYLD_LIBRARY_PATH=./lib
Learn more
Any questions about the driver after reading the documentation? Feel free to ask on our Discord community server. |