Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bookstore example #143

Merged
merged 110 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
9943a15
Commerce bookstore initial commit
izmalk Nov 29, 2022
4a8b57c
Added simple requests
izmalk Nov 30, 2022
2ac249d
Minor updates after testing
izmalk Nov 30, 2022
d32e1c9
Added order generation/import
izmalk Dec 1, 2022
6003880
Debugging and polishing data loading
izmalk Dec 1, 2022
d079401
Added orders and tags (WIP)
izmalk Dec 1, 2022
5501562
Added super typing
izmalk Dec 1, 2022
f60c114
Added Readme and comments, modified load_data.py
izmalk Dec 3, 2022
310eac0
Added info into typedb-examples readme
izmalk Dec 4, 2022
ae9bc02
Refactoring titles
izmalk Dec 5, 2022
1ef6b95
Added info for the bookstore example
izmalk Dec 5, 2022
75040e8
Fixed two typos
izmalk Dec 5, 2022
0d9fdec
Update commerce/bookstore/README.md
izmalk Dec 5, 2022
737f59c
Update commerce/bookstore/python/load_data.py
izmalk Dec 5, 2022
e614f09
Add comments and minor refactoring
izmalk Dec 6, 2022
d049e4a
Fixed a bug with no rating books
izmalk Dec 6, 2022
54667a7
Added sorting and fixed a bug with genre tags
izmalk Dec 6, 2022
5f7a34b
Renamed template functions
izmalk Dec 7, 2022
bac11c1
Capitalisation refactoring
izmalk Dec 7, 2022
cf7a8c3
Refactoring for kebab... case
izmalk Dec 7, 2022
7a18b0f
Update commerce/bookstore/README.md
izmalk Dec 7, 2022
bc132e9
Python capitalisation, 2nd attempt
izmalk Dec 7, 2022
926e3a7
Merge branch 'izmalk_bookstore_init' of github.com:izmalk/typedb-exam…
izmalk Dec 7, 2022
6d50dec
Switched quotes and fixed quote parsing problem
izmalk Dec 7, 2022
1f78463
Added Classes
izmalk Dec 8, 2022
012a37e
Minor fixes and polishing
izmalk Dec 8, 2022
a1fe04b
Fixed title after review
izmalk Dec 8, 2022
1f5c70b
Added option --debug to both scripts
izmalk Dec 8, 2022
4a1797d
Renamed review relation
izmalk Dec 8, 2022
c5cfbfe
Update commerce/bookstore/schema.tql
izmalk Dec 8, 2022
eeee228
Added loaders.py with classes to load input files and config.py with …
izmalk Dec 9, 2022
d436356
Fixed after review. Renames and file parameter refactoring
izmalk Dec 9, 2022
a615078
Genre tags without taging relation
izmalk Dec 9, 2022
404caa1
Renamed genre attribute to genre-tag
izmalk Dec 9, 2022
02c0708
Orders are now relations
izmalk Dec 10, 2022
458eca4
Fixed a multiple relations insert
izmalk Dec 10, 2022
c2c538f
Deleted redundant commented code fragment
izmalk Dec 10, 2022
88aeae7
Merge pull request #2 from izmalk/izmalk_orders_refactoring
izmalk Dec 10, 2022
1e4cc80
Fixed title after review
izmalk Dec 8, 2022
32d4d73
Added option --debug to both scripts
izmalk Dec 8, 2022
9226c84
Renamed review relation
izmalk Dec 8, 2022
c75c653
Update commerce/bookstore/schema.tql
izmalk Dec 8, 2022
4bc5ad5
Genre tags without taging relation
izmalk Dec 9, 2022
efde23f
Renamed genre attribute to genre-tag
izmalk Dec 9, 2022
049828c
Orders are now relations
izmalk Dec 10, 2022
4515e9b
Fixed a multiple relations insert
izmalk Dec 10, 2022
7de65f0
Deleted redundant commented code fragment
izmalk Dec 10, 2022
59d95fd
Added Classes
izmalk Dec 8, 2022
c1e664e
Minor fixes and polishing
izmalk Dec 8, 2022
714dc63
Added loaders.py with classes to load input files and config.py with …
izmalk Dec 9, 2022
c7ac535
Fixed after merge conflict
izmalk Dec 11, 2022
f2b0ea1
Minor fixes
izmalk Dec 11, 2022
2432fa8
Minor fixes and Readme update
izmalk Dec 11, 2022
bf16e0c
Added Classes
izmalk Dec 8, 2022
6b50537
Minor fixes and polishing
izmalk Dec 8, 2022
91fe69d
Added loaders.py with classes to load input files and config.py with …
izmalk Dec 9, 2022
96ed5eb
Fixed after review. Renames and file parameter refactoring
izmalk Dec 9, 2022
b11f6f3
Added option --debug to both scripts
izmalk Dec 8, 2022
8b1f191
Renamed review relation
izmalk Dec 8, 2022
61ef668
Orders are now relations
izmalk Dec 10, 2022
92a74b7
Added Classes
izmalk Dec 8, 2022
bba4973
Minor fixes and polishing
izmalk Dec 8, 2022
81e529e
Added loaders.py with classes to load input files and config.py with …
izmalk Dec 9, 2022
4f91fdd
Fixed after merge conflict
izmalk Dec 11, 2022
189f62d
Minor fixes
izmalk Dec 11, 2022
bb5191e
Minor fixes and Readme update
izmalk Dec 11, 2022
2e89dd5
Merge branch 'izmalk_class_input' of github.com:izmalk/typedb-example…
izmalk Dec 11, 2022
d2db89d
Deleted zip file
izmalk Dec 12, 2022
7affce3
Delete zip file
izmalk Dec 12, 2022
bd33d9e
Minor fixes
izmalk Dec 12, 2022
57c3fa0
Renamed argument name to verbose
izmalk Dec 12, 2022
496d6fc
Merge branch 'izmalk_bookstore_init' into izmalk_class_input
izmalk Dec 14, 2022
54f3b86
Trying to fix building error with checkstyle_test
izmalk Dec 14, 2022
1b45d0d
Trying to fix BUILD attempt 2
izmalk Dec 14, 2022
1f54c0d
Trying to fix BUILD attempt 3
izmalk Dec 15, 2022
fead20e
Trying to fix BUILD attempt 4
izmalk Dec 15, 2022
137573f
Merge pull request #1 from izmalk/izmalk_class_input
izmalk Dec 15, 2022
b67880b
Enum ResultCode
izmalk Dec 16, 2022
d5ac4b4
Add bazel support
izmalk Dec 19, 2022
956abda
Fix bookstore_pip in Bazel
izmalk Dec 19, 2022
ec270a3
Commented python Bazel
izmalk Dec 19, 2022
b9fe205
Fixing Bazel build
izmalk Dec 19, 2022
46c66b4
fixing Bazel building 2
izmalk Dec 19, 2022
ff0e0c6
Genre loading from CSV + Minor refactoring
izmalk Dec 20, 2022
240df54
fix for bazel (#3)
izmalk Dec 24, 2022
4dfd62e
Deleted redundant comments
izmalk Jan 12, 2023
23e1b86
Update commerce/bookstore/python/requirements.txt
izmalk Jan 12, 2023
d753eb7
Moved data directory
izmalk Jan 16, 2023
8dd078b
Fixed path to data directory
izmalk Jan 16, 2023
0655495
Rename setup() function
izmalk Jan 16, 2023
1964bc9
Loaders verbose and bazel fix experiments
izmalk Jan 17, 2023
822564e
Function and comments refactor
izmalk Jan 17, 2023
f10b9c8
Finish refactoring functions
izmalk Jan 17, 2023
d32623b
super-tag rule rename
izmalk Jan 18, 2023
964b18e
Remove bazel
izmalk Jan 19, 2023
277c691
Fixing build without Bazel
izmalk Jan 19, 2023
d0999e0
Fixing build without Bazel
izmalk Jan 19, 2023
9c06920
Fixing the checkstyle check to build
izmalk Jan 19, 2023
15a54fd
Fixing the checkstyle tests to build 2
izmalk Jan 19, 2023
bf751b0
Fixing the checkstyle tests to build 3
izmalk Jan 19, 2023
3d4b5c7
Add os.path
izmalk Jan 19, 2023
ef9fa71
Refactor delivery address data
izmalk Jan 20, 2023
49cbdd1
Store server addr in config.py
izmalk Jan 20, 2023
f815079
Refactoring input into loader
izmalk Jan 20, 2023
167562a
Add tests
izmalk Jan 29, 2023
0408342
Update Copyright notice to 2023
izmalk Feb 10, 2023
090229b
Create a client only once per app
izmalk Feb 10, 2023
1d54bc4
Add comments to tests
izmalk Feb 10, 2023
dbda7f6
Revert copyright changes to fix tests
izmalk Feb 10, 2023
f6d4569
Minor fixes
izmalk Feb 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ bazel-*

# Distribution
dist/
.typedb-studio/

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ constraints, all at query (run) time.
[TypeDB CTI](https://github.com/typedb-osi/typedb-cti) is an open source threat intelligence platform for organisations
to store and manage their cyber threat intelligence (CTI) knowledge. It enables threat intel professionals to bring
together their disparate CTI information into one database and find new insights about cyber threats.

## [Commerce: Bookstore](commerce/bookstore)
izmalk marked this conversation as resolved.
Show resolved Hide resolved

The bookstore example uses python to showcases migration of data into TypeDB and executing queries on this data.
izmalk marked this conversation as resolved.
Show resolved Hide resolved

Read the [README](commerce/bookstore/README.md) file for instructions. Check [the schema](commerce/bookstore/schema.tql) or the initial [dataset](commerce/bookstore/python/data) for additional information. All logic accessible in the script files.
137 changes: 137 additions & 0 deletions commerce/bookstore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Bookstore example

[![forthebadge](https://forthebadge.com/images/badges/made-with-python.svg)](http://forthebadge.com) [![forthebadge](images/made-with-typedb.svg)](https://forthebadge.com)

This is a simple example of using TypeDB.

## Introduction

We have imaginary online Bookstore and our script/application implements some very basic functions:
izmalk marked this conversation as resolved.
Show resolved Hide resolved

- Search for a Book
- Search for a User
- Search for an Order
- Search for books by genre
alexjpwalker marked this conversation as resolved.
Show resolved Hide resolved

This application consists of two python scripts. It has not much in terms of usability as it is merely a demonstration of TypeDB queries.

You can clearly see the implementation of mentioned functions in the source codes.
izmalk marked this conversation as resolved.
Show resolved Hide resolved

## Prerequisites

* [TypeDB](https://docs.vaticle.com/docs/running-typedb/install-and-run) v2.14.1+
* Python v.3.9+
* `typedb.client` — [python client](https://docs.vaticle.com/docs/client-api/python) for TypeDB
* This repository
izmalk marked this conversation as resolved.
Show resolved Hide resolved

## Quickstart

1. Checkout this repository: `git clone https://github.com/vaticle/typedb-examples && cd typedb-examples`
2. Start the [TypeDB Server](http://docs.vaticle.com/docs/running-typedb/install-and-run#start-the-typedb-server). Check that it's listening to address: `0.0.0.0:1729`.
3. Launch `typedb-examples/commerce/bookstore/python/load_data.py` python script. It will load the bookstore schema and data into the DB.
4. Launch `typedb-examples/commerce/bookstore/python/requests.py` python script and follow the instructions to explore example functions and data.
5. Use this simple example to learn the basics of using TypeDB! Explore source codes of the both python scripts and TypeDB Studio to explore DB schema and content.

## How it works

### Files

This example located in the `typedb/commerce/bookstore/` directory and consists of the following main files:
izmalk marked this conversation as resolved.
Show resolved Hide resolved
- Python scripts
- `python/load_data.py` — used to load the bookstore DB schema and data
- `python/requests.py` — provides simple command line interface to execute requests on TypeDB database
- `schema.tql` — DB schema
- `README.md` — documentation for the bookstore example. You are reading it right now
- Bookstore dataset `python/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 clients

### Schema

The schema stored in the `schema.tql` file and loaded by the `load_data.py` script.

![bookstore_schema](images/bookstore_schema.png)

#### Attributes

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 (string)

- date (datetime)

- price (long)
- stock (long)
- rating (long)
- age (long)


#### Entities

The bookstore schema has the following entities:

- Product
- Book
- Person
- User
- Order

#### Relations

The bookstore schema has the following relations:

- reviewing
- taging
- ordering
- tag-hierarchy

#### Rules

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 first rule text:

```
rule super-tag-hierarchy:
when {
$b isa genre;
$p isa genre;
$bb isa genre;
(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 of its parent tags.

```
rule super-tag:
when {
$book isa Book;
$tag (tag:$g, book: $b) isa taging;
$g isa genre;
$sup isa genre;
(sup-tag: $sup, sub-tag: $g) isa tag-hierarchy;
} then {
(tag:$sup, book: $b) isa taging;
};
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions commerce/bookstore/images/made-with-typedb.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading