nayaverdier / dyntastic

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

support for env variable based metadata #15

Closed peterb154 closed 8 months ago

peterb154 commented 8 months ago

Hey all, just stumbled on this project and I LOVE it so far.

I use a the docker based amazon/dynamodb-local:latest image for local testing and deploy tables into AWS with cdk, which makes the tables names variable based on the deployment.

When testing locally, I have to change the model to set __table_host__ and __table_region__ to the local ddb instance, and remember to remove that before commit/deploy.

And for the __table_name__, I currently have to hard code the table name in cdk so that it matches the model definition. I'd rather use the dynamically generated table names and then set that as an ENV Variable on my lambdas/containers.

I think it would be great if these props be set natively using environment variables, rather than hard coded in the models. That way they would be easy to over-ride for local testing/unit tests and could be dynamically set when deployed to AWS.

If you are in support of this idea, I'll create a PR to implement.

nayaverdier commented 8 months ago

Hey! For table name it supports being set to a callable (e.g. __table_name__ = lambda: os.getenv(...)), is that sufficient for your use case? For the local testing case, great idea to allow setting the host/region configuration without needing any code changes.

Probably safe to use what's provided in the table definition, and then just default to DYNTASTIC_HOST and DYNTASTIC_REGION if not defined (we should do it at first use rather than at import time to enable other code dynamically changing environment variables, like python-dotenv).

If you're feeling ambitious, feel free to add support for those two fields to be callable similar to table name as well (see _resolve_table_name function in main.py).

peterb154 commented 8 months ago

Just created a PR - https://github.com/nayaverdier/dyntastic/pull/17 see what you think. Note that the environment variables have precedence over the hard set meta attributes. I think that works best so that hard set production values can be easily overridden in local testing.

nayaverdier commented 8 months ago

Release in 0.14.0!