deez/macros/
create.rs

1/// Convenience macro for put with ConditionExpression parameter to ensure
2/// records are only “created” and not overwritten when inserting new records
3/// into the table.
4/// 
5/// # Examples
6/// 
7/// ```
8/// create!(
9///     client;
10///     Task {
11///         project: "foo_project".to_string(),
12///         employee: "e42069".to_string(),
13///         description: "nothin' but chillin' 20's".to_string(),
14///         some_metadata: "baz".to_string(),
15///         ..Default::default()
16///     }
17/// )?;
18/// ```
19#[macro_export]
20macro_rules! create {
21    ($client:expr; $inst:expr) => {{
22        let inst = $inst;
23        let inst_keys = inst.primary_keys();
24
25        $client
26            .put_item()
27            .table_name(inst.table__name())
28            .condition_expression("attribute_not_exists(#pk) AND attribute_not_exists(#sk)")
29            .set_expression_attribute_names(Some(HashMap::from([
30                ("#pk".to_string(), inst_keys.hash.field()),
31                ("#sk".to_string(), inst_keys.range.field()),
32            ])))
33            .set_item(Some(inst.into()))
34            .send()
35            .await
36    }};
37}