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	pub users: bool,
11	pub accesses: bool,
12	pub params: bool,
13	pub functions: bool,
14	pub analyzers: bool,
15	pub apis: bool,
16	pub buckets: bool,
17	pub modules: bool,
18	pub configs: bool,
19	pub tables: TableConfig,
20	pub versions: bool,
21	pub records: bool,
22	pub sequences: bool,
23}
24
25impl Default for Config {
26	fn default() -> Config {
27		Config {
28			users: true,
29			accesses: true,
30			params: true,
31			functions: true,
32			analyzers: true,
33			apis: true,
34			buckets: true,
35			modules: true,
36			configs: true,
37			tables: TableConfig::default(),
38			versions: false,
39			records: true,
40			sequences: true,
41		}
42	}
43}
44
45/// Named-field wrapper so that the untagged `SurrealValue` serialization
46/// can differentiate `Exclude` from `Some` (include).
47#[derive(Clone, Debug, SurrealValue)]
48#[surreal(crate = "surrealdb_types")]
49pub struct ExcludedTables {
50	pub exclude: Vec<String>,
51}
52
53#[derive(Clone, Debug, Default, SurrealValue)]
54#[surreal(crate = "surrealdb_types")]
55#[surreal(untagged)]
56pub enum TableConfig {
57	#[default]
58	#[surreal(value = true)]
59	All,
60	#[surreal(value = false)]
61	None,
62	Some(Vec<String>),
63	Exclude(ExcludedTables),
64}
65
66// `From<bool>` exists so the SDK's `ExportBuilder::tables(impl Into<TableConfig>)`
67// accepts `tables(true)` / `tables(false)` directly. The semantics are
68// documented at the call site (`surrealdb/src/method/export.rs::tables`):
69// `true` selects all tables, `false` selects none.
70impl From<bool> for TableConfig {
71	fn from(value: bool) -> Self {
72		match value {
73			true => TableConfig::All,
74			false => TableConfig::None,
75		}
76	}
77}
78
79impl From<Vec<String>> for TableConfig {
80	fn from(value: Vec<String>) -> Self {
81		TableConfig::Some(value)
82	}
83}
84
85impl From<Vec<&str>> for TableConfig {
86	fn from(value: Vec<&str>) -> Self {
87		TableConfig::Some(value.into_iter().map(ToOwned::to_owned).collect())
88	}
89}
90
91impl TableConfig {
92	/// Check if we should export tables
93	pub fn is_any(&self) -> bool {
94		matches!(self, Self::All | Self::Some(_) | Self::Exclude(_))
95	}
96
97	/// Check if we should export a specific table
98	pub fn includes(&self, table: &str) -> bool {
99		match self {
100			Self::All => true,
101			Self::None => false,
102			Self::Some(v) => v.iter().any(|v| v.eq(table)),
103			Self::Exclude(v) => !v.exclude.iter().any(|v| v.eq(table)),
104		}
105	}
106
107	/// Returns the explicitly listed table names, if any.
108	pub fn names(&self) -> Option<&[String]> {
109		match self {
110			Self::Some(v) => Some(v.as_slice()),
111			Self::Exclude(v) => Some(v.exclude.as_slice()),
112			_ => None,
113		}
114	}
115}