nayaverdier / dyntastic

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

Index documentation / source #12

Closed photonbit closed 11 months ago

photonbit commented 11 months ago

I am trying from pynamodb to dyntastic and I am finding trouble to understand how to use indexes or which ones are supported. I've found some misleading clues:

https://github.com/nayaverdier/dyntastic/blob/main/dyntastic/main.py#L66

# TODO: support INCLUDE projection?
        self.projection = "KEYS_ONLY" if keys_only else "ALL"

https://github.com/nayaverdier/dyntastic/blob/main/dyntastic/main.py#L98

        validated, fields_set, errors = validate_model(model, item)
        if errors:
            # assume KEYS_ONLY or INCLUDE index

https://github.com/nayaverdier/dyntastic/blob/main/README.md?plain=1#L164

DynamoDB Indexes using a `KEYS_ONLY` or `INCLUDE` projection are supported:

So what are the projections supported?

Also, how could we use the dyntastic.Index class so I can add the index as a field to another model?

nayaverdier commented 11 months ago

@photonbit That is confusing from the source, thanks for pointing that out. The first comment, from Index, is only used for table creation, which is meant just for setting up tests at this point, and that only supports keys only indexes.

But all indexes should be supported, just by passing the index name as a string to the query function. If an index does not include all the attributes, the resulting models will error when you try to access a missing attribute (until you call refresh(), or if you pass load_full_item=True to query.

photonbit commented 11 months ago

Perfect, thank you!