This is a simple example of using TypeDB.
We have an imaginary online bookstore and our script/application implements some very basic functions:
- Search for a book
- Search for a user
- Search for an order
- Search for books by genre
This application consists of Python scripts. It has not much in terms of usability as it is merely a demonstration of TypeDB queries. But you can easily explore the implementation of each function in the scripts.
- TypeDB v2.14.1+
- Python v.3.9+
typedb.driver
— Python driver for TypeDB- Common Python libraries: os, csv, argparse, enum, uuid, random, unittest
- This repository
- Checkout this repository:
git clone https://github.com/vaticle/typedb-driver-examples && cd typedb-driver-examples
- Start the TypeDB Server. Check that it's listening to address:
0.0.0.0:1729
. - Launch
typedb-driver-examples/commerce/bookstore/python/load_data.py
Python script. It will load the bookstore schema and data into the DB. - Launch
typedb-driver-examples/commerce/bookstore/python/requests.py
Python script and follow the instructions to explore example functions and data. - Use this simple example to learn the basics of using TypeDB! Explore source codes of the Python scripts and use TypeDB Studio to explore DB schema and content.
This example is located in the typedb-driver-examples/commerce/bookstore/
directory and consists of the following main files:
- Python scripts
python/load_data.py
— used to load the bookstore DB schema and datapython/requests.py
— provides simple command line interface to execute requests on TypeDB databasepython/loaders.py
— internal (imported) file with data loading classes and functionspython/config.py
— internal settings: database name and path to directory with imported csv files
schema.tql
— DB schema in TypeQLREADME.md
— documentation for the bookstore example. You are reading it right nowtests.py
— a set of tests for the exampletodo.md
— list of ideas for improvements in the future. If you want to contribute to this example, you can start with these ideas- Bookstore dataset
data/
:books.csv
users.csv
ratings.csv
order.csv
genres.csv
request-examples/
— directory with examples of requests for the database. Files of.tql
format can be used in Type DB Studio directly or to create a request for other TypeDB clientsrequirements.txt
— list of major requirements for internal testing environment
The schema stored in the schema.tql
file and loaded by the load_data.py
script.
The bookstore schema has the following attributes:
-
name (string)
-
description (string)
-
id (string)
-
ISBN (string)
-
book-author (string)
-
publisher (string)
-
foreign-user-id (string)
-
status (string)
-
delivery-address (string)
-
payment-details (string)
-
username (string)
-
password (string)
-
foreign-id (string)
-
genre-tag (string)
-
created-date (datetime)
-
price (long)
-
stock (long)
-
rating (long)
-
age (long)
The bookstore schema has the following entities:
- product
- book
- person
- user
The bookstore schema has the following relations:
- review
- order
- tag-hierarchy
The bookstore schema has two rules to demonstrate rules usability.
The first one works for genre tags, serves no real purpose but follows the following basic logic — a child (sub-tag) of my child is my child. The code for the first rule:
rule super-tag-hierarchy:
when {
(sup-tag: $p, sub-tag: $b) isa tag-hierarchy;
(sup-tag: $b, sub-tag: $bb) isa tag-hierarchy;
} then {
(sup-tag: $p, sub-tag: $bb) isa tag-hierarchy;
};
The second one works also for genre tags, used to improve tag searching experience. By assigning tag to a book you are also assigning all sup-tags to the same book. So the book can be found not only by exact tag you have assigned to it, but also by all the parent tags of this tag.
rule super-tag-ownership:
when {
$book isa book;
$g isa genre-tag;
$book has $g;
$sup isa genre-tag;
(sup-tag: $sup, sub-tag: $g) isa tag-hierarchy;
} then {
$book has $sup;
};