Skip to main content

KeyCondition

Struct KeyCondition 

Source
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:

  1. A partition key equality condition is always provided (via pk).
  2. Sort key methods (sk_eq, sk_begins_with, etc.) are only available for composite-key schemas (tables or indexes with a sort key).
  3. 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>

Source

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>

Source

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"))"#);
Source

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"))"#);
Source

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"))"#);
Source

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"))"#);
Source

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"))"#);
Source

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"))"#,
);
Source

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>

Source§

fn clone(&self) -> KeyCondition<'a, KS, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, KS: Debug + KeySchema, S: Debug + KeyConditionState> Debug for KeyCondition<'a, KS, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<KS: KeySchema, S: KeyConditionState> Display for KeyCondition<'_, KS, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, KS, S> Freeze for KeyCondition<'a, KS, S>

§

impl<'a, KS, S> RefUnwindSafe for KeyCondition<'a, KS, S>

§

impl<'a, KS, S> Send for KeyCondition<'a, KS, S>
where KS: Send, S: Send,

§

impl<'a, KS, S> Sync for KeyCondition<'a, KS, S>
where KS: Sync, S: Sync,

§

impl<'a, KS, S> Unpin for KeyCondition<'a, KS, S>
where KS: Unpin, S: Unpin,

§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more