2024-06-17 14:30:30 +00:00
|
|
|
use crate::opt::capabilities::Capabilities;
|
2024-03-28 16:29:55 +00:00
|
|
|
#[cfg(any(
|
2024-05-23 13:04:20 +00:00
|
|
|
feature = "kv-mem",
|
2024-03-28 16:29:55 +00:00
|
|
|
feature = "kv-surrealkv",
|
|
|
|
feature = "kv-rocksdb",
|
|
|
|
feature = "kv-fdb",
|
|
|
|
feature = "kv-tikv",
|
|
|
|
))]
|
|
|
|
use std::path::PathBuf;
|
2023-07-26 11:54:48 +00:00
|
|
|
use std::time::Duration;
|
2024-06-17 14:30:30 +00:00
|
|
|
use surrealdb_core::{dbs::Capabilities as CoreCapabilities, iam::Level};
|
2023-07-26 11:54:48 +00:00
|
|
|
|
|
|
|
/// Configuration for server connection, including: strictness, notifications, query_timeout, transaction_timeout
|
2023-10-10 06:01:21 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2023-07-26 11:54:48 +00:00
|
|
|
pub struct Config {
|
|
|
|
pub(crate) strict: bool,
|
|
|
|
pub(crate) notifications: bool,
|
|
|
|
pub(crate) query_timeout: Option<Duration>,
|
|
|
|
pub(crate) transaction_timeout: Option<Duration>,
|
2023-08-22 09:23:49 +00:00
|
|
|
#[cfg(any(feature = "native-tls", feature = "rustls"))]
|
|
|
|
pub(crate) tls_config: Option<super::Tls>,
|
|
|
|
// Only used by the local engines
|
|
|
|
// `Level::No` in this context means no authentication information was configured
|
|
|
|
pub(crate) auth: Level,
|
|
|
|
pub(crate) username: String,
|
|
|
|
pub(crate) password: String,
|
2023-08-25 07:55:22 +00:00
|
|
|
pub(crate) tick_interval: Option<Duration>,
|
2024-06-17 14:30:30 +00:00
|
|
|
pub(crate) capabilities: CoreCapabilities,
|
2024-03-28 16:29:55 +00:00
|
|
|
#[cfg(any(
|
2024-05-23 13:04:20 +00:00
|
|
|
feature = "kv-mem",
|
2024-03-28 16:29:55 +00:00
|
|
|
feature = "kv-surrealkv",
|
|
|
|
feature = "kv-rocksdb",
|
|
|
|
feature = "kv-fdb",
|
|
|
|
feature = "kv-tikv",
|
|
|
|
))]
|
|
|
|
pub(crate) temporary_directory: Option<PathBuf>,
|
2023-07-26 11:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2023-08-22 09:23:49 +00:00
|
|
|
/// Create a default config that can be modified to configure a connection
|
2023-07-26 11:54:48 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2023-08-22 09:23:49 +00:00
|
|
|
/// Set the strict value of the config to the supplied value
|
2023-07-26 11:54:48 +00:00
|
|
|
pub fn set_strict(mut self, strict: bool) -> Self {
|
|
|
|
self.strict = strict;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-22 09:23:49 +00:00
|
|
|
/// Enables `strict` server mode
|
2023-07-26 11:54:48 +00:00
|
|
|
pub fn strict(mut self) -> Self {
|
|
|
|
self.strict = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-22 09:23:49 +00:00
|
|
|
/// Set the notifications value of the config to the supplied value
|
2023-11-13 17:19:47 +00:00
|
|
|
#[deprecated(
|
|
|
|
since = "1.1.0",
|
|
|
|
note = "Moved to `Capabilities::with_live_query_notifications()`"
|
|
|
|
)]
|
2023-07-26 11:54:48 +00:00
|
|
|
pub fn set_notifications(mut self, notifications: bool) -> Self {
|
|
|
|
self.notifications = notifications;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-22 09:23:49 +00:00
|
|
|
/// Set the config to use notifications
|
2023-11-13 17:19:47 +00:00
|
|
|
#[deprecated(
|
|
|
|
since = "1.1.0",
|
|
|
|
note = "Moved to `Capabilities::with_live_query_notifications()`"
|
|
|
|
)]
|
2023-07-26 11:54:48 +00:00
|
|
|
pub fn notifications(mut self) -> Self {
|
|
|
|
self.notifications = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-22 09:23:49 +00:00
|
|
|
/// Set the query timeout of the config
|
2023-07-26 11:54:48 +00:00
|
|
|
pub fn query_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
|
|
|
|
self.query_timeout = timeout.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-22 09:23:49 +00:00
|
|
|
/// Set the transaction timeout of the config
|
2023-07-26 11:54:48 +00:00
|
|
|
pub fn transaction_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
|
|
|
|
self.transaction_timeout = timeout.into();
|
|
|
|
self
|
|
|
|
}
|
2023-08-22 09:23:49 +00:00
|
|
|
|
|
|
|
/// Set the default user
|
|
|
|
pub fn user(mut self, user: crate::opt::auth::Root<'_>) -> Self {
|
|
|
|
self.auth = Level::Root;
|
2024-03-22 20:50:11 +00:00
|
|
|
user.username.clone_into(&mut self.username);
|
|
|
|
user.password.clone_into(&mut self.password);
|
2023-08-22 09:23:49 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use Rustls to configure TLS connections
|
|
|
|
#[cfg(feature = "rustls")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "rustls")))]
|
|
|
|
pub fn rustls(mut self, config: rustls::ClientConfig) -> Self {
|
|
|
|
self.tls_config = Some(super::Tls::Rust(config));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use native TLS to configure TLS connections
|
|
|
|
#[cfg(feature = "native-tls")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "native-tls")))]
|
|
|
|
pub fn native_tls(mut self, config: native_tls::TlsConnector) -> Self {
|
|
|
|
self.tls_config = Some(super::Tls::Native(config));
|
|
|
|
self
|
|
|
|
}
|
2023-08-25 07:55:22 +00:00
|
|
|
|
|
|
|
/// Set the interval at which the database should run node maintenance tasks
|
|
|
|
pub fn tick_interval(mut self, interval: impl Into<Option<Duration>>) -> Self {
|
|
|
|
self.tick_interval = interval.into().filter(|x| !x.is_zero());
|
|
|
|
self
|
|
|
|
}
|
2023-08-30 18:01:30 +00:00
|
|
|
|
|
|
|
/// Set the capabilities for the database
|
|
|
|
pub fn capabilities(mut self, capabilities: Capabilities) -> Self {
|
2024-06-17 14:30:30 +00:00
|
|
|
self.capabilities = capabilities.build();
|
2023-08-30 18:01:30 +00:00
|
|
|
self
|
|
|
|
}
|
2024-05-23 13:04:20 +00:00
|
|
|
|
2024-03-28 16:29:55 +00:00
|
|
|
#[cfg(any(
|
2024-05-23 13:04:20 +00:00
|
|
|
feature = "kv-mem",
|
2024-03-28 16:29:55 +00:00
|
|
|
feature = "kv-surrealkv",
|
|
|
|
feature = "kv-rocksdb",
|
|
|
|
feature = "kv-fdb",
|
|
|
|
feature = "kv-tikv",
|
|
|
|
))]
|
|
|
|
pub fn temporary_directory(mut self, path: Option<PathBuf>) -> Self {
|
|
|
|
self.temporary_directory = path;
|
|
|
|
self
|
|
|
|
}
|
2023-07-26 11:54:48 +00:00
|
|
|
}
|