nayaverdier / dyntastic

A DynamoDB library on top of Pydantic and boto3.
MIT License
55 stars 12 forks source link

[FEATURE] Support KEYS_ONLY secondary index #9

Closed flolas closed 11 months ago

flolas commented 11 months ago

given a Example Table with secondary index that ha projection_type = 'KEYS_ONLY':

class Example(Dyntastic):
    __table_name__ = "example"
    __hash_key__ = "id"
    __table_host__ = "http://localhost:8000"
    __table_region__ = "us-east-1"

    some_required_attribute: str

when you get the results from dynamodb only return the __hash_key__ because of projection_type, this raises an error when calling the query() method because some_required_attribute is missing.

query_results = cls.query(
    A.is == id,
    index="example-index",
)

You can make optional all the fields to make this work, but I think that is a bad idea when using schema validation, lol.

Feature Idea: When querying secondary indexes with KEYS_ONLYS projection would be great to: (1) disable the validation of the pydantic schema upon getting the data and validate only keys and return the object with the partion keys only. (2) [optional argument for query] query method return the full object using GET operations after getting (1)

nayaverdier commented 11 months ago

Thanks for the feature request, definitely would like to support this for both query and scan. The tricky part will be running validation / parsing on the keys while ignoring all of the other fields. I'll try to get something out this week.

nayaverdier commented 11 months ago

@flolas This is supported in 0.12.0. You can pass load_full_item=True to automatically refresh and get the full item. I haven't tested with INCLUDE projection indexes, but it should also work in that case.