dynamodb-facade 0.1.1

A typed facade over aws-sdk-dynamodb with expression builders and batch/transaction support
Documentation
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use std::future::{Future, IntoFuture};
use std::pin::Pin;

use super::*;

use aws_sdk_dynamodb::operation::delete_item::builders::DeleteItemFluentBuilder;

/// Builder for a DynamoDB `DeleteItem` request.
///
/// Constructed via [`DynamoDBItemOp::delete`] / [`DynamoDBItemOp::delete_by_id`]
/// (typed, with a concrete `T`) or [`DeleteItemRequest::new`] (stand-alone,
/// raw output). The builder provides:
///
/// - **Output format** — the result can be deserialized into `T`.
///   Call [`.raw()`][DeleteItemRequest::raw] to receive an untyped [`Item<TD>`]
///   instead (one-way).
/// - **Return value** — Call [`.return_old()`][DeleteItemRequest::return_old]
///   to request the deleted item, or [`.return_none()`][DeleteItemRequest::return_none]
///   to return nothing.
/// - **Condition** — optionally add a guard expression via
///   [`.condition()`][DeleteItemRequest::condition], or
///   [`.exists()`][DeleteItemRequest::exists].
///
/// The builder implements [`IntoFuture`], so it can
/// be `.await`ed directly.
///
/// # Errors
///
/// Returns [`Err`] if the DynamoDB request fails or if a condition
/// expression is set and the check fails
/// (`ConditionalCheckFailedException`).
///
/// # Examples
///
/// ```no_run
/// # use dynamodb_facade::test_fixtures::*;
/// use dynamodb_facade::{DynamoDBItemOp, Condition, KeyId};
///
/// # async fn example(cclient: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
/// let enrollment = sample_enrollment();
///
/// # let client = cclient.clone();
/// // Simple delete
/// enrollment.delete(client).await?;
///
/// # let client = cclient.clone();
/// // Delete only if the item exists
/// enrollment.delete(client).exists().await?;
///
/// # let client = cclient.clone();
/// // Delete with a custom condition
/// enrollment
///     .delete(client)
///     .condition(Enrollment::exists() & Condition::not_exists("completed_at"))
///     .await?;
///
/// # let client = cclient.clone();
/// // Delete and return the old item
/// let old /* : Option<Enrollment> */ = enrollment.delete(client).return_old().await?;
///
/// # let client = cclient.clone();
/// // Delete by ID and return the old item
/// let old /* : Option<Enrollment> */ = Enrollment::delete_by_id(
///     client,
///     KeyId::pk("user-1").sk("course-42"),
/// )
/// .await?;
/// # Ok(())
/// # }
/// ```
#[must_use = "builder does nothing until awaited or executed"]
pub struct DeleteItemRequest<
    TD: TableDefinition,
    T = (),
    O: OutputFormat = Raw,
    R: ReturnValue = ReturnNothing,
    C: ConditionState = NoCondition,
> {
    builder: DeleteItemFluentBuilder,
    _marker: PhantomData<(TD, T, O, R, C)>,
}

// -- Common methods (all states) --------------------------------------------

impl<TD: TableDefinition, T, O: OutputFormat, R: ReturnValue, C: ConditionState>
    DeleteItemRequest<TD, T, O, R, C>
{
    /// Consumes the builder and returns the underlying SDK
    /// [`DeleteItemFluentBuilder`].
    ///
    /// Use this escape hatch when you need to set options not exposed by this
    /// facade, or when integrating with code that expects the raw SDK builder.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::DynamoDBItemOp;
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// let sdk_builder = sample_enrollment().delete(client).into_inner();
    /// // configure sdk_builder further, then call .send().await
    /// # Ok(())
    /// # }
    /// ```
    pub fn into_inner(self) -> DeleteItemFluentBuilder {
        self.builder
    }
}

// -- Stand-alone constructor (ReturnNothing, NoCondition, T = (), O = Raw)

impl<TD: TableDefinition> DeleteItemRequest<TD> {
    /// Creates a stand-alone `DeleteItemRequest` with raw output (`T = ()`, `O = Raw`).
    ///
    /// Use this when you already have a [`Key<TD>`] and do not need typed
    /// deserialization of the deleted value. For typed access, prefer
    /// [`DynamoDBItemOp::delete`] or [`DynamoDBItemOp::delete_by_id`] instead.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::DeleteItemRequest;
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// let key = sample_user_item().into_key_only();
    /// DeleteItemRequest::<PlatformTable>::new(client, key).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(client: aws_sdk_dynamodb::Client, key: Key<TD>) -> Self {
        Self::_new(client, key)
    }
}

// -- Constructor (any R, any O, any C) ------------------------------

impl<TD: TableDefinition, T, O: OutputFormat, R: ReturnValue, C: ConditionState>
    DeleteItemRequest<TD, T, O, R, C>
{
    /// Creates a new `DeleteItemRequest` targeting the given key.
    pub(super) fn _new(client: aws_sdk_dynamodb::Client, key: Key<TD>) -> Self {
        let table_name = TD::table_name();
        tracing::debug!(table_name, ?key, "DeleteItem");
        Self {
            builder: client
                .delete_item()
                .table_name(table_name)
                .set_key(Some(key.into_inner())),
            _marker: PhantomData,
        }
    }
}

// -- Return-value transitions (preserve O, C) -------------------------------

impl<TD: TableDefinition, T, O: OutputFormat, C: ConditionState>
    DeleteItemRequest<TD, T, O, ReturnNothing, C>
{
    /// Requests that DynamoDB return the item's attributes before deletion.
    ///
    /// When executed, [`execute`][DeleteItemRequest::execute] returns
    /// `Option<T>` (typed) or `Option<Item<TD>>` (raw) — `None` if no item
    /// existed at that key.
    ///
    /// Use [`.return_none()`][DeleteItemRequest::return_none] to revert.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::DynamoDBItemOp;
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// let old /* : Option<Enrollment> */ = sample_enrollment()
    ///     .delete(client)
    ///     .return_old()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn return_old(self) -> DeleteItemRequest<TD, T, O, Return<Old>, C> {
        tracing::debug!("DeleteItem return_old");
        DeleteItemRequest {
            builder: self.builder,
            _marker: PhantomData,
        }
    }
}

impl<TD: TableDefinition, T, O: OutputFormat, C: ConditionState>
    DeleteItemRequest<TD, T, O, Return<Old>, C>
{
    /// Reverts the return-value setting so that nothing is returned.
    ///
    /// After this call, [`execute`][DeleteItemRequest::execute] returns `()`
    /// instead of the deleted item.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::{DynamoDBItemOp, KeyId};
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// // delete_by_id defaults to Return<Old>; opt out with return_none
    /// Enrollment::delete_by_id(client, KeyId::pk("user-1").sk("course-42"))
    ///     .return_none()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn return_none(self) -> DeleteItemRequest<TD, T, O, ReturnNothing, C> {
        tracing::debug!("DeleteItem return_none");
        DeleteItemRequest {
            builder: self.builder,
            _marker: PhantomData,
        }
    }
}

// -- Condition (NoCondition only) -------------------------------------------

impl<TD: TableDefinition, T, O: OutputFormat, R: ReturnValue>
    DeleteItemRequest<TD, T, O, R, NoCondition>
{
    /// Adds a condition expression that must be satisfied for the delete to succeed.
    ///
    /// DynamoDB accepts a single condition expression per request, so this
    /// method can only be called once. If the condition fails at runtime,
    /// DynamoDB returns a `ConditionalCheckFailedException`.
    ///
    /// For the common item exists case, prefer the
    /// [`.exists()`][DeleteItemRequest::exists] shorthands.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::{DynamoDBItemOp, Condition};
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// // Delete only if the enrollment has not been completed
    /// sample_enrollment()
    ///     .delete(client)
    ///     .condition(Enrollment::exists() & Condition::not_exists("completed_at"))
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn condition(
        mut self,
        condition: Condition<'_>,
    ) -> DeleteItemRequest<TD, T, O, R, AlreadyHasCondition> {
        tracing::debug!(%condition, "DeleteItem condition");
        self.builder = condition.apply(self.builder);
        DeleteItemRequest {
            builder: self.builder,
            _marker: PhantomData,
        }
    }
}
impl<TD: TableDefinition, T: DynamoDBItem<TD>, O: OutputFormat, R: ReturnValue>
    DeleteItemRequest<TD, T, O, R, NoCondition>
{
    /// Adds an `attribute_exists(<PK>)` condition, requiring the item to exist before deletion.
    ///
    /// The delete fails with `ConditionalCheckFailedException` if the item does not exist.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::DynamoDBItemOp;
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// sample_enrollment().delete(client).exists().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn exists(self) -> DeleteItemRequest<TD, T, O, R, AlreadyHasCondition> {
        self.condition(T::exists())
    }
}

// -- Output format transition (preserve R, C) -------------------------------

impl<TD: TableDefinition, T, R: ReturnValue, C: ConditionState>
    DeleteItemRequest<TD, T, Typed, R, C>
{
    /// Switches the output format from `Typed` to `Raw`.
    ///
    /// After calling `.raw()`, [`execute`][DeleteItemRequest::execute] returns
    /// `Option<Item<TD>>` instead of `Option<T>` when `Return<Old>` is active.
    /// This transition is one-way.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::DynamoDBItemOp;
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// let old_raw /* : Option<Item<PlatformTable>> */ =
    ///     sample_enrollment()
    ///         .delete(client)
    ///         .return_old()
    ///         .raw()
    ///         .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn raw(self) -> DeleteItemRequest<TD, T, Raw, R, C> {
        DeleteItemRequest {
            builder: self.builder,
            _marker: PhantomData,
        }
    }
}

// -- Terminal: ReturnNothing (any O, any C) ---------------------------------

impl<TD: TableDefinition, T, O: OutputFormat, C: ConditionState>
    DeleteItemRequest<TD, T, O, ReturnNothing, C>
{
    /// Sends the `DeleteItem` request, returning nothing on success.
    ///
    /// This method is also available implicitly via `.await`.
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the DynamoDB request fails or if a condition
    /// expression is set and the check fails
    /// (`ConditionalCheckFailedException`).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::DynamoDBItemOp;
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// sample_enrollment().delete(client).exists().execute().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[tracing::instrument(level = "debug", skip(self), name = "delete_execute")]
    pub fn execute(self) -> impl Future<Output = Result<()>> + Send + 'static {
        let builder = self.builder;
        async move {
            builder.return_values(SDKReturnValue::None).send().await?;
            Ok(())
        }
    }
}

impl<TD: TableDefinition, T, O: OutputFormat, C: ConditionState> IntoFuture
    for DeleteItemRequest<TD, T, O, ReturnNothing, C>
{
    type Output = Result<()>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(self.execute())
    }
}

// -- Terminal: ReturnItem<Old> + Typed (any C) ------------------------------

impl<TD: TableDefinition, T: DynamoDBItem<TD> + DeserializeOwned, C: ConditionState>
    DeleteItemRequest<TD, T, Typed, Return<Old>, C>
{
    /// Sends the `DeleteItem` request and returns the deleted item deserialized as `T`.
    ///
    /// Returns `Ok(None)` if no item existed at the key.
    ///
    /// This method is also available implicitly via `.await`.
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the DynamoDB request fails, if a condition check
    /// fails, or if deserialization of the returned attributes fails.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::{DynamoDBItemOp, KeyId};
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// let old /* : Option<Enrollment> */ = Enrollment::delete_by_id(
    ///     client,
    ///     KeyId::pk("user-1").sk("course-42"),
    /// )
    /// .execute()
    /// .await?;
    /// # Ok(())
    /// # }
    /// ```
    #[tracing::instrument(level = "debug", skip(self), name = "delete_execute_old")]
    pub fn execute(self) -> impl Future<Output = Result<Option<T>>> + Send + 'static {
        let builder = self.builder;
        async move {
            builder
                .return_values(SDKReturnValue::AllOld)
                .send()
                .await?
                .attributes
                .map(Item::from_dynamodb_response)
                .map(T::try_from_item)
                .transpose()
        }
    }
}

impl<TD: TableDefinition, T: DynamoDBItem<TD> + DeserializeOwned, C: ConditionState> IntoFuture
    for DeleteItemRequest<TD, T, Typed, Return<Old>, C>
{
    type Output = Result<Option<T>>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(self.execute())
    }
}

// -- Terminal: ReturnItem<Old> + Raw (any C) --------------------------------

impl<TD: TableDefinition, T, C: ConditionState> DeleteItemRequest<TD, T, Raw, Return<Old>, C> {
    /// Sends the `DeleteItem` request and returns the deleted raw item map.
    ///
    /// Returns `Ok(None)` if no item existed at the key.
    ///
    /// This method is also available implicitly via `.await`.
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the DynamoDB request fails or if a condition check
    /// fails.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use dynamodb_facade::test_fixtures::*;
    /// use dynamodb_facade::DynamoDBItemOp;
    ///
    /// # async fn example(client: aws_sdk_dynamodb::Client) -> dynamodb_facade::Result<()> {
    /// let old_raw = sample_enrollment()
    ///     .delete(client)
    ///     .return_old()
    ///     .raw()
    ///     .execute()
    ///     .await?;
    /// // old_raw: Option<Item<PlatformTable>>
    /// # Ok(())
    /// # }
    /// ```
    #[tracing::instrument(level = "debug", skip(self), name = "delete_execute_old_raw")]
    pub fn execute(self) -> impl Future<Output = Result<Option<Item<TD>>>> + Send + 'static {
        let builder = self.builder;
        async move {
            Ok(builder
                .return_values(SDKReturnValue::AllOld)
                .send()
                .await
                .map(|out| out.attributes.map(Item::from_dynamodb_response))?)
        }
    }
}

impl<TD: TableDefinition, T, C: ConditionState> IntoFuture
    for DeleteItemRequest<TD, T, Raw, Return<Old>, C>
{
    type Output = Result<Option<Item<TD>>>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(self.execute())
    }
}