pub struct KeyCondition<'a, KS: KeySchema, S: KeyConditionState = PkOnly>(/* private fields */);Expand description
Builder for DynamoDB key condition expressions.
KeyCondition builds the KeyConditionExpression used in Query
operations. It enforces that:
- A partition key equality condition is always provided (via
pk). - Sort key methods (
sk_eq,sk_begins_with, etc.) are only available for composite-key schemas (tables or indexes with a sort key). - At most one sort key condition can be added, as required by the DynamoDB API.
In practice you rarely name KS or S directly — they are inferred from
the item type’s key schema when using the generated T::key_condition(pk_id)
helper.
§Examples
PK-only query (all enrollments for a user):
use dynamodb_facade::{KeyCondition, TableSchema};
// Targets all items with PK = "USER#user-1"
let kc = KeyCondition::<TableSchema<PlatformTable>>::pk("USER#user-1");
assert_eq!(format!("{kc}"), r#"PK = S("USER#user-1")"#);
// Or you may prefer for the same result:
use dynamodb_facade::DynamoDBItemOp;
let kc = User::key_condition("user-1");
assert_eq!(format!("{kc}"), r#"PK = S("USER#user-1")"#);PK + SK prefix query:
use dynamodb_facade::{KeyCondition, TableSchema};
let kc = KeyCondition::<TableSchema<PlatformTable>>::pk("USER#user-1")
.sk_begins_with("ENROLL#");
assert_eq!(format!("{kc}"), r#"(PK = S("USER#user-1") AND begins_with(SK, S("ENROLL#")))"#);
// Or you may prefer for the same result:
use dynamodb_facade::DynamoDBItemOp;
let kc = Enrollment::key_condition("user-1").sk_begins_with("ENROLL#");
assert_eq!(format!("{kc}"), r#"(PK = S("USER#user-1") AND begins_with(SK, S("ENROLL#")))"#);Implementations§
Source§impl<'a, KS: KeySchema> KeyCondition<'a, KS>
impl<'a, KS: KeySchema> KeyCondition<'a, KS>
Sourcepub fn pk(value: impl IntoAttributeValue) -> Self
pub fn pk(value: impl IntoAttributeValue) -> Self
Creates a key condition with a partition key equality constraint: PK = value.
This is the required starting point for all key conditions. The partition
key attribute name is taken from the key schema KS at compile time.
§Examples
use dynamodb_facade::{KeyCondition, TableSchema};
let kc = KeyCondition::<TableSchema<PlatformTable>>::pk("USER#user-1");
assert_eq!(format!("{kc}"), r#"PK = S("USER#user-1")"#);
// Or you may prefer for the same result:
use dynamodb_facade::DynamoDBItemOp;
let kc = User::key_condition("user-1");
assert_eq!(format!("{kc}"), r#"PK = S("USER#user-1")"#);Source§impl<'a, KS: CompositeKeySchema> KeyCondition<'a, KS>
impl<'a, KS: CompositeKeySchema> KeyCondition<'a, KS>
Sourcepub fn sk_eq(
self,
value: impl IntoAttributeValue,
) -> KeyCondition<'a, KS, WithSk>
pub fn sk_eq( self, value: impl IntoAttributeValue, ) -> KeyCondition<'a, KS, WithSk>
Adds a sort key equality constraint: SK = value.
§Examples
use dynamodb_facade::DynamoDBItemOp;
let kc = Enrollment::key_condition("user-1").sk_eq("ENROLL#course-42");
assert_eq!(format!("{kc}"), r#"(PK = S("USER#user-1") AND SK = S("ENROLL#course-42"))"#);Sourcepub fn sk_lt(
self,
value: impl IntoAttributeValue,
) -> KeyCondition<'a, KS, WithSk>
pub fn sk_lt( self, value: impl IntoAttributeValue, ) -> KeyCondition<'a, KS, WithSk>
Adds a sort key less-than constraint: SK < value.
§Examples
use dynamodb_facade::DynamoDBItemOp;
let kc = Enrollment::key_condition("user-1").sk_lt("ENROLL#z");
assert_eq!(format!("{kc}"), r#"(PK = S("USER#user-1") AND SK < S("ENROLL#z"))"#);Sourcepub fn sk_le(
self,
value: impl IntoAttributeValue,
) -> KeyCondition<'a, KS, WithSk>
pub fn sk_le( self, value: impl IntoAttributeValue, ) -> KeyCondition<'a, KS, WithSk>
Adds a sort key less-than-or-equal constraint: SK <= value.
§Examples
use dynamodb_facade::DynamoDBItemOp;
let kc = Enrollment::key_condition("user-1").sk_le("ENROLL#z");
assert_eq!(format!("{kc}"), r#"(PK = S("USER#user-1") AND SK <= S("ENROLL#z"))"#);Sourcepub fn sk_gt(
self,
value: impl IntoAttributeValue,
) -> KeyCondition<'a, KS, WithSk>
pub fn sk_gt( self, value: impl IntoAttributeValue, ) -> KeyCondition<'a, KS, WithSk>
Adds a sort key greater-than constraint: SK > value.
§Examples
use dynamodb_facade::DynamoDBItemOp;
let kc = Enrollment::key_condition("user-1").sk_gt("ENROLL#2024");
assert_eq!(format!("{kc}"), r#"(PK = S("USER#user-1") AND SK > S("ENROLL#2024"))"#);Sourcepub fn sk_ge(
self,
value: impl IntoAttributeValue,
) -> KeyCondition<'a, KS, WithSk>
pub fn sk_ge( self, value: impl IntoAttributeValue, ) -> KeyCondition<'a, KS, WithSk>
Adds a sort key greater-than-or-equal constraint: SK >= value.
§Examples
use dynamodb_facade::DynamoDBItemOp;
let kc = Enrollment::key_condition("user-1").sk_ge("ENROLL#2024");
assert_eq!(format!("{kc}"), r#"(PK = S("USER#user-1") AND SK >= S("ENROLL#2024"))"#);Sourcepub fn sk_between(
self,
low: impl IntoAttributeValue,
high: impl IntoAttributeValue,
) -> KeyCondition<'a, KS, WithSk>
pub fn sk_between( self, low: impl IntoAttributeValue, high: impl IntoAttributeValue, ) -> KeyCondition<'a, KS, WithSk>
Adds a sort key range constraint: SK BETWEEN low AND high (inclusive).
§Examples
use dynamodb_facade::DynamoDBItemOp;
let kc = Enrollment::key_condition("user-1").sk_between("ENROLL#2024-01", "ENROLL#2024-12");
assert_eq!(
format!("{kc}"),
r#"(PK = S("USER#user-1") AND SK BETWEEN S("ENROLL#2024-01") AND S("ENROLL#2024-12"))"#,
);Sourcepub fn sk_begins_with(
self,
prefix: impl IntoAttributeValue,
) -> KeyCondition<'a, KS, WithSk>
pub fn sk_begins_with( self, prefix: impl IntoAttributeValue, ) -> KeyCondition<'a, KS, WithSk>
Adds a sort key prefix constraint: begins_with(SK, prefix).
This is the most common sort key condition for hierarchical single-table
designs, where sort keys are prefixed by entity type (e.g. "ENROLL#").
§Examples
use dynamodb_facade::DynamoDBItemOp;
let kc = Enrollment::key_condition("user-1").sk_begins_with("ENROLL#2025");
assert_eq!(
format!("{kc}"),
r#"(PK = S("USER#user-1") AND begins_with(SK, S("ENROLL#2025")))"#,
);Trait Implementations§
Source§impl<'a, KS: Clone + KeySchema, S: Clone + KeyConditionState> Clone for KeyCondition<'a, KS, S>
impl<'a, KS: Clone + KeySchema, S: Clone + KeyConditionState> Clone for KeyCondition<'a, KS, S>
Source§fn clone(&self) -> KeyCondition<'a, KS, S>
fn clone(&self) -> KeyCondition<'a, KS, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a, KS: Debug + KeySchema, S: Debug + KeyConditionState> Debug for KeyCondition<'a, KS, S>
impl<'a, KS: Debug + KeySchema, S: Debug + KeyConditionState> Debug for KeyCondition<'a, KS, S>
Auto Trait Implementations§
impl<'a, KS, S> Freeze for KeyCondition<'a, KS, S>
impl<'a, KS, S> RefUnwindSafe for KeyCondition<'a, KS, S>where
KS: RefUnwindSafe,
S: RefUnwindSafe,
impl<'a, KS, S> Send for KeyCondition<'a, KS, S>
impl<'a, KS, S> Sync for KeyCondition<'a, KS, S>
impl<'a, KS, S> Unpin for KeyCondition<'a, KS, S>
impl<'a, KS, S> UnsafeUnpin for KeyCondition<'a, KS, S>
impl<'a, KS, S> UnwindSafe for KeyCondition<'a, KS, S>where
KS: UnwindSafe,
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more