use crate::data::{Database, EngineError};
use crate::error_messages::ERROR_DB_SETUP;
use csml_interpreter::data::csml_bot::CsmlBot;
use serde::{Deserialize, Serialize};
#[cfg(feature = "dynamo")]
use self::dynamodb as dynamodb_connector;
#[cfg(feature = "mongo")]
use self::mongodb as mongodb_connector;
#[cfg(feature = "postgresql")]
use self::postgresql as postgresql_connector;
#[cfg(feature = "sqlite")]
use self::sqlite as sqlite_connector;
pub mod bot;
pub mod conversations;
pub mod memories;
pub mod messages;
pub mod state;
pub mod user;
pub mod clean_db;
pub mod utils;
pub mod db_test;
use crate::Client;
#[cfg(feature = "dynamo")]
mod dynamodb;
#[cfg(feature = "mongo")]
mod mongodb;
#[cfg(feature = "postgresql")]
mod postgresql;
#[cfg(feature = "sqlite")]
mod sqlite;
#[derive(Serialize, Deserialize, Debug)]
pub struct DbConversation {
pub id: String,
pub client: Client,
pub flow_id: String,
pub step_id: String,
pub status: String,
pub last_interaction_at: String,
pub updated_at: String,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbMemory {
pub id: String,
pub client: Client,
pub interaction_id: String,
pub conversation_id: String,
pub flow_id: String,
pub step_id: String,
pub memory_order: i32,
pub interaction_order: i32,
pub key: String,
pub value: serde_json::Value,
pub expires_at: Option<String>,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbMessage {
pub id: String,
pub client: Client,
pub conversation_id: String,
pub flow_id: String,
pub step_id: String,
pub message_order: i32,
pub interaction_order: i32,
pub direction: String,
pub payload: serde_json::Value,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbState {
pub id: String,
pub client: Client,
#[serde(rename = "type")]
pub _type: String,
pub value: serde_json::Value,
pub expires_at: Option<String>,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbBot {
pub id: String,
pub bot_id: String,
pub bot: String,
pub engine_version: String,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BotVersion {
pub bot: CsmlBot,
pub version_id: String,
pub engine_version: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BotVersionCreated {
pub version_id: String,
pub engine_version: String,
}
impl BotVersion {
pub fn flatten(&self) -> serde_json::Value {
let mut value = self.bot.to_json();
value["version_id"] = serde_json::json!(self.version_id);
value["engine_version"] = serde_json::json!(self.engine_version);
value
}
}
#[cfg(feature = "mongo")]
pub fn is_mongodb() -> bool {
match std::env::var("ENGINE_DB_TYPE") {
Ok(val) => val == "mongodb".to_owned(),
Err(_) => true,
}
}
#[cfg(feature = "dynamo")]
pub fn is_dynamodb() -> bool {
match std::env::var("ENGINE_DB_TYPE") {
Ok(val) => val == "dynamodb".to_owned(),
Err(_) => false,
}
}
#[cfg(feature = "postgresql")]
pub fn is_postgresql() -> bool {
match std::env::var("ENGINE_DB_TYPE") {
Ok(val) => val == "postgresql".to_owned(),
Err(_) => false,
}
}
#[cfg(feature = "sqlite")]
pub fn is_sqlite() -> bool {
match std::env::var("ENGINE_DB_TYPE") {
Ok(val) => val == "sqlite".to_owned(),
Err(_) => false,
}
}
pub fn init_db() -> Result<Database, EngineError> {
#[cfg(feature = "mongo")]
if is_mongodb() {
return mongodb_connector::init();
}
#[cfg(feature = "dynamo")]
if is_dynamodb() {
return dynamodb_connector::init();
}
#[cfg(feature = "postgresql")]
if is_postgresql() {
return postgresql_connector::init();
}
#[cfg(feature = "sqlite")]
if is_sqlite() {
return sqlite_connector::init();
}
Err(EngineError::Manager(ERROR_DB_SETUP.to_owned()))
}
pub fn make_migrations() -> Result<(), EngineError> {
#[cfg(feature = "postgresql")]
if is_postgresql() {
return self::postgresql::make_migrations();
}
#[cfg(feature = "sqlite")]
if is_sqlite() {
return self::sqlite::make_migrations();
}
Ok(())
}