Understanding Database Interactions
Now that our tables are defined, let's learn how to interact with those form a Zephyr program.
Accessing the Tables
Before starting to write to/read from the tables we defined in the zephyr.toml
, we need to define them in our program.
We're defining here the table "feedback" of the one of the zephyr.toml
as a struct and assigning it the DatabaseDerive macro (more on that later). When using DatabaseDerive we also need to specify the table's name. Moreover, the field names in the structure must be the same as defined in your zephyr.toml
columns definition.
Now to access (in this case to write) the table from the program we just have to create an instance of the struct, which will represent a row in the table, and call the Environment functions to interact with the database, such as EnvClient::put()
. More about database operations on the next page.
DatabaseDerive and Defining Tables Rust-Side
The magic of rust metaprogramming comes into play when working with the database. Assuming you've already read through the quickstart, let's jump to an immediate example. Before the DatabaseDerive
macro, reading, and writing to the database was done as follows:
Reading from the database and updating was even worse. This is unsound and non-idiomatic, but more importantly very unsafe. With the DatabaseDerive
macro, this becomes much better and safer:
The DatabaseDerive
macro builds the required trait implementation to construct a code similar to the one used in the xycLoans example under the hood. For this to work, however, you need to make sure that the field names in the structure are the same as defined in your zephyr.toml
columns definition.
Last updated