Skip to main content

KeyBuilder

Trait KeyBuilder 

Source
pub trait KeyBuilder<TD: TableDefinition> {
    type KeyId<'id>;

    // Required methods
    fn get_key_from_id(key_id: Self::KeyId<'_>) -> Key<TD>;
    fn get_key_id(&self) -> Self::KeyId<'_>;

    // Provided method
    fn get_key(&self) -> Key<TD> { ... }
}
Expand description

Builds DynamoDB keys from type-safe key IDs.

This trait is automatically implemented for every type that implements DynamoDBItem. It provides three methods:

The associated type KeyId<'id> is a KeyId<PkId, SkId> whose concrete PkId and SkId types are determined by the item’s HasAttribute impls.

§Examples

Building a key from an existing instance:

use dynamodb_facade::{Key, KeyBuilder};

let user = sample_user();
let key: Key<PlatformTable> = user.get_key();

let raw = key.into_inner();
assert_eq!(raw["PK"].as_s().unwrap(), "USER#user-1");
assert_eq!(raw["SK"].as_s().unwrap(), "USER");

Building a key from a KeyId without an instance:

use dynamodb_facade::{Key, KeyBuilder, KeyId};

let key: Key<PlatformTable> = User::get_key_from_id(KeyId::pk("user-42"));

let raw = key.into_inner();
assert_eq!(raw["PK"].as_s().unwrap(), "USER#user-42");
assert_eq!(raw["SK"].as_s().unwrap(), "USER");

Required Associated Types§

Source

type KeyId<'id>

The logical key identifier type for this item, typically a KeyId<PkId, SkId> whose components are derived from the item’s HasAttribute implementations.

Required Methods§

Source

fn get_key_from_id(key_id: Self::KeyId<'_>) -> Key<TD>

Constructs a Key<TD> from a KeyId without requiring an instance of the implementing type.

§Examples
use dynamodb_facade::{Key, KeyBuilder, KeyId};

let key: Key<PlatformTable> = User::get_key_from_id(KeyId::pk("user-42"));

let raw = key.into_inner();
assert_eq!(raw["PK"].as_s().unwrap(), "USER#user-42");
assert_eq!(raw["SK"].as_s().unwrap(), "USER");
Source

fn get_key_id(&self) -> Self::KeyId<'_>

Extracts the logical KeyId from an existing instance of the implementing type.

The returned KeyId borrows from self and can be passed to get_key_from_id to produce a Key<TD>.

§Examples
use dynamodb_facade::{KeyBuilder, KeyId, NoId};

let user = sample_user();
let key_id: KeyId<&str, NoId> = <User as KeyBuilder<PlatformTable>>::get_key_id(&user);

Provided Methods§

Source

fn get_key(&self) -> Key<TD>

Convenience method that builds a Key<TD> directly from self.

Equivalent to calling get_key_id followed by get_key_from_id. Prefer this method when you have an instance of the item and simply need its DynamoDB primary key.

§Examples
use dynamodb_facade::{Key, KeyBuilder};

let user = sample_user();
let key: Key<PlatformTable> = user.get_key();

let raw = key.into_inner();
assert_eq!(raw["PK"].as_s().unwrap(), "USER#user-1");
assert_eq!(raw["SK"].as_s().unwrap(), "USER");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<TD: TableDefinition, T> KeyBuilder<TD> for T
where T: KeyBuilderHelper<TD, <TD::KeySchema as KeySchema>::Kind>,

Source§

type KeyId<'id> = KeyId<<T as KeyBuilderHelper<TD, <<TD as TableDefinition>::KeySchema as KeySchema>::Kind>>::PkId<'id>, <T as KeyBuilderHelper<TD, <<TD as TableDefinition>::KeySchema as KeySchema>::Kind>>::SkId<'id>>