Design the data model
In this section, we'll walk you through designing a simple database model for your dapp.
In our dapp, the database model consists of three main tables:
- Users: We need a table to store user identities.
- Posts: We'll create a table to manage posts, including post content, the user who created it, and the creation timestamp.
- Followers: We'll design a table representing connections between users (followers).
This model can be visualized as follows:
Implement the model in Rell
In Chromia, you define your blockchain model using a language called Rell. Let's break down the Rell code that corresponds to our model:
module;
entity user {
mutable name;
key id: byte_array;
}
entity follower {
index user;
index follower: user;
key user, follower;
}
entity post {
timestamp = op_context.last_block_time;
index user;
content: text;
}
User entity
The user
entity represents a database table with two fields: name
and id
. The name
is marked as mutable
to let
the user change this, and the id
is marked as key
, signifying that it's a primary key and must be unique. Here are
some additional details:
name
is an alias for thetext
datatype.
Follower entity
The follower
entity is responsible for linking one user to another as a follower. Let's break it down step by step:
index user;
: This field represents a link to theuser
entity, creating a one-to-many relationship because a user can have multiple followers. We mark it as anindex
because we'll frequently query this table, and indexing speeds up SQL queries.index follower: user;
: The second field, named "follower," has a data type ofuser
, representing a follower of the user specified in the first field. It's also indexed for efficient querying.key user, follower;
: This combined key ensures that each user can follow another user only once, maintaining the uniqueness of the follower relationship.
For a deeper understanding of managing relationships between entities, check out our Understand relationships in Rell course.
Post entity
The post
entity represents an actual post on our social media platform. It includes:
timestamp = op_context.last_block_time;
: This field is a timestamp with an implicitinteger
data type. It defaults to the time of the last known block's creation. This setup eliminates the need to set the timestamp explicitly in your code.index user;
: This field references the user who created the post and is indexed for efficient retrieval.content: text;
: Thecontent
field stores the text content of the post.
Great! Using Rell, You created a basic database model for a social media platform on the Chromia blockchain. This model allows you to efficiently store user information, posts, and follower relationships. Now, you're ready to build your decentralized app on Chromia!