HENNGE / aiodynamo

Asynchronous, fast, pythonic DynamoDB Client
https://aiodynamo.readthedocs.io/
Other
73 stars 21 forks source link

Document Condition and KeyCondition #91

Open dimaqq opened 3 years ago

dimaqq commented 3 years ago

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

ojii commented 3 years ago

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 commented 3 years ago

Okay I can see the intention.

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

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

https://github.com/HENNGE/aiodynamo/blob/fc179f3d3c02f081857d700dd6383c6aeb0d2631/src/aiodynamo/expressions.py#L229-L245

raulolteanu-sudo commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Thanks, @ojii , that worked great!