Chromia
Start Building

06 / 27Module 1: A Rell backend with FT accountsBeginner

Code the model in Rell

Chromia uses Rell to define the blockchain's data model. The Rell code for this model appears below, and the sections that follow explain each part.

src/news_feed/model.rell
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 works like a database table with two fields, name and id. Because name is declared mutable, you can change its value later. The id field is declared as a key, which makes its value unique across all records of the entity. One more detail:

  • The type of name is text.

Follower entity

The follower entity records that one user follows another. Its fields are:

  • index user;: References the user entity. Since one user can have many followers, this forms a one-to-many relationship. Indexing the field makes SQL queries on it faster.
  • index follower: user;: A second field, called "follower", that holds the user following the user in the first field. It is also indexed so queries stay fast.
  • key user, follower;: A composite key that prevents a user from following the same user more than once, keeping each follower relationship unique.

Post entity

The post entity stores a single post on your social media platform. It contains:

  • timestamp = op_context.last_block_time;: Holds the post's timestamp. Its type is inferred as integer, and its default value is the creation time of the last known block, so your code never has to set it explicitly.
  • index user;: Points to the user who wrote the post. The index speeds up lookups.
  • content: text;: Holds the text of the post in the content field.

You now have a basic Rell database model for a social media platform on the Chromia blockchain. It stores users, their posts, and who follows whom, which gives you the foundation for building the rest of your decentralized app on Chromia.

Written for this site from Chromia’s public course. Code samples are the course’s own.