Quickstart
This Quickstart guide shows how to set up a TypeDB database and run queries using TypeDB Studio. The guide is available in both video and text formats.
TypeDB & TypeQL are in the process of being rewritten in Rust. There will be significant refinement to the language, and minor breaks in backwards compatibility. Learn more about the changes from our 3.0 Roadmap. The biggest change to TypeDB 3.0 will be our storage data structure and architecture that significantly boosts performance. We’re aiming to release 3.0 in the autumn this year, along with preliminary benchmarks of TypeDB. |
Run TypeDB
To start TypeDB, select the TypeDB edition below and follow the instructions.
-
Cloud
-
Core
Log in to TypeDB Cloud and create a new deployment on the Deployments page.
For self-hosted TypeDB Cloud installation and setup, see the Self-hosted deployment page.
Ensure TypeDB Core is installed.
To start a locally installed TypeDB Core server:
typedb server
To create a new Docker container with TypeDB Core server:
docker volume create typedb-data
docker create --name typedb -v typedb-data:/opt/typedb-all-linux-x86_64/server/data -p 1729:1729 --platform linux/amd64 vaticle/typedb:latest
Connect to TypeDB
In this guide, we will use TypeDB Studio, the standalone GUI-based client for TypeDB. Ensure Studio is installed, then launch the application.
-
Cloud
-
Core
-
Click Connect to TypeDB on the right side of the top toolbar.
-
Switch the
Server
field drop-down toTypeDB Cloud
. -
Click Manage Cloud Addresses button.
-
Add address and port for at least one server from your TypeDB Cloud deployment, then close the Address Management window.
-
Fill in the
Username
andPassword
fields with valid user credentials.For newly created deployments, the default username and password are
admin
andpassword
. After connecting for the first time, you will be required to change the password before queries can be run. -
Turn on the
Enable TLS
option and leave theCA Certificate
field empty. For self-hosted deployments, encryption parameters may vary. -
Click
Connect
.
Create a project
TypeDB Studio stores queries you create as TypeQL files in a local project directory. To select a project directory, click either Open Project in the Project panel, or the Open Project Directory button in the top toolbar.
Create a new database
To create a new database:
-
Click the Manage Databases button in the top toolbar.
-
Enter a name for a new database and click Create.
-
Select the new database from the Select Database dropdown () in the top toolbar.
Define a schema
A new database needs a schema before we can load any data:
-
Ensure the session type toggle () is set to
schema
and the transaction type toggle () is set towrite
. -
Open a New Tab in the Text Editor and paste in the following Define query.
define user sub entity, owns id @key, owns name, owns birth-date, plays purchase:buyer; order sub entity, owns id @key, owns timestamp, owns status, plays purchase:order; purchase sub relation, relates order, relates buyer; id sub attribute, value string; name sub attribute, value string; birth-date sub attribute, value datetime; timestamp sub attribute, value datetime; status sub attribute, value string, regex "^(paid|dispatched|delivered|returned|canceled)$";
-
Click the Run Query button.
-
When the query has completed, click Commit Transaction.
With the schema defined, we can now insert data into the database.
Load data
Let’s insert sample data for our newly defined schema:
-
Ensure the session toggle is set to
data
and the transaction toggle is set towrite
. -
Open a new tab and paste in the following Insert query.
insert $user-1 isa user, has id "u0001", has name "Kevin Morrison", has birth-date 1995-10-29; $user-2 isa user, has id "u0002", has name "Cameron Osborne", has birth-date 1954-11-11; $user-3 isa user, has id "u0003", has name "Keyla Pineda"; $order-1 isa order, has id "o0001", has timestamp 2022-08-03T19:51:24.324, has status "canceled"; $order-2 isa order, has id "o0002", has timestamp 2021-04-27T05:02:39.672, has status "dispatched"; $order-6 isa order, has id "o0006", has timestamp 2020-08-19T20:21:54.194, has status "paid"; (order: $order-1, buyer: $user-1) isa purchase; (order: $order-2, buyer: $user-1) isa purchase; (order: $order-6, buyer: $user-2) isa purchase;
-
Click the Run Query button.
-
When the query has completed, click Commit Transaction.
The data should now be persisted in the database, and is ready to be queried.
Read data
Let’s retrieve some details for orders that are currently dispatched. The following Fetch query matches any dispatched orders, and returns the order timestamp, order status, and the name of the buyer. Using a data
session and read
transaction, run this query.
match
$order has status "dispatched";
(order: $order, buyer: $buyer) isa purchase;
fetch
$order: timestamp, status;
$buyer: name;
Update data
To update data in a database, we match the existing data, delete it, and then insert new data to replace it. The following Update query updates the status of the order with ID "o0006". Using a data
session and write
transaction, run this query, then commit the transaction.
match
$order-6 isa order,
has id "o0006",
has status $old-status;
delete
$order-6 has $old-status;
insert
$order-6 has status "dispatched";
After committing the transaction, the updated information is persisted in the database and becomes available for querying. If we then re-run the above Fetch query, we should see the results have updated to reflect the change.