Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Condition and KeyCondition #91

Open
dimaqq opened this issue Aug 30, 2021 · 10 comments
Open

Document Condition and KeyCondition #91

dimaqq opened this issue Aug 30, 2021 · 10 comments

Comments

@dimaqq
Copy link
Contributor

dimaqq commented Aug 30, 2021

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

@ojii
Copy link
Contributor

ojii commented Aug 30, 2021

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.

@dimaqq
Copy link
Contributor Author

dimaqq commented Aug 30, 2021

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)

@raulolteanu-sudo
Copy link

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)

@ojii
Copy link
Contributor

ojii commented Jan 25, 2022

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")?

@raulolteanu-sudo
Copy link

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)

@mohneeshdamade
Copy link

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.

@ojii
Copy link
Contributor

ojii commented Jun 2, 2022

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?

@kipkoan
Copy link

kipkoan commented Jan 29, 2023

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" } } } } } }

@ojii
Copy link
Contributor

ojii commented Jan 30, 2023

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()

@kipkoan
Copy link

kipkoan commented Jan 30, 2023

Thanks, @ojii , that worked great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants