surrealdb/api/opt/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::opt::capabilities::Capabilities;
#[cfg(storage)]
use std::path::PathBuf;
use std::time::Duration;
use surrealdb_core::{dbs::Capabilities as CoreCapabilities, iam::Level};

/// Configuration for server connection, including: strictness, notifications, query_timeout, transaction_timeout
#[derive(Debug, Clone, Default)]
pub struct Config {
	pub(crate) strict: bool,
	pub(crate) ast_payload: bool,
	pub(crate) query_timeout: Option<Duration>,
	pub(crate) transaction_timeout: Option<Duration>,
	#[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,
	pub(crate) capabilities: CoreCapabilities,
	#[cfg(storage)]
	pub(crate) temporary_directory: Option<PathBuf>,
	pub(crate) node_membership_refresh_interval: Option<Duration>,
	pub(crate) node_membership_check_interval: Option<Duration>,
	pub(crate) node_membership_cleanup_interval: Option<Duration>,
	pub(crate) changefeed_gc_interval: Option<Duration>,
}

impl Config {
	/// Create a default config that can be modified to configure a connection
	pub fn new() -> Self {
		Default::default()
	}

	/// Set the strict value of the config to the supplied value
	pub fn set_strict(mut self, strict: bool) -> Self {
		self.strict = strict;
		self
	}

	/// Enables `strict` server mode
	pub fn strict(mut self) -> Self {
		self.strict = true;
		self
	}

	/// Whether to send queries as AST
	pub fn set_ast_payload(mut self, ast_payload: bool) -> Self {
		self.ast_payload = ast_payload;
		self
	}

	/// Send queries as AST
	pub fn ast_payload(mut self) -> Self {
		self.ast_payload = true;
		self
	}

	/// Set the query timeout of the config
	pub fn query_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
		self.query_timeout = timeout.into();
		self
	}

	/// Set the transaction timeout of the config
	pub fn transaction_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
		self.transaction_timeout = timeout.into();
		self
	}

	/// Set the default user
	pub fn user(mut self, user: crate::opt::auth::Root<'_>) -> Self {
		self.auth = Level::Root;
		user.username.clone_into(&mut self.username);
		user.password.clone_into(&mut self.password);
		self
	}

	/// Use Rustls to configure TLS connections
	///
	/// WARNING: `rustls` is not stable yet. As we may need to upgrade this dependency from time to time
	/// to keep up with its security fixes, this method is excluded from our stability guarantee.
	#[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
	///
	/// WARNING: `native-tls` is not stable yet. As we may need to upgrade this dependency from time to time
	/// to keep up with its security fixes, this method is excluded from our stability guarantee.
	#[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
	}

	/// Set the capabilities for the database
	pub fn capabilities(mut self, capabilities: Capabilities) -> Self {
		self.capabilities = capabilities.build();
		self
	}

	/// Get the capabilities for the database
	/// Used internally in the CLI to pass on capabilities to the parser
	#[doc(hidden)]
	pub fn get_capabilities(&self) -> &CoreCapabilities {
		&self.capabilities
	}

	#[cfg(storage)]
	pub fn temporary_directory(mut self, path: Option<PathBuf>) -> Self {
		self.temporary_directory = path;
		self
	}

	/// Set the interval at which the database should run node maintenance tasks
	pub fn node_membership_refresh_interval(
		mut self,
		interval: impl Into<Option<Duration>>,
	) -> Self {
		self.node_membership_refresh_interval = interval.into().filter(|x| !x.is_zero());
		self
	}

	/// Set the interval at which the database should run node maintenance tasks
	pub fn node_membership_check_interval(mut self, interval: impl Into<Option<Duration>>) -> Self {
		self.node_membership_check_interval = interval.into().filter(|x| !x.is_zero());
		self
	}

	/// Set the interval at which the database should run node maintenance tasks
	pub fn node_membership_cleanup_interval(
		mut self,
		interval: impl Into<Option<Duration>>,
	) -> Self {
		self.node_membership_cleanup_interval = interval.into().filter(|x| !x.is_zero());
		self
	}

	/// Set the interval at which the database should run node maintenance tasks
	pub fn changefeed_gc_interval(mut self, interval: impl Into<Option<Duration>>) -> Self {
		self.changefeed_gc_interval = interval.into().filter(|x| !x.is_zero());
		self
	}
}