Quickstart guide
Overview
In this Quickstart guide we will learn how to do the following basic TypeDB operations with TypeDB Studio:
-
Connect to a TypeDB server,
-
Create a new database,
-
Prepare a TypeQL file (
.tql
), -
Define a schema of a database,
-
Load data into a database,
-
Retrieve schema types and/or data from a database.
For this guide, we will use the sample schema and dataset designed for an Identity and Access Management solution that will be described later.
We highly recommend completing this guide. Its goal is to prepare an environment for TypeDB exploration and development. The resulting database (schema and data) will be needed for example queries throughout the documentation. |
Prerequisites
This Quickstart guide takes advantage of TypeDB Studio. It is an integrated development environment (IDE) for TypeDB. It was specifically designed to connect to TypeDB servers (Core and Enterprise), and use TypeQL.
To proceed, please install both TypeDB and TypeDB Studio:
Use the same version numbers for TypeDB and TypeDB Studio to ensure compatibility. If TypeDB Studio doesn’t have a version similar to the TypeDB release — use the Version compatibility table to determine a compatible version. |
Alternatively, we can use any other TypeDB Client to achieve the same results. But we recommend trying TypeDB Studio first.
Initialize database
Start TypeDB
Run the following command in a terminal:
typedb server
After displaying the TypeDB ASCII logo and the bootup completion time, TypeDB is ready for connections.
Connect to TypeDB
Launch TypeDB Studio, and connect to TypeDB:
-
Click the Connect to TypeDB button (right side of the toolbar).
-
Enter
localhost:1729
in the Address field. -
Click the Connect button (the dialog will close after a successful connection).
Choose a project directory
TypeDB Studio project directory is set to allow us to organize and save related queries for future reuse.
The files and folders in the current project directory are shown in the Project panel on the left side of the window.
To select a project directory:
-
Click the Open Project button in the Project panel (upper left).
-
Choose a directory for the project files.
-
Click the Open button.
The Project panel will now display the contents of the selected directory and a hidden subdirectory for unsaved files.
Prepare a TypeQL file
A TypeDB schema contains entity, relation, and attribute type definitions that make up the data model, as well as rules which may be applied to it — all of which are expressed in TypeQL, TypeDB’s query language.
To prepare the schema definition file:
-
Click the
+
icon in the top left corner of the Text editor panel (directly right from the Project panel). -
Copy the
iam-schema.tql
file listing below inside the new tab.See the full listing of the
iam-schema.tql
filedefine 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; 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; 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; };
-
Click the save icon on the left side of the toolbar.
-
Enter
iam-schema.tql
in the Save As field. -
Click the Save button.
Find the |
Upload the schema
To execute the TypeQL statements in the opened file and send them as queries:
-
Ensure the Session type (
schema
/data
) switch (next to the database dropdown) is set to theschema
. -
Ensure the Transaction type (
write
/read
) switch is set to thewrite
. -
Click the Run Query button (with a "play" symbol, in the middle of the toolbar) to start the transaction.
-
Click the Commit Transaction button (with a "checkmark" symbol, left of the Run Query button) to commit the changes.
The transaction has been committed, and the iam
database now has a schema.
The Types panel will now display the entity, relation, and attribute types within a type hierarchy of the IAM schema.
Data can now be inserted.
First important queries
Read the schema
TypeQL can be used to query schema types.
To execute a simple schema query:
-
Ensure the Session type (
schema
/data
) switch is set to theschema
(next to the database dropdown). -
Ensure the Transaction type (
write
/read
) switch is set to theread
. -
Click the
+
icon right from the Project panel next to theiam-schema.tql
tab in the Text editor panel. -
Copy the TypeQL statement below.
-
Click the Run Query button (with a "play" symbol)
match $t sub thing;
The above query returns all types in the schema of the current database. By default, the results in TypeDB Studio are visualized as a graph.
The Consider using To produce the same result as the above example, use the following query:
|
Insert data
We will insert data the same way we created the schema, by creating a .tql
file in our project and executing it.
To create the file:
-
Click the
+
icon right from the Project panel in the top of the Text editor panel, to create a new tab. -
Copy the
iam-data.tql
file listing below inside the new tab.See the full listing of the
iam-data.tql
file# Subjects insert $p isa person, has full-name "Masako Holley", has email "[email protected]"; # No access insert $p isa person, has full-name "Pearle Goodman", has email "[email protected]"; # Sales manager insert $p isa person, has full-name "Kevin Morrison", has email "[email protected]"; # Full access # Objects insert $f isa file, has path "iopvu.java", has size-kb 55; insert $f isa file, has path "zlckt.ts", has size-kb 143; insert $f isa file, has path "psukg.java", has size-kb 171; insert $f isa file, has path "axidw.java", has size-kb 212; insert $f isa file, has path "lzfkn.java", has size-kb 70; insert $f isa file, has path "budget_2022-05-01.xlsx", has size-kb 758; insert $f isa file, has path "zewhb.java"; insert $f isa file, has path "budget_2021-08-01.xlsx", has size-kb 1705; insert $f isa file, has path "LICENSE"; insert $f isa file, has path "README.md"; # Operations insert $o isa operation, has name "modify_file"; insert $o isa operation, has name "view_file"; # Potential access types match $ob isa file, has path "iopvu.java"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "zlckt.ts"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "psukg.java"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "axidw.java"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "lzfkn.java"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "budget_2022-05-01.xlsx"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "zewhb.java"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "budget_2021-08-01.xlsx"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "LICENSE"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "README.md"; $op isa operation, has name "modify_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "iopvu.java"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "zlckt.ts"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "psukg.java"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "axidw.java"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "lzfkn.java"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "budget_2022-05-01.xlsx"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "zewhb.java"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "budget_2021-08-01.xlsx"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "LICENSE"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; match $ob isa file, has path "README.md"; $op isa operation, has name "view_file"; insert $a (object: $ob, action: $op) isa access; # Permissions match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "iopvu.java"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "zlckt.ts"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "psukg.java"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "axidw.java"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "lzfkn.java"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "budget_2022-05-01.xlsx"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "zewhb.java"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "budget_2021-08-01.xlsx"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "LICENSE"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Kevin Morrison"; $o isa object, has path "README.md"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Pearle Goodman"; $o isa object, has path "budget_2022-05-01.xlsx"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Pearle Goodman"; $o isa object, has path "zewhb.java"; $a isa action, has name "view_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Pearle Goodman"; $o isa object, has path "budget_2021-08-01.xlsx"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Pearle Goodman"; $o isa object, has path "LICENSE"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission; match $s isa subject, has full-name "Pearle Goodman"; $o isa object, has path "README.md"; $a isa action, has name "modify_file"; $ac (object: $o, action: $a) isa access; insert $p (subject: $s, access: $ac) isa permission;
-
Click the save icon (left side of the toolbar).
-
Enter
iam-data.tql
in the Save As field. -
Click the Save button.
Find the |
To execute the TypeQL statements copied from the code block above:
-
Ensure the Session type (
schema
/data
) switch (next to the database dropdown) is set to thedata
. -
Ensure the Transaction type (
write
/read
) switch is set to thewrite
. -
Click the Run Query button (with a "play" symbol).
-
Click the Commit Transaction button (with a "checkmark" symbol).
The transaction has been committed, and the data can now be queried.
Read data
To retrieve data from a database:
-
Click the
+
icon right from the Project panel next to theiam-data.tql
tab in the Text editor panel. -
Ensure the Session type (
schema
/data
) switch (next to the database dropdown) is set to thedata
. -
Ensure the Transaction type (
write
/read
) switch is set to theread
. -
Replace the TypeQL statement in the Text editor panel with the one below.
-
Click the Run Query button (with a "play" symbol).
match $f isa file, has path $fp;
The above query returns all file
entities with their path
attributes. By default, TypeDB Studio displays the
results as shown in the image below.
Summary
The Quickstart guide above provides a fast and easy way to set up the minimum IAM database environment: a TypeDB database with IAM schema and a small dataset. It can be used for examples in this documentation or for independent exploration of TypeDB features.
Here are the files used in the guide above:
-
iam-schema.tql — TypeQL script for the IAM schema definition.
-
iam-data.tql — TypeQL script to load a sample dataset into a database with the IAM schema.
We can do the same process of creating a database, and loading schema and data through any other TypeDB Client.
Here is an example with the TypeDB Console:
# create database
typedb console --command="database create test-iam-db"
# load schema into the new database from a file
typedb console --command="transaction test-iam-db schema write" --command="source iam-schema.tql" --command="commit"
# load data into the new database from a file
typedb console --command="transaction test-iam-db data write" --command="source iam-data.tql" --command="commit"
# check the data loaded (single quotes for bash syntax compatibility with the variables like $t)
typedb console --command="transaction test-iam-db data read" --command='match $t isa thing; get $t;'
Learn more
After completing this guide we can recommend the following order of topics to continue exploring TypeDB:
-
Explore the Fundamentals section for essential information of how TypeDB works:
-
Find out more about how to connect to TypeDB, and use databases, sessions, and transactions.
-
Learn how to define a schema of a database.
-
Check out how to interpret a TypeDB responses to a query.
-
Explore all TypeDB Clients to find the most suitable one.
-
(Optional) Learn more about the IAM sample schema that will be used in the majority of examples throughout this documentation.
-
(Optional) Explore the Sample application written in Java, Python, or Node.js.