User

To represent a user stored in the users table, we will add a User struct with the following declaration:

#[derive(Debug, Identifiable, Queryable, Serialize, Deserialize)]
#[table_name = "users"]
pub struct User {
pub id: Id,
pub email: String,
}

As you can see, we use the Id type for the ID column that has the SERIAL SQL type. For the email field, we use the String type, which maps to the TEXT column type in PostgreSQL.

There is also the table_name attribute to bind this struct with a table. We also derive some traits for this model—Debug for printing the model's value to a terminal, and the Serialize and Deserialize traits to make this model convertible to any serialization format. Theser are basic traits that I recommend to implement for database models, especially if you want to use the same types in a REST API.

The Queryable trait represents the result of SQL expressions that can be converted in a struct that implements the trait. This lets us convert tuples to the User struct in our database interaction API later.

The Identifiable trait means the struct that implements this trait represents a single record in a table. This trait has an associated Id type that is set to the corresponding type in the SQL table. This trait also contains an id method that returns the identifier of a record in a table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.62