Connecting to a database

In our tool, we will implement two subcommands—add to add a new user, and list to retrieve all available users from the database. Import all the necessary dependencies and add the modules with schema and models:

extern crate clap;
#[macro_use]
extern crate diesel;
extern crate failure;
extern crate serde_derive;

use clap::{
crate_authors, crate_description, crate_name, crate_version,
App, AppSettings, Arg, SubCommand,
};
use diesel::prelude::*;
use diesel::r2d2::ConnectionManager;
use failure::Error;

pub mod models;
pub mod schema;

Since we are using the r2d2 crate, we also have to import ConnectionManager to use diesel's abstraction over the traditional database connection.

Modules declared with the pub modifier make them available in documentation. It's useful for modules that are generated by the diesel crate so that you can explore the functions provided by a generated DSL.
..................Content has been hidden....................

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