DynamoDBClient

Struct DynamoDBClient 

Source
pub struct DynamoDBClient { /* private fields */ }
Expand description

Dynamodb client for host interfaces.

This client uses Momento’s host-provided AWS communication channel, which is kept hot at all times. When your Function has not run in several days or more, the channel is still hot and ready, keeping your Function invocations predictable even when your demand is unpredictable.

Implementations§

Source§

impl DynamoDBClient

Source

pub fn new(credentials: &AwsCredentialsProvider) -> Self

Create a new DynamoDB client.

let client = DynamoDBClient::new(
    &AwsCredentialsProvider::new(
        "us-east-1",
        build_environment_aws_credentials!()
    )?
);
Source

pub fn get_item<V, E>( &self, table_name: impl Into<String>, key: impl Into<Key>, ) -> Result<Option<V>, GetItemError<E>>
where V: TryFrom<Item, Error = E>,

Get an item from a DynamoDB table.

Examples:


Custom bound types:

use momento_functions_host::aws::ddb::{AttributeValue, DynamoDBClient, DynamoDBError, GetItemError, Item};

/// Look up an item from a DynamoDB table and deserialize it into a MyStruct.
/// Returns None if the item does not exist.
fn get_my_struct(client: &DynamoDBClient, which_one: &str) -> Result<Option<MyStruct>, GetItemError<String>> {
    client.get_item("my_table", ("some_attribute", which_one))
}

struct MyStruct {
    some_attribute: String,
}

// Boilerplate to convert from dynamodb format

impl TryFrom<Item> for MyStruct {
    type Error = String;
    fn try_from(mut value: Item) -> Result<Self, Self::Error> {
        Ok(Self {
            some_attribute: value.attributes.remove("some_attribute").ok_or("missing some_attribute")?.try_into()?,
        })
    }
}
Source

pub fn get_item_raw( &self, table_name: impl Into<String>, key: impl Into<Key>, ) -> Result<Option<Item>, DynamoDBError>

Get an item from a DynamoDB table.

Examples:


use momento_functions_host::aws::ddb::{DynamoDBClient, DynamoDBError, Item};

/// Read an item from a DynamoDB table "my_table" with a S key attribute "some_attribute".
fn get_some_item(client: &DynamoDBClient, which_one: &str) -> Result<Option<Item>, DynamoDBError> {
    client.get_item_raw("my_table", ("some_attribute", which_one))
}
Source

pub fn put_item( &self, table_name: impl Into<String>, item: impl Into<Item>, ) -> Result<(), DynamoDBError>

Put an item into a DynamoDB table.

Examples: Raw item:



client.put_item(
    "my_table",
    [
        ("some_attribute", "some S value"),
        ("some_other_attribute", "some other S value"),
    ]
)

Custom bound types:

use momento_functions_host::aws::ddb::{AttributeValue, DynamoDBClient, DynamoDBError, Item};

/// Store an item in a DynamoDB table by serializing a MyStruct.
fn put_my_struct(client: &DynamoDBClient, which_one: MyStruct) -> Result<(), DynamoDBError> {
    client.put_item("my_table", which_one)
}

struct MyStruct {
    some_attribute: String,
}

// Boilerplate to convert into dynamodb format
impl From<MyStruct> for Item {
    fn from(value: MyStruct) -> Self {
        [
            ("some_attribute", AttributeValue::from(value.some_attribute)),
        ].into()
    }
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.