Business logic of the example application

In this section, we will learn how to transform data relations into ORM models. We will implement a database interaction crate for a chat application. Imagine, we need to express these data relations in Rust:

We have four tables. The first table contains users. It's the main table and is used by all the other tables. Every user can create a channel and become the owner of a channel. The second table contains channels, and every channel has the owner represented by a record in the users table.

In our chat application, every user can join a channel and post messages to it. To maintain this relation, we will add a memberships table that contains records with two references—a user who is a member of a channel and a channel record that contains a user as a member.

Also, users can post messages to channels. We will keep all messages in a separate messages table. Every message has two relations: the channel that contains the message and the user who posted the message.