Model Builder and Data Modeling
In this post, I will guide you through the steps needed to design your database schema using TerminusDB 4.0 Console and its model builder.
Having your data in the right format makes it easier to analyze the data properly but you can model your data in more than one way. This is a reality for all the databases, so how to make the right decision?
The model builder is a tool that lets you visualize and edit your database schema. It is very useful for designing and understanding complex data models. I’ll show you how you can start from a simple model and build out something more complex in no time.
if you have never used TerminusDB before, this article includes everything you need to get started with TerminusDB. My First TerminusDB Graph Visualisation — Bike Share Data.
In our examples we use the collection of data about the bike journeys between stations in Washington D.C., USA.
The CSV data used this tutorial is available at https://terminusdb.com/t/data/bike_tutorial.csv
How do I model my schema using the model builder?
I assume you already have TerminusDB and Console running in your system and you have created a db named myBikes.
Form the myBikes main page.
- Select the menu Schema->Schema Builder to arrive at the model builder interface.
- In the whiteboard select the node myBikes Schema, the list of all available node types will show up.
Let’s see the different nodes in detail.
Object contains a piece of the graph and can be accessed by a unique url. An Object can have multiple properties and can be contained inside other objects.
Document is a special type of Object. Documents are always top-level objects, never embedded inside other objects, using the Link Property you can link documents to each other.
Enum or Enumerated is a special data type that enables a property to be a set of predefined constants. The property must be equal to one of the values that have been predefined for it. To use this set of values you’ll need to create an Enum Property and link it to an Emun Element.
Let’s use these elements in practice.
For storing our data we have to create 3 different Documents Bicycle, Station and Journey
Follow the steps and start to build your schema graph.
- Select the myBikes Schema node in the whiteboard and click the + icon
- Select Add Document from the menu. A new node will be added under the node Documents
- We start to add Bicycle Document. Fill the form in the right sidebar with the data from the image above. (Unique Id:Bicycle..)
- Follow the same steps above for adding the other Documents Station and Journey
- Click on the Save icon button in the tools bar to save your work.
At this point our documents are completely unrelated.
To descrive your Documents and to link them with each other, we need to add properties to our documents. We have various different type of properties identified by datatypes
DataProperty : String/Numeric/Geo/Temporal Property refers to the format of data storage
ObjectProperty : Enum/Link Property refers to a relationship between elements
Let’s add the property start_station.
- Select the node Journey in the whiteboard, in the right panel click on the Properties
- In the properties panel, click Add Property in the pop up menu you can see the list of the available property types.
- Select Property Link after the form shows up, fill the fields with the value reported in the table above (Unique ID/Label/Description).
- Click on the Link to Type menu and Select Bike Station Links to Type is a list with all the node that you can linked
- Follow the step 1 to 4 for adding the others properties end_station, journey_bicycle
- Click on the Save icon button in the tools bar to save your work.
- Select Relationships in the right panel top bar to see the links.
We have created our relationship beetween Documents.
Schema Evolution and Compatibility
An important aspect of data management is schema evolution. My initial schema is defined, but my application now need to change it, what can I do?
Now we need to model the Document Station as Object, so we have to delete the Document Station and create a new node type.
The database doesn’t allow you to delete a node if it is related with another node. More precisely the node can not have children and it can not be the range of a Link Property/Enum Property. This constraint is used to prevent actions that would destroy the integrity of the data model.
Delete a Node
Here the step for doing this. First we have to remove all the relationship related to our node.
- Select the Document Journey node in the whiteboard, in the right panel Select Properties. the list of the properties will show up.
- Select End Station Property and delete it using the red delete icon.
- Follow the above steps for deleting Start Station Property too.
- All the Station node constraints have been removed
- Select the Station node in the whiteboard, in the right panel, Click the delete red icon.
Deleting a node is easy at this stage because we don’t have any data in the database.
Add an Object Type
Now we add the Station node as Object Type.
- Select the Schema myBikes, click on the + icon, from the menu Select Add Object a new node will be add in the whiteboard.
- As we did before we have to add the Unique ID/Station and the Label/Description Bike Station in the fields in the right panel.
- Select the Node Journey and add the Link Property Start Station End Station again (Follow the add property steps above).
Difference between Object and Document Types
The difference in Linking a Property to a Document than a Object is in how this data will be represented and managed in document form.
Here is an example with bike data
If Station is a Document Type you have to create a Station Document
and link the id of the Document Station with the Journey Document
This means that if you remove one Journey Document it removes the link but not the data in the related Station document.
If, on the other hand, the Station is an Object Type, its data will be embedded in the Journey Document So if you remove one Journey Document entry it will be remove the related Station Object.
Add an Enum Node
We continue our Schema evolution by adding the Type Bike enum node
- Select the Schema myBikes, Select the + icon, from the menu Select Add Enum
- The Enum node will be added in the whiteboard
- Fill the fields in the right panel (Unique Id:Bike_Type..)
- Select values, Fill the fields and click the button Add a value for adding the list of possible values
Link the Enum Node with the Enum Property
We are going to create our Enum property for the Bicycle Documents
- Select the Bicycle node
- In the right panel, Select the Properties tab
- Select the Add Property menu and Click Enum Property
- Fill the Property fields in the Property panel (Unique ID:bicycle_type ….)
- From the Enum Type menu Select Bike Type
We said that the property Bicycle Type in Bicycle Document is a string with a value chosen from a list of permitted values.
We are going to add other elements to create a more complex hierarchy
At this point in our project we need to add information about the bike user, we suppose we can have 2 types of user that rent the bike, a registered user and a guest user.
First we create an abstract Document User for grouping the different type of user.
An Abstract Document is a completely “abstract class” that is used to group related properties.
Abstract Documents cannot be used to create Data directly, you cannot add data in a User Document. An abstract Document can have children, all the children will inherit all the parent’s properties and if the children are not abstract, you can insert data as children.
Let’s see all the steps for creating an abstract document and its children.
- Select the Document node in the whiteboard, Select the + icon and from the menu that show up, Choose Add Document.
- A new node Document will be added.
- In the right panel fill the fields for the new Document (Unique ID:User, Label:Bike User….)
- Check the Abstract checkbox.
- Select the tab Properties, from the menu Add Property Choose String Property
- The String Property Panel will show up, fill the fields (Unique ID: email, Label:Email)
- Choose Email from the String Type menu.
- In the Cardinality Min field add 1 (this means that if you add an User Document, the email property must have a value in it).
we added the Abstract User Document with its properties, now let’s add the children.
- Select the node Bike User in the whiteboard, Click on the + icon, from the menu Select Add Child
- A new Node will be added, in the right panel fill all the fields (Unique Id:Guest ….)
- Follow the above steps for add the Member Document too
We can decide to not add new properties in the Guest document so the guest user will be identified by the email, but for the Member Document we need to add at least the String property Password.
We have to create a Link Property Bicycle User in the Bike Journey Document for link the User Document with the Bike Journey Document.
Add a Parent
At this point of our project we want to reorganize our nodes.
We have to create an element that can be a Parent for an Object node and a Document node. Objects can only be children of Object types, Documents can be children of Object Types and Document Types, so we have to create a New Object Node
We can call this node Entity. The Entity node is abstract and has a Geo Property Position
I am sure you already know all the steps for doing this 😊.
Let’s group Bike Station and Bicycle under this new node.
- Select the node Bicycle in the whiteboard, in the right panel Select Relationships
- Under Add/Remove Parents panel from the menu Select Object option
- In Add Object as Parent list Select Entity
- The node Bicycle will be moved under the new parent Entity.
Good Job, you made your first TerminusDB schema using the model builder. Data is constantly evolving, be ready to change❗
You can clone the full schema from TerminusHub
Never stop learning.