Skip to main content

surrealdb_rpc/
export.rs

1//! Configuration for database exports, shared by the export engine and the
2//! clients that request one over the wire.
3
4use surrealdb_types::SurrealValue;
5
6#[derive(Clone, Debug, SurrealValue)]
7#[surreal(crate = "surrealdb_types")]
8#[surreal(default)]
9pub struct Config {
10	/// Whether the database's own definition — its `STRICT` flag, `COMMENT` and
11	/// `CHANGEFEED` — is carried.
12	///
13	/// Restoring it rewrites those clauses on the target database, so a selective
14	/// export turns it off along with every other section it did not ask for.
15	pub database_definition: bool,
16	pub users: bool,
17	pub accesses: bool,
18	pub params: bool,
19	pub functions: bool,
20	pub analyzers: bool,
21	pub apis: bool,
22	pub buckets: bool,
23	pub modules: bool,
24	pub configs: bool,
25	pub tables: TableConfig,
26	pub versions: bool,
27	pub records: bool,
28	pub sequences: bool,
29}
30
31impl Default for Config {
32	fn default() -> Config {
33		Config {
34			database_definition: true,
35			users: true,
36			accesses: true,
37			params: true,
38			functions: true,
39			analyzers: true,
40			apis: true,
41			buckets: true,
42			modules: true,
43			configs: true,
44			tables: TableConfig::default(),
45			versions: false,
46			records: true,
47			sequences: true,
48		}
49	}
50}
51
52/// Named-field wrapper so that the untagged `SurrealValue` serialization
53/// can differentiate `Exclude` from `Some` (include).
54#[derive(Clone, Debug, SurrealValue)]
55#[surreal(crate = "surrealdb_types")]
56pub struct ExcludedTables {
57	pub exclude: Vec<String>,
58}
59
60#[derive(Clone, Debug, Default, SurrealValue)]
61#[surreal(crate = "surrealdb_types")]
62#[surreal(untagged)]
63pub enum TableConfig {
64	#[default]
65	#[surreal(value = true)]
66	All,
67	#[surreal(value = false)]
68	None,
69	Some(Vec<String>),
70	Exclude(ExcludedTables),
71}
72
73// `From<bool>` exists so the SDK's `ExportBuilder::tables(impl Into<TableConfig>)`
74// accepts `tables(true)` / `tables(false)` directly. The semantics are
75// documented at the call site (`surrealdb/src/method/export.rs::tables`):
76// `true` selects all tables, `false` selects none.
77impl From<bool> for TableConfig {
78	fn from(value: bool) -> Self {
79		match value {
80			true => TableConfig::All,
81			false => TableConfig::None,
82		}
83	}
84}
85
86impl From<Vec<String>> for TableConfig {
87	fn from(value: Vec<String>) -> Self {
88		TableConfig::Some(value)
89	}
90}
91
92impl From<Vec<&str>> for TableConfig {
93	fn from(value: Vec<&str>) -> Self {
94		TableConfig::Some(value.into_iter().map(ToOwned::to_owned).collect())
95	}
96}
97
98impl TableConfig {
99	/// Check if we should export tables
100	pub fn is_any(&self) -> bool {
101		matches!(self, Self::All | Self::Some(_) | Self::Exclude(_))
102	}
103
104	/// Check if we should export a specific table
105	pub fn includes(&self, table: &str) -> bool {
106		match self {
107			Self::All => true,
108			Self::None => false,
109			Self::Some(v) => v.iter().any(|v| v.eq(table)),
110			Self::Exclude(v) => !v.exclude.iter().any(|v| v.eq(table)),
111		}
112	}
113
114	/// Returns the explicitly listed table names, if any.
115	pub fn names(&self) -> Option<&[String]> {
116		match self {
117			Self::Some(v) => Some(v.as_slice()),
118			Self::Exclude(v) => Some(v.exclude.as_slice()),
119			_ => None,
120		}
121	}
122}