January 2019
Intermediate to advanced
520 pages
14h 32m
English
Schema declaration defines a table structure only. To map tables to Rust types, you have to add a module with models that will be used to convert records from the users table to native Rust types. Let's create one and call it models.rs. It will contain the following code:
use serde_derive::Serialize;use super::schema::users;#[derive(Debug, Serialize, Queryable)]pub struct User { pub id: String, pub name: String, pub email: String,}#[derive(Insertable)]#[table_name = "users"]pub struct NewUser<'a> { pub id: &'a str, pub name: &'a str, pub email: &'a str,}
We declared two models here: User to represent a user in a database and NewUser for creating a new record of a user. We derive the necessary traits for the User struct. The Queryable ...
Read now
Unlock full access