HENNGE / aiodynamo

Asynchronous, fast, pythonic DynamoDB Client

Home Page:https://aiodynamo.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Document Condition and KeyCondition

dimaqq opened this issue · comments

I can't figure out what the difference between Condition and KeyCondition is 😢

I can't figure out what the difference between Condition and KeyCondition is 😢

KeyCondition can be thought of as a specialized version of Condition that only works on the key fields. They are constructed with HashKey(field, value) (since the hash key always needs to be set to an exact value) and can optionally be combined with a RangeKey(field).<operation>(...).

Condition on the other hand do not care about which field they operate on, so they are constructed with F(path).<operation>(...) and can be combined freely.

KeyCondition is special to prevent mistakes, such as for example trying to do F("hash_key").lt(12) which is not allowed in Dynamo.

aiodynamos KeyCondition is used to build a KeyConditionExpression in DynamoDB, while aiodynamos Condition is used to build a ConditionExpression.

Okay I can see the intention.

Looking over this code though, it's a bit confusing, because:

  • partition key can only equal something (in a query operation), while
  • sort key can equal/being with/lt/gt/etc.

Meanwhile, both partition and partition + sort key (and their values) are both KeyConditions 🤷🏿

@dataclass(frozen=True)
class HashKey(KeyCondition):
"""
Used for Key Conditions. To also constrain the Key Condition by the
range key, create an instance of RangeKey, call a method on it and
combine the HashKey with the return value of that method call using
the & operator.
"""
name: str
value: Any
def encode(self, params: Parameters) -> str:
return f"{params.encode_path(KeyPath(self.name))} = {params.encode_value(self.value)}"
def __and__(self, other: Condition) -> KeyCondition:
return HashAndRangeKeyCondition(self, other)

Guys, how can i write a query after a certain GlboalSecondary index that a table contains ? how would the query look in aiodynamo ?(can't seem to figure it out)

Guys, how can i write a query after a certain GlboalSecondary index that a table contains ? how would the query look in aiodynamo ?(can't seem to figure it out)

could you explain it a bit more? are you trying to do a query(...) on a secondary index? Are you looking for query("table-name", HashKey("secondary-index-key-field", "value"), index="name-of-secondary-index")?

Yep that's the one thanks. Was a bit confused but now it makes sense,
HashKey("secondary-index-key-field", "value") - this was the part that was a bit unclear. (didn't know how to compose the underlying KeyCondition for the GlobalSecondaryIndex but HashKey function fills both roles)

Can someone help me with an example of doing a query on a table ?
I really am confused as to how to put a KeyCondition in a query operation.

Can someone help me with an example of doing a query on a table ? I really am confused as to how to put a KeyCondition in a query operation.

can you describe what you're trying to do and what they key schema of your table is?

I'm also not sure of how to do a particular query. Can someone help me query the following table using aiodynamo - I want to find all ids that include "ABCDEF" in their players map:

{ "id": { "S": "123456" }, "players": { "M": { "ABCDEF": { "M": { "name": { "S": "kipkoan" } } } } } }

I'm also not sure of how to do a particular query. Can someone help me query the following table using aiodynamo - I want to find all ids that include "ABCDEF" in their players map:

F("players", "ABCDEF").exists()

Thanks, @ojii , that worked great!