Database interaction API crate

Let's add an implementation of the database interaction API to the lib.rs source file. We need to import the diesel crate and declare the module:

#[macro_use]
extern crate diesel;

mod models;
mod schema;

You can see that we have added two modules: models and schema. In the implementation, we need the following types:

use diesel::{Connection, ExpressionMethods, OptionalExtension, PgConnection, QueryDsl, RunQueryDsl, insert_into};
use chrono::Utc;
use failure::{Error, format_err};
use self::models::{Channel, Id, Membership, Message, User};
use self::schema::{channels, memberships, messages, users};
use std::env;

The imports include all models and all tables. We also imported the Connection trait to the establish connection, the ExpressionMethods trait to use the eq method of DSL to set the equality of columns to values, the OptionalExtension trait to use the optional method to try to get a record that cannot be in a table, the QueryDsl trait that has the filter method, and RunQueryDsl to use the get_result method that tries to convert a record to the Rust type. The insert_into method lets us insert new records into a table. Now, we have everything we need to declare the Api struct.

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

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