vantage_dataset/traits/valueset.rs
1use std::pin::Pin;
2
3use crate::record::ActiveRecord;
4
5use super::Result;
6use async_trait::async_trait;
7use futures_core::Stream;
8use indexmap::IndexMap;
9use vantage_types::Record;
10
11/// Foundation trait for all dataset operations, defining the basic types used for storage.
12/// Typically you would implement ValueSet in combination with:
13///
14/// - [`ReadableValueSet`]
15/// - [`WritableValueSet`]
16/// - [`InsertableValueSet`]
17///
18/// `ValueSet` establishes the contract for working with raw storage values, providing
19/// the building blocks that higher-level [`DataSet`](super::DataSet) traits build upon.
20/// This separation allows the same storage backend to work with both typed entities
21/// and raw values efficiently.
22///
23/// # Type Parameters
24///
25/// - `Id`: Unique identifier type chosen by the storage implementation
26/// - `Value`: Raw storage value type, typically JSON-like structures
27///
28/// # Example
29///
30/// ```rust,ignore
31/// use vantage_dataset::{ReadableValueSet, ValueSet, prelude::*};
32/// use vantage_types::Record;
33/// use serde_json::Value;
34///
35/// struct CsvFile {
36/// filename: String,
37/// }
38///
39/// impl ValueSet for CsvFile {
40/// type Id = String;
41/// type Value = serde_json::Value;
42/// }
43///
44/// #[async_trait]
45/// impl ReadableValueSet for CsvFile {
46/// async fn list_values(&self) -> Result<IndexMap<Self::Id, Record<Self::Value>>> {
47/// // Parse CSV and return as JSON values
48/// // Implementation details...
49/// }
50///
51/// async fn get_value(&self, id: &Self::Id) -> Result<Self::Value> {
52/// // Find specific record by ID
53/// // Implementation details...
54/// }
55///
56/// async fn get_some_value(&self) -> Result<Option<(Self::Id, Self::Value)>> {
57/// // Return first record if any exists
58/// // Implementation details...
59/// }
60/// }
61/// ```
62#[async_trait]
63pub trait ValueSet {
64 /// Unique identifier type for records in this storage backend.
65 ///
66 /// Common choices:
67 /// - `String` for most databases and APIs
68 /// - `uuid::Uuid` if database does not support other types of IDs.
69 /// - Database-specific types like `surrealdb::sql::Thing`
70 type Id: Send + Sync + Clone;
71
72 /// Raw storage value type, representing data as stored in the backend, like
73 /// serde_json::Value or cborium::Value. Can also be a custom type.
74 type Value: Send + Sync + Clone;
75}
76
77/// Read-only access to raw storage values without entity deserialization.
78///
79/// See documentation for [`ValueSet`] for implementation example.
80#[async_trait]
81pub trait ReadableValueSet: ValueSet {
82 /// Retrieve all records as raw storage values preserving insertion order where supported.
83 ///
84 /// # Performance
85 /// In Vantage you can't retrieve values of a Set partially. Instead you should
86 /// create a sub-set of your existing set, then list values of that set instead.
87 async fn list_values(&self) -> Result<IndexMap<Self::Id, Record<Self::Value>>>;
88
89 /// Retrieve a specific record by ID as a structured record.
90 ///
91 /// Returns `Ok(None)` when no record exists with the given ID. Returns an
92 /// error only if the lookup itself fails.
93 async fn get_value(&self, id: &Self::Id) -> Result<Option<Record<Self::Value>>>;
94
95 /// Retrieve one single record from the set. If records are ordered - return first record.
96 /// will return Ok(None).
97 ///
98 /// Useful when you operate with a very specific subset of data.
99 async fn get_some_value(&self) -> Result<Option<(Self::Id, Record<Self::Value>)>>;
100
101 /// Stream all records as (Id, Record) pairs.
102 ///
103 /// Default wraps `list_values()`. Backends with native streaming
104 /// (e.g. paginated REST APIs) can override for incremental fetching.
105 #[allow(clippy::type_complexity)]
106 fn stream_values(
107 &self,
108 ) -> Pin<Box<dyn Stream<Item = Result<(Self::Id, Record<Self::Value>)>> + Send + '_>>
109 where
110 Self: Sync,
111 {
112 Box::pin(async_stream::stream! {
113 let records = self.list_values().await;
114 match records {
115 Ok(map) => {
116 for item in map {
117 yield Ok(item);
118 }
119 }
120 Err(e) => yield Err(e),
121 }
122 })
123 }
124}
125
126/// Write operations on raw storage values with idempotent behavior.
127///
128/// See documentation for [`ValueSet`] for implementation example.
129#[async_trait]
130pub trait WritableValueSet: ValueSet {
131 /// Insert value with a specific ID (often generated) (HTTP POST with ID)
132 ///
133 /// **Idempotent**: Succeeds if no record exists with the given ID. If
134 /// record already exists, must return success without overwriting
135 /// data, returning original data.
136 ///
137 /// **Returns**: Record as it was stored.
138 ///
139 /// # Use Case
140 /// Generate unique ID and store record while avoiding duplicates.
141 async fn insert_value(
142 &self,
143 id: &Self::Id,
144 record: &Record<Self::Value>,
145 ) -> Result<Record<Self::Value>>;
146
147 /// Replace the entire record at the specified ID (HTTP PUT)
148 ///
149 /// **Idempotent**: Always succeeds, completely overwrites existing data
150 /// if present. If possible, will remove/recreate record; therefore if
151 /// `record` doesn't contain certain attributes which were present in the
152 /// database, those will be removed. If record does not exist, will
153 /// create it.
154 ///
155 /// **Returns**: Record as it was stored.
156 ///
157 /// # Use Case
158 /// Replace with a new version of a record.
159 async fn replace_value(
160 &self,
161 id: &Self::Id,
162 record: &Record<Self::Value>,
163 ) -> Result<Record<Self::Value>>;
164
165 /// Partially update a record by merging with the provided value (HTTP PATCH)
166 ///
167 /// **Fails if record doesn't exist**. The exact merge behavior depends on
168 /// the storage implementation - typically merges object fields for JSON-like values.
169 ///
170 /// **Returns**: Record as it was stored (not only the partial change).
171 ///
172 /// # Use Case
173 /// Update only the modified fields of a record.
174 async fn patch_value(
175 &self,
176 id: &Self::Id,
177 partial: &Record<Self::Value>,
178 ) -> Result<Record<Self::Value>>;
179
180 /// Delete a record by ID (HTTP DELETE)
181 ///
182 /// **Idempotent**: Always succeeds, even if the record doesn't exist.
183 /// This allows safe cleanup operations without checking existence first.
184 async fn delete(&self, id: &Self::Id) -> Result<()>;
185
186 /// Delete all records in the set (HTTP DELETE without ID)
187 ///
188 /// **Idempotent**: All records in the set will be deleted.
189 /// Executing several times is OK.
190 ///
191 /// Execute on a subset of your entire database.
192 async fn delete_all(&self) -> Result<()>;
193}
194
195/// Append-only operations on raw storage values with automatic ID generation.
196///
197/// See documentation for [`ValueSet`] for implementation example.
198#[async_trait]
199pub trait InsertableValueSet: ValueSet {
200 /// Insert a value and return the generated ID (Similar to HTTP POST without ID)
201 ///
202 /// The storage backend generates a unique identifier for the new record.
203 ///
204 /// # Warning
205 ///
206 /// This method is **not idempotent** - each call creates a new record with
207 /// a new ID, even if the value data is identical.
208 async fn insert_return_id_value(&self, record: &Record<Self::Value>) -> Result<Self::Id>;
209}
210
211/// Change tracking for raw storage values with automatic persistence.
212///
213/// See documentation for [`ValueSet`] for implementation example.
214#[async_trait]
215pub trait ActiveRecordSet: ReadableValueSet + WritableValueSet {
216 /// Retrieve a record wrapped for change tracking and deferred persistence.
217 ///
218 /// The returned `RecordValue` can be modified in-place and will track all
219 /// changes for efficient persistence when `save()` is called.
220 ///
221 /// # Returns
222 ///
223 /// - `Ok(Some(RecordValue))`: Record wrapper with change tracking
224 /// - `Ok(None)`: No record with this ID
225 /// - `Err`: If the lookup itself fails
226 async fn get_value_record(&self, id: &Self::Id) -> Result<Option<ActiveRecord<'_, Self>>>;
227
228 /// Retrieve all records wrapped for change tracking.
229 ///
230 /// Each returned `RecordValue` operates independently - modifications to one
231 /// record don't affect others, and each must be saved separately.
232 ///
233 /// # Performance Note
234 ///
235 /// This loads all records into memory. Consider pagination or streaming
236 /// approaches for large datasets.
237 async fn list_value_records(&self) -> Result<Vec<ActiveRecord<'_, Self>>>;
238}
239
240// Auto-implement for any type that has both readable and writable traits
241#[async_trait]
242impl<T> ActiveRecordSet for T
243where
244 T: ReadableValueSet + WritableValueSet + Sync,
245{
246 async fn get_value_record(&self, id: &Self::Id) -> Result<Option<ActiveRecord<'_, Self>>> {
247 Ok(self
248 .get_value(id)
249 .await?
250 .map(|record| ActiveRecord::new(id.clone(), record, self)))
251 }
252
253 async fn list_value_records(&self) -> Result<Vec<ActiveRecord<'_, Self>>> {
254 let items = self.list_values().await?;
255
256 Ok(items
257 .into_iter()
258 .map(|(id, record)| ActiveRecord::new(id, record, self))
259 .collect::<Vec<_>>())
260 }
261}