pythonpython-3.xamazon-web-servicesamazon-dynamodbpynamodb

PynamoDB same model with multipe databases


The way PynamoDB is implemented is that it looks to a specific single DynamoDB table:

class UserModel(Model):
    class Meta:
        # Specific table.
        table_name = 'dynamodb-user'
        region = 'us-west-1'

The way my infrastructure works is that it has as many dynamodb tables as I have clients, so a single Lambda function has to deal with any amount of separate tables that are identical in structure e.g. represent "UserModel". I can't specify a concrete one.

How would I make this model definition dynamic?

Thanks!


Solution

  • Open-sourced a solution that is tested and works.

    https://github.com/Biomapas/B.DynamoDbCommon/blob/master/b_dynamodb_common/models/model_type_factory.py

    Read README.md for more details.

    Code:

    from typing import TypeVar, Generic, Type
    
    from pynamodb.models import Model
    
    T = TypeVar('T')
    
    
    class ModelTypeFactory(Generic[T]):
        def __init__(self, model_type: Type[T]):
            self.__model_type = model_type
    
            # Ensure that given generic belongs to pynamodb.Model class.
            if not issubclass(model_type, Model):
                raise TypeError('Given model type must inherit from pynamodb.Model class!')
    
        def create(self, custom_table_name: str, custom_region: str) -> Type[T]:
            parent_class = self.__model_type
    
            class InnerModel(parent_class):
                class Meta:
                    table_name = custom_table_name
                    region = custom_region
    
            return InnerModel