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:
get_key_from_id— construct aKey<TD>from aKeyIdwithout an instance of the typeget_key_id— extract the logicalKeyIdfrom an existing instanceget_key— convenience: extract theKeyIdfromselfand immediately build theKey<TD>
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§
Sourcetype KeyId<'id>
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§
Sourcefn get_key_from_id(key_id: Self::KeyId<'_>) -> Key<TD>
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");Sourcefn get_key_id(&self) -> Self::KeyId<'_>
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§
Sourcefn get_key(&self) -> Key<TD>
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.