surrealdb/api/opt/
config.rs

1use crate::{dbs::Capabilities, iam::Level};
2use std::time::Duration;
3
4/// Configuration for server connection, including: strictness, notifications, query_timeout, transaction_timeout
5#[derive(Debug, Clone, Default)]
6pub struct Config {
7	pub(crate) strict: bool,
8	pub(crate) notifications: bool,
9	pub(crate) query_timeout: Option<Duration>,
10	pub(crate) transaction_timeout: Option<Duration>,
11	#[cfg(any(feature = "native-tls", feature = "rustls"))]
12	pub(crate) tls_config: Option<super::Tls>,
13	// Only used by the local engines
14	// `Level::No` in this context means no authentication information was configured
15	pub(crate) auth: Level,
16	pub(crate) username: String,
17	pub(crate) password: String,
18	pub(crate) tick_interval: Option<Duration>,
19	pub(crate) capabilities: Capabilities,
20}
21
22impl Config {
23	/// Create a default config that can be modified to configure a connection
24	pub fn new() -> Self {
25		Default::default()
26	}
27
28	/// Set the strict value of the config to the supplied value
29	pub fn set_strict(mut self, strict: bool) -> Self {
30		self.strict = strict;
31		self
32	}
33
34	/// Enables `strict` server mode
35	pub fn strict(mut self) -> Self {
36		self.strict = true;
37		self
38	}
39
40	/// Set the notifications value of the config to the supplied value
41	#[deprecated(
42		since = "1.1.0",
43		note = "Moved to `Capabilities::with_live_query_notifications()`"
44	)]
45	pub fn set_notifications(mut self, notifications: bool) -> Self {
46		self.notifications = notifications;
47		self
48	}
49
50	/// Set the config to use notifications
51	#[deprecated(
52		since = "1.1.0",
53		note = "Moved to `Capabilities::with_live_query_notifications()`"
54	)]
55	pub fn notifications(mut self) -> Self {
56		self.notifications = true;
57		self
58	}
59
60	/// Set the query timeout of the config
61	pub fn query_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
62		self.query_timeout = timeout.into();
63		self
64	}
65
66	/// Set the transaction timeout of the config
67	pub fn transaction_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
68		self.transaction_timeout = timeout.into();
69		self
70	}
71
72	/// Set the default user
73	pub fn user(mut self, user: crate::opt::auth::Root<'_>) -> Self {
74		self.auth = Level::Root;
75		self.username = user.username.to_owned();
76		self.password = user.password.to_owned();
77		self
78	}
79
80	/// Use Rustls to configure TLS connections
81	#[cfg(feature = "rustls")]
82	#[cfg_attr(docsrs, doc(cfg(feature = "rustls")))]
83	pub fn rustls(mut self, config: rustls::ClientConfig) -> Self {
84		self.tls_config = Some(super::Tls::Rust(config));
85		self
86	}
87
88	/// Use native TLS to configure TLS connections
89	#[cfg(feature = "native-tls")]
90	#[cfg_attr(docsrs, doc(cfg(feature = "native-tls")))]
91	pub fn native_tls(mut self, config: native_tls::TlsConnector) -> Self {
92		self.tls_config = Some(super::Tls::Native(config));
93		self
94	}
95
96	/// Set the interval at which the database should run node maintenance tasks
97	pub fn tick_interval(mut self, interval: impl Into<Option<Duration>>) -> Self {
98		self.tick_interval = interval.into().filter(|x| !x.is_zero());
99		self
100	}
101
102	/// Set the capabilities for the database
103	pub fn capabilities(mut self, capabilities: Capabilities) -> Self {
104		self.capabilities = capabilities;
105		self
106	}
107}