Skip to content
Hugo Tiburtino edited this page Dec 28, 2022 · 1 revision

How Database Layer works

If you want to understand the database structure, go here.

Basics

The Database Layer is, as the name indicates, a layer between the API and the database.
Its main purpose is to store data to and retrieve it from the database.
Serlo's DB Layer is written in Rust using the popular web server framework actix for handling the requests and sqlx for communicating with a MySQL database. It also uses extensively serde.rs.

Overview

As in every Rust app, it begins in main.rs, where the app is configured and started. Differently to usual web servers and inspired by GraphQL, the DB layer has just one "relevant" endpoint.
Each payload it receives should conform one of the supported messages. For example, a payload about the Serlo's uuids should have the fields "type" and "payload", like

{
  "type": "UuidQuery",
  "payload": {
    "id": 1
  }
}

If we had wrongly written instead of 'UuidQuery' 'IdQuery' or instead of 'id' 'uuid' we would have gotten Json deserialize error: data did not match any variant of untagged enum Message.
Once the payload is correct, it calls the corresponding method which in turn gives back a response that is going to be translated into a actix http response.

TODO:

  • explain how it connects to the database
  • explain operation
  • explain events
  • note on the custom datetime
Clone this wiki locally