easy_dynamodb/
lib.rs

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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
pub mod error;

use aws_config::retry::RetryConfig;
use aws_config::timeout::TimeoutConfig;
use aws_sdk_dynamodb::types::AttributeValue;
use aws_sdk_dynamodb::Error;
use error::DynamoException;
use slog::{debug, o, Logger};

use std::collections::HashMap;
use std::fmt::Debug;
use std::time::Duration;

pub struct Client {
    client: aws_sdk_dynamodb::Client,
    table_name: String,
    log: Logger,
    key_field: String,
    #[allow(dead_code)]
    sort_key_field: Option<String>,
}

impl Client {
    pub fn new(
        logger: Logger,
        access_key_id: String,
        secret_access_key: String,
        region: String,
        table_name: String,
        key_field: String,

        // TODO: not implement sort key yet
        sort_key_field: Option<String>,
        endpoint: Option<String>,
    ) -> Self {
        use aws_config::Region;
        use aws_sdk_dynamodb::Config;

        let timeout_config = TimeoutConfig::builder()
            .operation_attempt_timeout(Duration::from_secs(5))
            .build();

        let retry_config = RetryConfig::standard().with_max_attempts(3);
        let config = Config::builder()
            .credentials_provider(aws_credential_types::Credentials::from_keys(
                access_key_id,
                secret_access_key,
                None,
            ))
            .region(Region::new(region))
            .set_timeout_config(Some(timeout_config))
            .set_retry_config(Some(retry_config))
            .clone();

        let config = match endpoint {
            Some(endpoint_url) => config.endpoint_url(endpoint_url),
            None => config,
        };

        let config = config.build();

        Client {
            client: aws_sdk_dynamodb::Client::from_conf(config),
            table_name,
            key_field,
            sort_key_field,
            log: logger.new(o!("crate" => "easy-dynamodb", "struct" => "Client")),
        }
    }

    pub fn get_client(&self) -> aws_sdk_dynamodb::Client {
        self.client.clone()
    }

    fn get_log(&self, method: &'static str) -> Logger {
        self.log.new(o!("method" => method))
    }

    pub async fn create<T>(&self, doc: T) -> Result<(), DynamoException>
    where
        T: Debug + serde::Serialize,
    {
        let log = self.get_log("create");
        debug!(log, "{:?}", doc);
        let value = match serde_json::to_value(doc) {
            Ok(value) => value,
            Err(e) => return Err(DynamoException::DynamoSerializeException(format!("{e:?}"))),
        };
        let item = match serde_dynamo::to_item(value) {
            Ok(item) => item,
            Err(e) => return Err(DynamoException::DynamoSerializeException(format!("{e:?}"))),
        };
        let condition = format!("attribute_not_exists({})", self.key_field);

        match self
            .client
            .put_item()
            .table_name(&self.table_name)
            .set_item(Some(item))
            .condition_expression(&condition)
            .send()
            .await
        {
            Ok(_) => Ok(()),
            Err(e) => Err(DynamoException::DynamoPutItemException(format!("{e:?}"))),
        }
    }

    pub async fn update<T>(&self, key: &str, fields: Vec<(&str, T)>) -> Result<(), DynamoException>
    where
        T: Debug + serde::Serialize,
    {
        let log = self.get_log("update");
        debug!(log, "{:?} {:?}", key, fields);

        let (names, values, condition) = self.to_attribute_names_and_values(fields)?;

        let update_expression = format!("SET {}", &condition.join(", "));

        let key_field = self.key_field.clone();
        let key_value = AttributeValue::S(key.to_string());

        let condition_expression = format!("attribute_exists({})", key_field);

        debug!(log, "update_expression({:?}), key_field({:?}), key_value({:?}), condition_expression({:?}) names({names:?}), values({values:?})", update_expression, key_field, key_value, condition_expression);

        match self
            .client
            .update_item()
            .table_name(&self.table_name)
            .key(key_field, key_value)
            .update_expression(&update_expression)
            .set_expression_attribute_names(Some(names))
            .set_expression_attribute_values(Some(values))
            .condition_expression(condition_expression)
            .send()
            .await
        {
            Ok(e) => {
                debug!(log, "succeed {:?}", e);
                Ok(())
            }
            Err(e) => Err(DynamoException::DynamoUpdateItemException(format!("{e:?}"))),
        }
    }

    pub async fn delete(&self, key: &str) -> Result<(), DynamoException> {
        let log = self.get_log("delete");
        debug!(log, "{:?}", key);

        match self
            .client
            .delete_item()
            .table_name(&self.table_name)
            .key(self.key_field.clone(), AttributeValue::S(key.to_string()))
            .send()
            .await
        {
            Ok(_) => Ok(()),
            Err(e) => Err(DynamoException::DynamoDeleteItemException(format!("{e:?}"))),
        }
    }

    pub async fn get<T>(&self, key: &str) -> Result<Option<T>, DynamoException>
    where
        T: Debug + serde::de::DeserializeOwned,
    {
        let log = self.get_log("get");
        debug!(log, "{:?}", key);
        let resp = match self
            .client
            .get_item()
            .table_name(&self.table_name)
            .key(self.key_field.clone(), AttributeValue::S(key.to_string()))
            .send()
            .await
        {
            Ok(resp) => resp,
            Err(e) => return Err(DynamoException::DynamoGetItemException(format!("{e:?}"))),
        };

        Ok(match resp.item {
            Some(item) => {
                debug!(log, "item: {:?}", item);
                let value: T = match serde_dynamo::from_item(item) {
                    Ok(value) => value,
                    Err(e) => {
                        return Err(DynamoException::DynamoSerializeException(format!("{e:?}")))
                    }
                };
                Some(value)
            }
            None => None,
        })
    }

    fn to_attribute_names_and_values<F>(
        &self,
        filter: Vec<(&str, F)>,
    ) -> Result<
        (
            HashMap<String, String>,
            HashMap<String, AttributeValue>,
            Vec<String>,
        ),
        DynamoException,
    >
    where
        F: Debug + serde::Serialize,
    {
        let mut names = HashMap::new();
        let mut values = HashMap::new();
        let mut condition = vec![];

        for (name, value) in filter.iter() {
            names.insert(format!("#{name}"), name.to_string().clone());
            let value = match serde_dynamo::to_attribute_value(value) {
                Ok(value) => value,
                Err(e) => return Err(DynamoException::DynamoSerializeException(format!("{e:?}"))),
            };
            values.insert(format!(":{name}"), value);

            condition.push(format!("#{name} = :{name}"));
        }

        Ok((names, values, condition))
    }

    pub async fn find<T, F>(
        &self,
        index: &str,
        bookmark: Option<String>,
        size: Option<i32>,
        filter: Vec<(&str, F)>,
    ) -> Result<(Option<Vec<T>>, Option<String>), DynamoException>
    where
        T: Debug + serde::de::DeserializeOwned,
        F: Debug + serde::Serialize,
    {
        let log = self.get_log("find");
        debug!(
            log,
            "index: {:?} bookmark: {:?} size: {:?} filter: {:?}", index, bookmark, size, filter
        );

        let (names, values, condition) = self.to_attribute_names_and_values(filter)?;
        let size = size.unwrap_or(10);
        let bookmark = self.decode_bookmark(bookmark);
        let key_condition = &condition.join(" AND ");

        debug!(
            log,
            "key_condition: {:?} names: {:?} values: {:?} size: {:?}",
            key_condition,
            names,
            values,
            size
        );

        let resp = match self
            .client
            .query()
            .table_name(&self.table_name)
            .set_exclusive_start_key(bookmark)
            .index_name(index)
            .key_condition_expression(key_condition)
            .set_expression_attribute_names(Some(names))
            .set_expression_attribute_values(Some(values))
            .limit(size)
            .send()
            .await
        {
            Ok(resp) => resp,
            Err(e) => {
                return Err(DynamoException::DynamoQueryException(format!("{e:?}")));
            }
        };

        crate::debug!(log, "response {:?}", resp);

        let docs = match resp.items {
            Some(items) => match serde_dynamo::from_items(items) {
                Ok(value) => Some(value),
                Err(e) => return Err(DynamoException::DynamoSerializeException(format!("{e:?}"))),
            },
            None => None,
        };

        crate::debug!(log, "docs: {:?}", docs);

        let bookmark = self.encode_bookmark(resp.last_evaluated_key);
        Ok((docs, bookmark))
    }

    fn encode_bookmark(&self, bookmark: Option<HashMap<String, AttributeValue>>) -> Option<String> {
        if bookmark.is_none() {
            return None;
        }

        let bookmark = bookmark.unwrap();
        let bookmark = BookmarkModel::new(bookmark);
        Some(bookmark.to_string())
    }

    fn decode_bookmark(&self, bookmark: Option<String>) -> Option<HashMap<String, AttributeValue>> {
        if bookmark.is_none() {
            return None;
        }

        let bookmark = BookmarkModel::from_string(&bookmark.unwrap());
        let mut result = HashMap::new();

        for (key, value) in bookmark.keys.iter().zip(bookmark.values.iter()) {
            result.insert(key.clone(), value.clone().into());
        }

        Some(result)
    }
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct BookmarkModel {
    keys: Vec<String>,
    values: Vec<serde_dynamo::AttributeValue>,
}

impl BookmarkModel {
    fn new(bookmark: HashMap<String, AttributeValue>) -> Self {
        let mut keys = vec![];
        let mut values = vec![];

        for (key, value) in bookmark {
            keys.push(key.clone());
            values.push(value.into());
        }
        BookmarkModel { keys, values }
    }

    fn to_string(&self) -> String {
        serde_json::to_string(self).unwrap()
    }

    fn from_string(s: &str) -> Self {
        serde_json::from_str(s).unwrap()
    }
}

impl Client {
    pub async fn table_exists(&self) -> Result<bool, Error> {
        let request = self.client.describe_table().table_name(&self.table_name);

        let resp = request.send().await;
        match resp {
            Ok(_) => Ok(true),
            Err(_) => Ok(false),
        }
    }

    pub async fn list_tables(&self) -> Result<Vec<String>, Error> {
        let paginator = self.client.list_tables().into_paginator().items().send();
        let table_names = paginator.collect::<Result<Vec<_>, _>>().await?;

        Ok(table_names)
    }

    pub async fn get_total_items(&self) -> Result<u64, Error> {
        use aws_sdk_dynamodb::types::Select;

        let request = self
            .client
            .scan()
            .table_name(&self.table_name)
            .select(Select::Count);

        let resp = request.send().await?;
        let count = resp.count as u64;
        Ok(count)
    }

    pub async fn scan_table_items(&self) -> Result<Vec<HashMap<String, AttributeValue>>, Error> {
        let request = self.client.scan().table_name(&self.table_name);

        let resp = request.send().await?;
        let result = resp.items.unwrap_or_else(|| vec![]);

        Ok(result)
    }
}

#[cfg(test)]
mod dyanomdb_tests {
    use std::thread;

    use super::*;

    #[derive(Debug, serde::Serialize, serde::Deserialize)]
    struct TestModel {
        key: String,
        id: String,
        created_at: i64,
    }

    #[derive(Debug, serde::Serialize, serde::Deserialize)]
    struct TestModelV2 {
        key: String,
        id: String,
        created_at: i64,
        str_field: String,
        bool_field: bool,
    }

    #[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
    struct IndexModel {
        key: String,
        id: String,
        created_at: i64,
        r#type: String,
    }

    #[derive(Debug, serde::Serialize, serde::Deserialize)]
    enum TestField {
        #[serde(untagged)]
        N(i64),
        #[serde(untagged)]
        S(String),
        #[serde(untagged)]
        B(bool),
    }

    fn new_cli() -> Client {
        Client::new(
            slog::Logger::root(slog::Discard, o!("goal" => "testing")),
            option_env!("AWS_ACCESS_KEY_ID")
                .expect("AWS_ACCESS_KEY_ID is required")
                .to_string(),
            option_env!("AWS_SECRET_ACCESS_KEY")
                .expect("AWS_SECRET_ACCESS_KEY is required")
                .to_string(),
            option_env!("AWS_REGION")
                .unwrap_or("ap-northeast-2")
                .to_string(),
            option_env!("AWS_DYNAMODB_TABLE")
                .expect("AWS_DYNAMODB_TABLE is required")
                .to_string(),
            option_env!("AWS_DYNAMODB_KEY").unwrap_or("key").to_string(),
            None,
            None,
        )
    }

    #[tokio::test]
    async fn test_create() {
        let client = new_cli();
        let ts = chrono::Utc::now().timestamp_nanos_opt();
        assert!(ts.is_some(), "timestamp is none");
        let ts = ts.unwrap();

        let result = client
            .create(TestModel {
                key: format!("test_create_key-{ts}"),
                id: format!("test_create_id-{ts}"),
                created_at: ts,
            })
            .await;

        assert!(result.is_ok(), "test_create failed: {result:?}");
    }

    #[tokio::test]
    async fn test_create_duplicated_error() {
        let client = new_cli();
        let ts = chrono::Utc::now().timestamp_nanos_opt();
        assert!(ts.is_some(), "timestamp is none");
        let ts = ts.unwrap();

        let result = client
            .create(TestModel {
                key: format!("test_create_duplicated_error_key-{ts}"),
                id: format!("test_create_duplicated_error_id-{ts}"),
                created_at: ts,
            })
            .await;

        assert!(result.is_ok(), "{result:?}");

        let result = client
            .create(TestModel {
                key: format!("test_create_duplicated_error_key-{ts}"),
                id: format!("test_create_duplicated_error_id-{ts}"),
                created_at: 0,
            })
            .await;

        assert!(
            matches!(result, Err(DynamoException::DynamoPutItemException(_))),
            "{result:?}"
        );
    }

    #[tokio::test]
    async fn test_get() {
        let client = new_cli();
        let ts = chrono::Utc::now().timestamp_nanos_opt();
        assert!(ts.is_some(), "timestamp is none");
        let ts = ts.unwrap();

        let result = client
            .create(TestModel {
                key: format!("test_get_key-{ts}"),
                id: format!("test_get_id-{ts}"),
                created_at: ts,
            })
            .await;

        assert!(result.is_ok(), "{result:?}");
        let key = format!(
            "test_get_{}-{ts}",
            option_env!("AWS_DYNAMODB_KEY").unwrap_or("key")
        );

        let doc = client.get(&key).await;
        assert!(matches!(doc, Ok(Some(_))), "{doc:?}");
        let doc: TestModel = doc.unwrap().unwrap();

        assert!(doc.created_at == ts, "{doc:?}");
        assert!(doc.id == format!("test_get_id-{ts}"), "{doc:?}");
        assert!(doc.key == format!("test_get_key-{ts}"), "{doc:?}");
    }

    #[tokio::test]
    async fn test_update() {
        let client = new_cli();
        let ts = chrono::Utc::now().timestamp_nanos_opt();
        assert!(ts.is_some(), "timestamp is none");
        let ts = ts.unwrap();

        let result = client
            .create(TestModel {
                key: format!("test_update_key-{ts}"),
                id: format!("test_update_id-{ts}"),
                created_at: ts,
            })
            .await;

        assert!(result.is_ok(), "{result:?}");

        let key = format!(
            "test_update_{}-{ts}",
            option_env!("AWS_DYNAMODB_KEY").unwrap_or("key")
        );
        let result = client
            .update(
                &key,
                vec![
                    ("created_at", TestField::N(0)),
                    ("str_field", TestField::S("updated".to_string())),
                    ("bool_field", TestField::B(true)),
                ],
            )
            .await;

        assert!(result.is_ok(), "{result:?}");

        let doc_v1 = client.get::<TestModel>(&key).await;
        assert!(matches!(doc_v1, Ok(Some(_))), "{doc_v1:?}");
        let doc_v1 = doc_v1.unwrap().unwrap();

        assert!(doc_v1.created_at == 0, "{doc_v1:?}");
        assert!(doc_v1.id == format!("test_update_id-{ts}"), "{doc_v1:?}");
        assert!(doc_v1.key == format!("test_update_key-{ts}"), "{doc_v1:?}");

        let doc_v2 = client.get::<TestModelV2>(&key).await;
        assert!(matches!(doc_v2, Ok(Some(_))), "{doc_v2:?}");
        let doc_v2 = doc_v2.unwrap().unwrap();

        assert!(doc_v2.bool_field, "{doc_v2:?}");
        assert!(doc_v2.str_field == "updated".to_string(), "{doc_v2:?}");
        assert!(doc_v2.created_at == 0, "{doc_v2:?}");
        assert!(doc_v2.id == format!("test_update_id-{ts}"), "{doc_v2:?}");
        assert!(doc_v2.key == format!("test_update_key-{ts}"), "{doc_v2:?}");
    }

    #[tokio::test]
    async fn test_find() {
        let client = new_cli();

        // NOTE: below code is commented out because it's not necessary for the test
        //       It is just for insertion at the first time.
        // let key_prefix = "test_find";
        // let ts = chrono::Utc::now().timestamp_nanos_opt();
        // let ts = ts.unwrap();
        // assert!(ts.is_some(), "timestamp is none");
        // for i in 0..10 {
        //     let result = client
        //         .create(
        //             IndexModel {
        //                 key: format!("{key_prefix}_key-{ts}_{i}"),
        //                 id: format!("{key_prefix}_id-{ts}_{i}"),
        //                 created_at: ts,
        //                 r#type: "type1".to_string(),
        //             },
        //         )
        //         .await;

        //     assert!(result.is_ok(), "{result:?}");
        // }

        // for i in 0..10 {
        //     let result = client
        //         .create(
        //             IndexModel {
        //                 key: format!("{key_prefix}_key-{ts}_{i}_2"),
        //                 id: format!("{key_prefix}_id-{ts}_{i}_2"),
        //                 created_at: ts,
        //                 r#type: "type2".to_string(),
        //             },
        //         )
        //         .await;

        //     assert!(result.is_ok(), "{result:?}");
        // }

        let result = client
            .find("type-index", None, Some(6), vec![("type", "type1")])
            .await;

        assert!(matches!(result, Ok((Some(_), Some(_)))), "{result:?}");
        let (docs, bookmark) = result.unwrap();
        let (docs, bookmark): (Vec<IndexModel>, String) = (docs.unwrap(), bookmark.unwrap());

        assert!(docs.len() == 6, "{docs:?}");
        assert!(bookmark.len() > 0, "{bookmark:?}");

        let result = client
            .find(
                "type-index",
                Some(bookmark),
                Some(6),
                vec![("type", "type1")],
            )
            .await;

        assert!(matches!(result, Ok((Some(_), None))), "{result:?}");
        let (docs, _) = result.unwrap();
        let docs: Vec<IndexModel> = docs.unwrap();

        assert!(docs.len() == 4, "{docs:?}");
    }

    #[tokio::test]
    async fn test_delete() {
        let client = new_cli();
        let ts = chrono::Utc::now().timestamp_nanos_opt();
        assert!(ts.is_some(), "timestamp is none");
        let ts = ts.unwrap();

        let result = client
            .create(TestModel {
                key: format!("test_delete_key-{ts}"),
                id: format!("test_delete_id-{ts}"),
                created_at: ts,
            })
            .await;

        thread::sleep(std::time::Duration::from_millis(100));
        assert!(result.is_ok(), "{result:?}");

        let key = format!(
            "test_delete_{}-{ts}",
            option_env!("AWS_DYNAMODB_KEY").unwrap_or("key")
        );
        let result = client.delete(&key).await;

        assert!(result.is_ok(), "{result:?}");

        thread::sleep(std::time::Duration::from_millis(100));
        let doc = client.get::<TestModel>(&key).await;
        assert!(matches!(doc, Ok(None)), "{doc:?}");
    }
}