sylvia_iot_broker/models/sqlite/
conn.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use std::error::Error as StdError;
use std::str::FromStr;

use log::LevelFilter;
use sqlx::{sqlite::SqliteConnectOptions, ConnectOptions, SqlitePool};

/// SQLite connection options.
pub struct Options {
    /// SQLite database file path. Use absolute/relative path.
    pub path: String,
}

/// Connect to SQLite.
pub async fn connect(options: &Options) -> Result<SqlitePool, Box<dyn StdError>> {
    let opts = SqliteConnectOptions::from_str(&options.path)?
        .create_if_missing(true)
        .log_statements(LevelFilter::Off);
    let result = SqlitePool::connect_with(opts).await?;
    Ok(result)
}