def add_extended_table_functions()

in api/utils.py [0:0]


def add_extended_table_functions(table):
    def get_existing_item(Key, *args, **kwargs):
        """
        Add a new 'get_existing_item' method for our tables that will throw a 404 when it doesn't exist
        """
        response = table.get_item(Key=Key, *args, **kwargs)
        if 'Item' not in response:
            raise NotFoundError(f"{table.name} : {Key} Could Not Be Found")
        return response['Item']

    def iter_query(*args, **kwargs):
        """Unwrap pagination from DynamoDB query results to yield items"""
        query_results = None
        while query_results is None or 'LastEvaluatedKey' in query_results:
            if query_results is not None:
                kwargs['ExclusiveStartKey'] = query_results['LastEvaluatedKey']
            query_results = table.query(*args, **kwargs)
            for item in query_results['Items']:
                yield item

    table.get_existing_item = get_existing_item
    table.iter_query = iter_query