Rust driver tutorial
In this tutorial, we’ll build a sample application with the Rust driver capable of basic interaction with TypeDB:
-
Connect to a TypeDB server (Core or Cloud),
-
Manage databases, sessions, and transactions,
-
Send different types of queries.
Follow the steps below or see the full source code.
Environment setup
To run this sample application, you’ll need:
-
TypeDB: either a TypeDB Cloud deployment or a TypeDB Core server. For TypeDB Core installation instructions, see the TypeDB Core installation guide page.
-
Rust, cargo, and TypeDB Rust driver. For the driver installation instructions, see the Rust driver page.
The Sample application below is synchronous for simplicity,
so make sure to add sync
feature either in the Cargo.toml
or via the following command:
cargo add typedb-driver -F sync
Imported modules
To be able to use the TypeDB Rust driver API in the Sample application, use the following import statements:
use std::{error::Error, fs, io};
use typedb_driver::{
answer::{ConceptMap, JSON},
concept::{Attribute, Concept, Value},
Connection, Credential, DatabaseManager, Error as TypeDBError, Options, Promise, Session, SessionType,
TransactionType,
};
Default values
We store default values as constants in the source code:
static DB_NAME: &str = "sample_app_db";
static SERVER_ADDR: &str = "127.0.0.1:1729";
enum Edition {
Core,
Cloud,
}
static TYPEDB_EDITION: Edition = Edition::Core;
static CLOUD_USERNAME: &str = "admin";
static CLOUD_PASSWORD: &str = "password";
where DB_NAME
— the name of the database to use;
SERVER_ADDR
— address of the TypeDB server to connect to;
TYPEDB_EDITION
— TypeDB Core or Cloud edition selector;
CLOUD_USERNAME
/CLOUD_PASSWORD
— credentials to connect to TypeDB Cloud.
Program structure
The main workflow of this sample application includes establishing a connection to TypeDB, database setup, and querying.
fn main() -> Result<(), Box<dyn Error>> {
println!("Sample App");
let driver = connect_to_TypeDB(&TYPEDB_EDITION, SERVER_ADDR)?;
match db_setup(driver.clone(), DB_NAME.to_owned(), false) {
Ok(_) => match queries(driver, DB_NAME.to_owned()) {
Ok(_) => {
return Ok(());
}
Err(x) => return Err(x),
},
Err(_) => return Err(Box::new(TypeDBError::Other("DB setup failed.".to_string()))),
};
}
The entire main()
function code is executed in the context of the network connection,
represented by the driver
object that is returned by the function.
TypeDB connection
The connect_to_TypeDB()
function takes edition
and addr
as mandatory parameters.
fn connect_to_TypeDB(edition: &Edition, addr: &str) -> Result<Connection, typedb_driver::Error> {
match edition {
Edition::Core => return Connection::new_core(addr),
Edition::Cloud => {
return Connection::new_cloud(&[addr], Credential::with_tls(CLOUD_USERNAME, CLOUD_PASSWORD, None)?)
}
};
}
The edition
is expected to be an Enum for selecting a TypeDB edition.
Depending on the TypeDB edition selected, this function initializes either a TypeDB Core or TypeDB Cloud connection.
TypeDB Cloud connection requires an object of the TypeDBCredential
class
that is initialized with a username and password.
For our sample application,
we have the default credentials for the admin
account
provided as default values for the function’s optional parameters.
TypeDB Cloud requires the default password for the default |
Database setup
To set up a TypeDB database, we need to make sure that it exists and has the correct schema and data. First, we check whether a database with the provided name already exists on the server.
If such a database doesn’t exist, we create a new database, define its schema, and load initial data.
To prevent data loss, avoid deleting an existing database without confirmation from a user. |
If a database with the specified name already exists, we check whether we need to replace it.
To do so, we check the db_reset
parameter, and, if it’s False
, ask for an input from a user.
If any of the two suggesting replacement of the database is acceptable,
we replace the database by deleting the existing database and then creating a new one.
As the final step of the database setup, we test it.
pub fn db_setup(driver: Connection, db_name: String, db_reset: bool) -> Result<bool, Box<dyn Error>> {
let databases = DatabaseManager::new(driver.to_owned());
println!("Setting up the database: {}", &db_name);
if databases.contains(&db_name)? {
if db_reset {
match replace_database(&driver, db_name.clone()) {
Ok(_) => (),
Err(e) => {
eprintln!("Error: {:#?}", e);
std::process::exit(1);
}
}
} else {
let mut answer = String::new();
print!("Found a pre-existing database. Do you want to replace it? (Y/N) ");
io::Write::flush(&mut io::stdout()).unwrap();
io::stdin().read_line(&mut answer).expect("Failed to read a line");
if answer.trim().to_lowercase() == "y" {
match replace_database(&driver, db_name.clone()) {
Ok(_) => (),
Err(e) => {
eprintln!("Error: {:#?}", e);
std::process::exit(1);
}
}
} else {
println!("Reusing an existing database.");
}
}
} else {
// No such database found on the server
let _ = create_database(&driver, db_name.clone());
}
let data_session = Session::new(databases.get(db_name.clone())?, SessionType::Data)?;
match db_check(&data_session) {
Ok(_) => return Ok(true),
Err(x) => return Err(x),
}
}
Creating a new database
We create a new database with the specified name (sample_app_db
by default)
and call functions to define its schema and load initial data.
fn create_database(driver: &Connection, db_name: String) -> Result<bool, Box<dyn Error>> {
let databases = DatabaseManager::new(driver.to_owned());
print!("Creating a new database...");
let result = databases.create(&db_name);
match result {
Ok(_) => {
println!("OK");
}
Err(_) => return Err(Box::new(TypeDBError::Other("Failed to create a DB.".to_string()))),
};
{
let schema_session = Session::new(databases.get(&db_name)?, SessionType::Schema)?;
db_schema_setup(&schema_session, "iam-schema.tql".to_string())?;
}
{
let data_session = Session::new(databases.get(&db_name)?, SessionType::Data)?;
db_dataset_setup(&data_session, "iam-data-single-query.tql".to_string())?;
}
return Ok(true);
}
Replacing a database
We delete a database with the specified name (sample_app_db
by default)
and call a function to create a new one instead:
fn replace_database(driver: &Connection, db_name: String) -> Result<bool, Box<dyn Error>> {
let databases = DatabaseManager::new(driver.to_owned());
print!("Deleting an existing database...");
let deletion_result = databases.get(&db_name)?.delete();
match deletion_result {
Ok(_) => println!("OK"),
Err(_) => return Err(Box::new(TypeDBError::Other("Failed to delete a database.".to_string()))),
};
let creation_result = create_database(&driver, db_name);
match creation_result {
Ok(_) => return Ok(true),
Err(_) => return Err(Box::new(TypeDBError::Other("Failed to create a new database.".to_string()))),
};
}
Defining a schema
We use a Define query to define a schema for the newly created database:
fn db_schema_setup(schema_session: &Session, schema_file: String) -> Result<(), TypeDBError> {
let tx = schema_session.transaction(TransactionType::Write)?;
let data = fs::read_to_string(schema_file)?; // "iam-schema.tql"
print!("Defining schema...");
let response = tx.query().define(&data).resolve();
tx.commit().resolve()?;
println!("OK");
return response;
}
The schema for the sample application is stored in the iam-schema.tql file.
See the full schema
define
credential sub attribute, value string;
full-name sub attribute, value string;
id sub attribute, abstract, value string;
email sub id, value string;
name sub id, value string;
number sub id, value string;
path sub id, value string;
object-type sub attribute, value string;
ownership-type sub attribute, value string;
review-date sub attribute, value datetime;
size-kb sub attribute, value long;
validity sub attribute, value boolean;
access sub relation,
relates action,
relates object,
plays change-request:change,
plays permission:access;
change-request sub relation,
relates change,
relates requestee,
relates requester;
membership sub relation,
relates member,
relates parent;
collection-membership sub membership,
relates collection as parent;
group-membership sub membership,
relates group as parent;
set-membership sub membership,
relates set as parent;
ownership sub relation,
relates owned,
relates owner;
group-ownership sub ownership,
owns ownership-type,
relates group as owned;
object-ownership sub ownership,
owns ownership-type,
relates object as owned;
permission sub relation,
owns review-date,
owns validity,
relates access,
relates subject;
segregation-policy sub relation,
owns name,
relates action,
plays segregation-violation:policy;
violation sub relation,
abstract;
segregation-violation sub violation,
relates object,
relates policy,
relates subject;
action sub entity,
abstract,
owns name,
owns object-type,
plays access:action,
plays membership:member,
plays segregation-policy:action;
operation sub action;
operation-set sub action,
plays set-membership:set;
object sub entity,
abstract,
owns object-type,
plays access:object,
plays membership:member,
plays object-ownership:object,
plays segregation-violation:object;
resource sub object,
abstract;
file sub resource,
owns path,
owns size-kb;
record sub resource,
owns number;
resource-collection sub object,
abstract,
plays collection-membership:collection;
database sub resource-collection,
owns name;
directory sub resource-collection,
owns path,
owns size-kb;
subject sub entity,
abstract,
owns credential,
plays change-request:requestee,
plays change-request:requester,
plays membership:member,
plays ownership:owner,
plays permission:subject,
plays segregation-violation:subject;
user sub subject,
abstract;
person sub user,
owns email,
owns full-name;
user-group sub subject,
abstract,
plays group-membership:group,
plays group-ownership:group;
business-unit sub user-group,
owns name;
user-account sub user-group,
owns email;
user-role sub user-group,
owns name;
rule add-view-permission: when {
$modify isa action, has name "modify_file";
$view isa action, has name "view_file";
$ac_modify (object: $obj, action: $modify) isa access;
$ac_view (object: $obj, action: $view) isa access;
(subject: $subj, access: $ac_modify) isa permission;
} then {
(subject: $subj, access: $ac_view) isa permission;
};
We use a session object passed as a parameter to open a transaction. Then we send the contents of the file as a TypeQL Define query and commit the changes made by the transaction.
Loading initial data
With the schema defined, we can load initial data into our database with the Insert query:
fn db_dataset_setup(data_session: &Session, data_file: String) -> Result<(), Box<dyn Error>> {
let tx = data_session.transaction(TransactionType::Write)?;
let data = fs::read_to_string(data_file)?; // "iam-data-single-query.tql"
print!("Loading data...");
let response = tx.query().insert(&data)?;
let result = response.collect::<Vec<_>>();
tx.commit().resolve()?;
println!("OK");
Ok({
drop(result);
})
}
We read the iam-data-single-query.tql file, send its contents as a single query, and then commit the changes.
See the full Insert query
insert
$p1 isa person,
has full-name "Masako Holley",
has email "masako.holley@typedb.com";
$p2 isa person,
has full-name "Pearle Goodman",
has email "pearle.goodman@typedb.com";
$p3 isa person,
has full-name "Kevin Morrison",
has email "kevin.morrison@typedb.com";
$f1 isa file,
has path "iopvu.java",
has size-kb 55;
$modify isa operation, has name "modify_file";
$view isa operation, has name "view_file";
$a1 (object: $f1, action: $modify) isa access;
$a11 (object: $f1, action: $view) isa access;
$permission1 (subject: $p3, access: $a1) isa permission;
$f2 isa file,
has path "zlckt.ts",
has size-kb 143;
$a2 (object: $f2, action: $modify) isa access;
$a22 (object: $f2, action: $view) isa access;
$permission2 (subject: $p3, access: $a2) isa permission;
$f3 isa file,
has path "psukg.java",
has size-kb 171;
$a3 (object: $f3, action: $modify) isa access;
$a33 (object: $f3, action: $view) isa access;
$permission3 (subject: $p3, access: $a3) isa permission;
$f4 isa file,
has path "axidw.java",
has size-kb 212;
$a4 (object: $f4, action: $modify) isa access;
$a44 (object: $f4, action: $view) isa access;
$permission4 (subject: $p3, access: $a4) isa permission;
$f5 isa file,
has path "lzfkn.java",
has size-kb 70;
$a5 (object: $f5, action: $modify) isa access;
$a55 (object: $f5, action: $view) isa access;
$permission5 (subject: $p3, access: $a5) isa permission;
$f6 isa file,
has path "budget_2022-05-01.xlsx",
has size-kb 758;
$a6 (object: $f6, action: $modify) isa access;
$a66 (object: $f6, action: $view) isa access;
$permission6 (subject: $p3, access: $a6) isa permission;
$permission66 (subject: $p2, access: $a66) isa permission;
$f7 isa file,
has path "zewhb.java";
$a7 (object: $f7, action: $modify) isa access;
$a77 (object: $f7, action: $view) isa access;
$permission7 (subject: $p3, access: $a7) isa permission;
$permission77 (subject: $p2, access: $a77) isa permission;
$f8 isa file,
has path "budget_2021-08-01.xlsx",
has size-kb 1705;
$a8 (object: $f8, action: $modify) isa access;
$a88 (object: $f8, action: $view) isa access;
$permission8 (subject: $p3, access: $a8) isa permission;
$permission88 (subject: $p2, access: $a88) isa permission;
$f9 isa file,
has path "LICENSE";
$a9 (object: $f9, action: $modify) isa access;
$a99 (object: $f9, action: $view) isa access;
$permission9 (subject: $p3, access: $a9) isa permission;
$permission99 (subject: $p2, access: $a99) isa permission;
$f10 isa file,
has path "README.md";
$a10 (object: $f10, action: $modify) isa access;
$a100 (object: $f10, action: $view) isa access;
$permission10 (subject: $p3, access: $a10) isa permission;
$permission100 (subject: $p2, access: $a100) isa permission;
Testing a database
With the schema defined and data loaded, we test our database to make sure it’s ready. To test the database, we send a query to count the number of users in the database:
fn db_check(data_session: &Session) -> Result<bool, Box<dyn Error>> {
let tx = data_session.transaction(TransactionType::Write)?;
let test_query = "match $u isa user; get $u; count;";
print!("Testing the database...");
let response = tx.query().get_aggregate(test_query).resolve();
let result = match response?.ok_or("Error: unexpected test query response.")? {
Value::Long(value) => value,
_ => unreachable!(),
};
if result == 3 {
println!("OK");
Ok(true)
} else {
Err(Box::new(TypeDBError::Other("Test failed. Terminating...".to_string())))
}
}
Query examples
After database setup is complete, we proceed with querying our database with different types of queries in the
queries()
function:
fn queries(driver: Connection, db_name: String) -> Result<(), Box<dyn Error>> {
println!("Request 1 of 6: Fetch all users as JSON objects with full names and emails");
let users = fetch_all_users(driver.clone(), db_name.clone());
assert!(users?.len() == 3);
let new_name = "Jack Keeper";
let new_email = "jk@typedb.com";
println!("Request 2 of 6: Add a new user with the full-name {} and email {}", new_name, new_email);
let new_user = insert_new_user(driver.clone(), db_name.clone(), new_name, new_email);
assert!(new_user?.len() == 1);
let infer = false;
let name = "Kevin Morrison";
println!("Request 3 of 6: Find all files that the user {} has access to view (no inference)", name);
let no_files = get_files_by_user(driver.clone(), db_name.clone(), name, infer);
assert!(no_files?.len() == 0);
let infer = true;
println!("Request 4 of 6: Find all files that the user {} has access to view (with inference)", name);
let files = get_files_by_user(driver.clone(), db_name.clone(), name, infer);
assert!(files?.len() == 10);
let old_path = "lzfkn.java";
let new_path = "lzfkn2.java";
println!("Request 5 of 6: Update the path of a file from {} to {}", old_path, new_path);
let updated_files = update_filepath(driver.clone(), db_name.clone(), old_path, new_path);
assert!(updated_files?.len() == 1);
let path = "lzfkn2.java";
println!("Request 6 of 6: Delete the file with path {}", path);
let deleted = delete_file(driver.clone(), db_name.clone(), path);
match deleted {
Ok(_) => return Ok(()),
Err(_) => return Err(Box::new(TypeDBError::Other("Application terminated unexpectedly".to_string()))),
};
}
The queries are as follows:
-
Fetch query — to retrieve information in a JSON format
-
Insert query — to insert new data into the database
-
Get query — to retrieve data from the database as stateful objects
-
Get query with inference — to retrieve data from the database as stateful objects using inference
-
Update query — to replace data in the database
-
Delete query — to delete data from the database
Every query is implemented as a function that includes some output of the query response and returns some meaningful data.
Fetch query
The main way to retrieve data from a TypeDB database is to use fetching to get values of attributes, matched by a pattern.
Let’s use a Fetch query to fetch names and emails for all users in the database:
fn fetch_all_users(driver: Connection, db_name: String) -> Result<Vec<JSON>, Box<dyn Error>> {
let databases = DatabaseManager::new(driver);
let session = Session::new(databases.get(db_name)?, SessionType::Data)?;
let tx = session.transaction(TransactionType::Read)?;
let iterator = tx.query().fetch("match $u isa user; fetch $u: full-name, email;")?;
let mut count = 0;
let mut result = vec![];
for item in iterator {
count += 1;
let json = item?;
println!("User #{}: {}", count.to_string(), json.to_string());
result.push(json);
}
if result.len() > 0 {
Ok(result)
} else {
Err(Box::new(TypeDBError::Other("Error: No users found in a database.".to_string())))
}
}
We get the response as a stream of results, containing JSON.
We create a result
variable to store the results and iterate through stream to print and store JSONs.
Insert query
Let’s insert a new user with full-name
and email
attributes to the database.
fn insert_new_user(
driver: Connection,
db_name: String,
new_name: &str,
new_email: &str,
) -> Result<Vec<ConceptMap>, Box<dyn Error>> {
let databases = DatabaseManager::new(driver);
let session = Session::new(databases.get(db_name)?, SessionType::Data)?;
let tx = session.transaction(TransactionType::Write)?;
let iterator = tx.query().insert(&format!(
"insert $p isa person, has full-name $fn, has email $e; $fn == '{}'; $e == '{}';",
new_name, new_email
))?;
let mut result = vec![];
for item in iterator {
let concept_map = item?;
let name = unwrap_string(concept_map.get("fn").unwrap().clone());
let email = unwrap_string(concept_map.get("e").unwrap().clone());
println!("Added new user. Name: {}, E-mail: {}", name, email);
result.push(concept_map);
}
if result.len() > 0 {
let _ = tx.commit().resolve();
Ok(result)
} else {
Err(Box::new(TypeDBError::Other("Error: No users found in a database.".to_string())))
}
}
The Insert query returns a stream of ConceptMaps: one for every insert
clause execution.
We iterate through the stream to print and store the inserted data.
Then we commit the changes and return the stored results.
Since the Insert query has no match
clause, the insert
clause is executed exactly once.
But the Insert query always returns a list of ConceptMap objects, where every ConceptMap represents an inserted result.
Get query
Let’s retrieve all files available for a user with a get_files_by_user()
function.
It can be used with or without inference enabled.
fn get_files_by_user(
driver: Connection,
db_name: String,
name: &str,
inference: bool,
) -> Result<Vec<(usize, ConceptMap)>, Box<dyn Error>> {
let databases = DatabaseManager::new(driver);
let session = Session::new(databases.get(db_name)?, SessionType::Data)?;
let tx = session.transaction_with_options(TransactionType::Read, Options::new().infer(inference))?;
let users = tx
.query()
.get(&format!("match $u isa user, has full-name '{}'; get;", name))?
.map(|x| x.unwrap())
.collect::<Vec<_>>();
let response;
if users.len() > 1 {
return Err(Box::new(TypeDBError::Other("Found more than one user with that name.".to_string())));
} else if users.len() == 1 {
response = tx
.query()
.get(&format!(
"match
$fn == '{}';
$u isa user, has full-name $fn;
$p($u, $pa) isa permission;
$o isa object, has path $fp;
$pa($o, $va) isa access;
$va isa action, has name 'view_file';
get $fp; sort $fp asc;
",
name
))?
.map(|x| x.unwrap())
.enumerate()
.collect::<Vec<_>>();
for (count, file) in &response {
println!("File #{}: {}", count + 1, unwrap_string(file.get("fp").unwrap().clone()));
}
if response.len() == 0 {
println!("No files found. Try enabling inference.");
}
return Ok(response);
} else {
return Err(Box::new(TypeDBError::Other("No users found with that name.".to_string())));
}
}
We call the function with the inference disabled (false
) and expect it to return no results, as the query pattern
matches only files available for view_file
action, and there are no such files initially in the database.
The get_files_by_user()
function checks
that there is only one user matched with the name provided by an input parameter.
It then executes the query to find the files, collect the results, and
iterates through them to print a value of every matched path
attribute.
For bigger numbers of results it might be faster to iterate through a stream, rather than collect and store the results first. |
Get query with inference
To get query results with inferred data, let’s enable the infer
parameter of the TypeDB transaction options.
We use the same get_files_by_user()
function, but set the inference
parameter to true
when we call it again.
The add-view-permission
rule provides us with some inferred results this time.
Update query
Let’s replace a path for one of the files with a new path. We can do that by deleting ownership of the old path attribute from the file entity and assigning it with ownership of the new path attribute with the Update query:
fn update_filepath(
driver: Connection,
db_name: String,
old_path: &str,
new_path: &str,
) -> Result<Vec<ConceptMap>, Box<dyn Error>> {
let databases = DatabaseManager::new(driver);
let session = Session::new(databases.get(db_name)?, SessionType::Data)?;
let tx = session.transaction(TransactionType::Write)?;
let response = tx
.query()
.update(&format!(
"match
$f isa file, has path $old_path;
$old_path = '{old}';
delete
$f has $old_path;
insert
$f has path $new_path;
$new_path = '{new}';",
old = old_path,
new = new_path
))?
.map(|x| x.unwrap())
.collect::<Vec<_>>();
if response.len() > 0 {
let _ = tx.commit().resolve();
println!("Total number of paths updated: {}", response.len());
return Ok(response);
} else if response.len() == 0 {
println!("No matched paths: nothing to update");
return Ok(response);
} else {
return Err(Box::new(TypeDBError::Other("Impossible query response.".to_string())));
}
}
We collect the response of the Update query and check the length of it to determine
the number of times the delete
and insert
clauses are executed.
We then commit the changes only if the number meets our expectation.
Delete query
Finally, let’s delete the same file we updated the path for. First, we match the file in a Get (or Fetch) query to check how many files get matched to prevent unplanned deletes. If the number (or any other relevant parameters) of matched results is as expected, we proceed with a Delete query with the same match clause.
By using the same write
transaction we employ snapshot isolation
to prevent any other transactions from changing the expected results.
If any other transaction makes a conflicting change before we commit this transaction,
then our transaction fails upon a commit.
fn delete_file(driver: Connection, db_name: String, path: &str) -> Result<(), Box<dyn Error>> {
let databases = DatabaseManager::new(driver);
let session = Session::new(databases.get(db_name)?, SessionType::Data)?;
let tx = session.transaction(TransactionType::Write)?;
let files = tx
.query()
.get(&format!(
"match
$f isa file, has path '{}';
get;",
path
))?
.map(|x| x.unwrap())
.collect::<Vec<_>>();
if files.len() == 1 {
let response = tx
.query()
.delete(&format!(
"match
$f isa file, has path '{path}';
delete
$f isa file;
"
))
.resolve();
match response {
Ok(_) => {
println!("File has been deleted.");
Ok(())
}
Err(_) => return Err(Box::new(TypeDBError::Other("Error: Failed to delete.".to_string()))),
}
} else {
return Err(Box::new(TypeDBError::Other(
format!("Wrong number of files to delete: {}", files.len()).to_string(),
)));
}
}