dynoxide-rs 0.11.1

A lightweight, embeddable DynamoDB emulator backed by SQLite
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Native [`StorageBackend`] implementation backed by the rusqlite-typed
//! [`Storage`].
//!
//! Each impl method is a thin async wrapper over the existing sync method on
//! `Storage`: the body invokes the sync method synchronously and returns a
//! ready future. No `spawn_blocking` is used, so today's behaviour of running
//! rusqlite calls on the active executor thread is preserved exactly.
//!
//! # Load-bearing invariant: native futures never suspend
//!
//! Every method here resolves on the first poll (it runs synchronous work and
//! returns `Ready`). The synchronous `Database` facade depends on this: it
//! drives these futures with `block_on`, which is only safe to call from inside
//! the tokio-based HTTP and MCP servers because an always-ready future never
//! parks the worker thread. Do not introduce a real `.await` on external async
//! I/O (a socket, a timer, a task spawn, a channel) into any method here
//! without first moving the facade off `block_on`; otherwise the server worker
//! threads will stall.

use crate::errors::DynoxideError;
use crate::storage::{
    CreateTableMetadata, DatabaseInfo, QueryParams, ScanParams, Storage, StreamRecord,
    TableMetadata, TableStats,
};
use crate::storage_backend::{BackendError, BaseItemRow, Clock, GsiItemRow, StorageBackend, error};
use crate::types::Tag;

/// Convert a [`DynoxideError`] into a [`BackendError`].
///
/// Storage's sync surface returns `Result<T, DynoxideError>`; the trait surface
/// returns `Result<T, BackendError>`. The conversion preserves rusqlite error
/// codes via [`error::from_rusqlite`]. A client-facing `ValidationException`
/// (a backend method such as `set_tags` enforces the tag-count limit) is
/// preserved as [`BackendError::Validation`] so the reverse conversion can
/// restore its 400 envelope; any other variant falls through to
/// [`BackendError::Other`] carrying the original `Display` output.
fn dyno_to_backend(err: DynoxideError) -> BackendError {
    match err {
        DynoxideError::SqliteError(e) => error::from_rusqlite(e),
        DynoxideError::ValidationException(msg) => BackendError::Validation(msg),
        other => BackendError::Other(other.to_string()),
    }
}

impl StorageBackend for Storage {
    fn clock(&self) -> &dyn Clock {
        Storage::clock(self)
    }

    async fn insert_table_metadata(&self, m: &CreateTableMetadata<'_>) -> Result<(), BackendError> {
        Storage::insert_table_metadata(self, m).map_err(dyno_to_backend)
    }

    async fn get_table_metadata(
        &self,
        table_name: &str,
    ) -> Result<Option<TableMetadata>, BackendError> {
        Storage::get_table_metadata(self, table_name).map_err(dyno_to_backend)
    }

    async fn delete_table_metadata(&self, table_name: &str) -> Result<bool, BackendError> {
        Storage::delete_table_metadata(self, table_name).map_err(dyno_to_backend)
    }

    async fn update_table_metadata(
        &self,
        table_name: &str,
        attribute_definitions: &str,
        gsi_definitions: Option<&str>,
    ) -> Result<(), BackendError> {
        Storage::update_table_metadata(self, table_name, attribute_definitions, gsi_definitions)
            .map_err(dyno_to_backend)
    }

    async fn update_provisioned_throughput(
        &self,
        table_name: &str,
        provisioned_throughput: &str,
    ) -> Result<(), BackendError> {
        Storage::update_provisioned_throughput(self, table_name, provisioned_throughput)
            .map_err(dyno_to_backend)
    }

    async fn clear_provisioned_throughput(&self, table_name: &str) -> Result<(), BackendError> {
        Storage::clear_provisioned_throughput(self, table_name).map_err(dyno_to_backend)
    }

    async fn update_billing_mode(
        &self,
        table_name: &str,
        billing_mode: &str,
    ) -> Result<(), BackendError> {
        Storage::update_billing_mode(self, table_name, billing_mode).map_err(dyno_to_backend)
    }

    async fn update_table_class(
        &self,
        table_name: &str,
        table_class: &str,
    ) -> Result<(), BackendError> {
        Storage::update_table_class(self, table_name, table_class).map_err(dyno_to_backend)
    }

    async fn update_on_demand_throughput(
        &self,
        table_name: &str,
        on_demand_throughput: &str,
    ) -> Result<(), BackendError> {
        Storage::update_on_demand_throughput(self, table_name, on_demand_throughput)
            .map_err(dyno_to_backend)
    }

    async fn get_tags(&self, table_name: &str) -> Result<Vec<Tag>, BackendError> {
        Storage::get_tags(self, table_name).map_err(dyno_to_backend)
    }

    async fn set_tags(&self, table_name: &str, new_tags: &[Tag]) -> Result<(), BackendError> {
        Storage::set_tags(self, table_name, new_tags).map_err(dyno_to_backend)
    }

    async fn update_deletion_protection(
        &self,
        table_name: &str,
        enabled: bool,
    ) -> Result<(), BackendError> {
        Storage::update_deletion_protection(self, table_name, enabled).map_err(dyno_to_backend)
    }

    async fn remove_tags(&self, table_name: &str, keys: &[String]) -> Result<(), BackendError> {
        Storage::remove_tags(self, table_name, keys).map_err(dyno_to_backend)
    }

    async fn list_table_names(&self) -> Result<Vec<String>, BackendError> {
        Storage::list_table_names(self).map_err(dyno_to_backend)
    }

    async fn table_exists(&self, table_name: &str) -> Result<bool, BackendError> {
        Storage::table_exists(self, table_name).map_err(dyno_to_backend)
    }

    async fn create_data_table(&self, table_name: &str) -> Result<(), BackendError> {
        Storage::create_data_table(self, table_name).map_err(dyno_to_backend)
    }

    async fn drop_data_table(&self, table_name: &str) -> Result<(), BackendError> {
        Storage::drop_data_table(self, table_name).map_err(dyno_to_backend)
    }

    async fn create_gsi_table(
        &self,
        table_name: &str,
        index_name: &str,
    ) -> Result<(), BackendError> {
        Storage::create_gsi_table(self, table_name, index_name).map_err(dyno_to_backend)
    }

    async fn drop_gsi_table(&self, table_name: &str, index_name: &str) -> Result<(), BackendError> {
        Storage::drop_gsi_table(self, table_name, index_name).map_err(dyno_to_backend)
    }

    async fn create_lsi_table(
        &self,
        table_name: &str,
        index_name: &str,
    ) -> Result<(), BackendError> {
        Storage::create_lsi_table(self, table_name, index_name).map_err(dyno_to_backend)
    }

    async fn drop_lsi_table(&self, table_name: &str, index_name: &str) -> Result<(), BackendError> {
        Storage::drop_lsi_table(self, table_name, index_name).map_err(dyno_to_backend)
    }

    async fn insert_gsi_item(
        &self,
        table_name: &str,
        index_name: &str,
        gsi_pk: &str,
        gsi_sk: &str,
        table_pk: &str,
        table_sk: &str,
        item_json: &str,
    ) -> Result<(), BackendError> {
        Storage::insert_gsi_item(
            self, table_name, index_name, gsi_pk, gsi_sk, table_pk, table_sk, item_json,
        )
        .map_err(dyno_to_backend)
    }

    async fn insert_gsi_items(
        &self,
        table_name: &str,
        index_name: &str,
        rows: &[GsiItemRow],
    ) -> Result<(), BackendError> {
        Storage::insert_gsi_items(self, table_name, index_name, rows).map_err(dyno_to_backend)
    }

    async fn delete_gsi_item(
        &self,
        table_name: &str,
        index_name: &str,
        table_pk: &str,
        table_sk: &str,
    ) -> Result<(), BackendError> {
        Storage::delete_gsi_item(self, table_name, index_name, table_pk, table_sk)
            .map_err(dyno_to_backend)
    }

    async fn query_gsi_items(
        &self,
        table_name: &str,
        index_name: &str,
        gsi_pk: &str,
        params: &QueryParams<'_>,
    ) -> Result<Vec<(String, String, String)>, BackendError> {
        Storage::query_gsi_items(self, table_name, index_name, gsi_pk, params)
            .map_err(dyno_to_backend)
    }

    async fn scan_gsi_items(
        &self,
        table_name: &str,
        index_name: &str,
        params: &ScanParams<'_>,
    ) -> Result<Vec<(String, String, String)>, BackendError> {
        Storage::scan_gsi_items(self, table_name, index_name, params).map_err(dyno_to_backend)
    }

    async fn insert_lsi_item(
        &self,
        table_name: &str,
        index_name: &str,
        pk: &str,
        sk: &str,
        base_pk: &str,
        base_sk: &str,
        item_json: &str,
    ) -> Result<(), BackendError> {
        Storage::insert_lsi_item(
            self, table_name, index_name, pk, sk, base_pk, base_sk, item_json,
        )
        .map_err(dyno_to_backend)
    }

    async fn delete_lsi_item(
        &self,
        table_name: &str,
        index_name: &str,
        base_pk: &str,
        base_sk: &str,
    ) -> Result<(), BackendError> {
        Storage::delete_lsi_item(self, table_name, index_name, base_pk, base_sk)
            .map_err(dyno_to_backend)
    }

    async fn query_lsi_items(
        &self,
        table_name: &str,
        index_name: &str,
        pk: &str,
        params: &QueryParams<'_>,
    ) -> Result<Vec<(String, String, String)>, BackendError> {
        Storage::query_lsi_items(self, table_name, index_name, pk, params).map_err(dyno_to_backend)
    }

    async fn scan_lsi_items(
        &self,
        table_name: &str,
        index_name: &str,
        params: &ScanParams<'_>,
    ) -> Result<Vec<(String, String, String)>, BackendError> {
        Storage::scan_lsi_items(self, table_name, index_name, params).map_err(dyno_to_backend)
    }

    async fn begin_transaction(&self) -> Result<(), BackendError> {
        Storage::begin_transaction(self).map_err(dyno_to_backend)
    }

    async fn commit(&self) -> Result<(), BackendError> {
        Storage::commit(self).map_err(dyno_to_backend)
    }

    async fn rollback(&self) -> Result<(), BackendError> {
        Storage::rollback(self).map_err(dyno_to_backend)
    }

    async fn enable_bulk_loading(&self) -> Result<(), BackendError> {
        Storage::enable_bulk_loading(self).map_err(dyno_to_backend)
    }

    async fn disable_bulk_loading(&self) -> Result<(), BackendError> {
        Storage::disable_bulk_loading(self).map_err(dyno_to_backend)
    }

    async fn put_item(
        &self,
        table_name: &str,
        pk: &str,
        sk: &str,
        item_json: &str,
        item_size: usize,
    ) -> Result<Option<String>, BackendError> {
        Storage::put_item(self, table_name, pk, sk, item_json, item_size).map_err(dyno_to_backend)
    }

    async fn put_item_with_hash(
        &self,
        table_name: &str,
        pk: &str,
        sk: &str,
        item_json: &str,
        item_size: usize,
        hash_prefix: &str,
    ) -> Result<Option<String>, BackendError> {
        Storage::put_item_with_hash(self, table_name, pk, sk, item_json, item_size, hash_prefix)
            .map_err(dyno_to_backend)
    }

    async fn put_base_items(
        &self,
        table_name: &str,
        rows: &[BaseItemRow],
    ) -> Result<(), BackendError> {
        Storage::put_base_items(self, table_name, rows).map_err(dyno_to_backend)
    }

    async fn get_item(
        &self,
        table_name: &str,
        pk: &str,
        sk: &str,
    ) -> Result<Option<String>, BackendError> {
        Storage::get_item(self, table_name, pk, sk).map_err(dyno_to_backend)
    }

    async fn get_partition_size(&self, table_name: &str, pk: &str) -> Result<i64, BackendError> {
        Storage::get_partition_size(self, table_name, pk).map_err(dyno_to_backend)
    }

    async fn get_lsi_partition_size(
        &self,
        table_name: &str,
        index_name: &str,
        pk: &str,
    ) -> Result<i64, BackendError> {
        Storage::get_lsi_partition_size(self, table_name, index_name, pk).map_err(dyno_to_backend)
    }

    async fn delete_item(
        &self,
        table_name: &str,
        pk: &str,
        sk: &str,
    ) -> Result<Option<String>, BackendError> {
        Storage::delete_item(self, table_name, pk, sk).map_err(dyno_to_backend)
    }

    async fn query_items(
        &self,
        table_name: &str,
        pk: &str,
        params: &QueryParams<'_>,
    ) -> Result<Vec<(String, String, String)>, BackendError> {
        Storage::query_items(self, table_name, pk, params).map_err(dyno_to_backend)
    }

    async fn scan_items(
        &self,
        table_name: &str,
        params: &ScanParams<'_>,
    ) -> Result<Vec<(String, String, String)>, BackendError> {
        Storage::scan_items(self, table_name, params).map_err(dyno_to_backend)
    }

    async fn count_items(&self, table_name: &str) -> Result<i64, BackendError> {
        Storage::count_items(self, table_name).map_err(dyno_to_backend)
    }

    async fn db_size_bytes(&self) -> Result<u64, BackendError> {
        Storage::db_size_bytes(self).map_err(dyno_to_backend)
    }

    async fn table_count(&self) -> Result<usize, BackendError> {
        Storage::table_count(self).map_err(dyno_to_backend)
    }

    async fn table_stats(&self) -> Result<Vec<TableStats>, BackendError> {
        Storage::table_stats(self).map_err(dyno_to_backend)
    }

    async fn database_info(&self) -> Result<DatabaseInfo, BackendError> {
        Storage::database_info(self).map_err(dyno_to_backend)
    }

    async fn vacuum(&self) -> Result<(), BackendError> {
        Storage::vacuum(self).map_err(dyno_to_backend)
    }

    async fn enable_stream(
        &self,
        table_name: &str,
        view_type: &str,
        label: &str,
    ) -> Result<(), BackendError> {
        Storage::enable_stream(self, table_name, view_type, label).map_err(dyno_to_backend)
    }

    async fn disable_stream(&self, table_name: &str) -> Result<(), BackendError> {
        Storage::disable_stream(self, table_name).map_err(dyno_to_backend)
    }

    async fn insert_stream_record(
        &self,
        table_name: &str,
        event_name: &str,
        keys_json: &str,
        new_image: Option<&str>,
        old_image: Option<&str>,
        sequence_number: &str,
        shard_id: &str,
        created_at: i64,
    ) -> Result<(), BackendError> {
        Storage::insert_stream_record(
            self,
            table_name,
            event_name,
            keys_json,
            new_image,
            old_image,
            sequence_number,
            shard_id,
            created_at,
        )
        .map_err(dyno_to_backend)
    }

    async fn insert_stream_record_with_identity(
        &self,
        table_name: &str,
        event_name: &str,
        keys_json: &str,
        new_image: Option<&str>,
        old_image: Option<&str>,
        sequence_number: &str,
        shard_id: &str,
        created_at: i64,
        user_identity: Option<&str>,
    ) -> Result<(), BackendError> {
        Storage::insert_stream_record_with_identity(
            self,
            table_name,
            event_name,
            keys_json,
            new_image,
            old_image,
            sequence_number,
            shard_id,
            created_at,
            user_identity,
        )
        .map_err(dyno_to_backend)
    }

    async fn next_stream_sequence_number(&self, table_name: &str) -> Result<i64, BackendError> {
        Storage::next_stream_sequence_number(self, table_name).map_err(dyno_to_backend)
    }

    async fn get_stream_records(
        &self,
        table_name: &str,
        shard_id: &str,
        after_sequence: i64,
        limit: usize,
    ) -> Result<Vec<StreamRecord>, BackendError> {
        Storage::get_stream_records(self, table_name, shard_id, after_sequence, limit)
            .map_err(dyno_to_backend)
    }

    async fn list_stream_enabled_tables(&self) -> Result<Vec<TableMetadata>, BackendError> {
        Storage::list_stream_enabled_tables(self).map_err(dyno_to_backend)
    }

    async fn update_ttl_config(
        &self,
        table_name: &str,
        attribute_name: Option<&str>,
        enabled: bool,
    ) -> Result<(), BackendError> {
        Storage::update_ttl_config(self, table_name, attribute_name, enabled)
            .map_err(dyno_to_backend)
    }

    async fn list_ttl_enabled_tables(&self) -> Result<Vec<TableMetadata>, BackendError> {
        Storage::list_ttl_enabled_tables(self).map_err(dyno_to_backend)
    }

    async fn get_shard_sequence_range(
        &self,
        table_name: &str,
        shard_id: &str,
    ) -> Result<(Option<String>, Option<String>), BackendError> {
        Storage::get_shard_sequence_range(self, table_name, shard_id).map_err(dyno_to_backend)
    }

    async fn touch_cached_at(
        &self,
        table_name: &str,
        pk: &str,
        sk: &str,
        timestamp: f64,
    ) -> Result<(), BackendError> {
        Storage::touch_cached_at(self, table_name, pk, sk, timestamp).map_err(dyno_to_backend)
    }

    async fn get_lru_items(
        &self,
        table_name: &str,
        limit: usize,
    ) -> Result<Vec<(String, String, i64)>, BackendError> {
        Storage::get_lru_items(self, table_name, limit).map_err(dyno_to_backend)
    }
}