Bookstore
Lorem ipsum dolor sit amet
Log in to TypeDB Cloud and deploy a new cluster on the Clusters page.
For self-hosted TypeDB installation and setup, see the Self-hosted deployment page.
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
-
In the TypeDB Cloud dashboard, find the cluster you want to connect, and click Connect. Copy the displayed connection URI, replacing the password placeholder with your actual database password.
-
In Studio, click Connect to TypeDB. Paste the copied URI into the Connection URI field and click Connect.
Click Connect to TypeDB. Your connection URI is typedb-core://<host>:<port>
(e.g: typedb-core://localhost:1729
).
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.