rayepps/dynamof

View on GitHub
dynofunc/operations/query.py

Summary

Maintainability
A
0 mins
Test Coverage

from dynofunc.core import builder as ab
from dynofunc.core import args
from dynofunc.core.utils import shake
from dynofunc.core.model import Operation, Condition
from dynofunc.core.response import response


def query(table_name: str, conditions: Condition, index_name: str = None):
    """Creates an Operation that will query a table for items that match the given
    conditions when run.

    Args:
        table_name (str): The name of the table to query
        conditions (Condition): A condition object that should be used to determine which items to return
            Examples:
                - query('products', conditions: attr('title').equals('Old Buggy'))
                - query('products', conditions: cand(attr('type').equals('car'), attr('color').equals('red'))

    """
    build = ab.builder(
        table_name=table_name,
        index_name=index_name,
        conditions=conditions)
    description = shake(
        TableName=build(args.TableName),
        IndexName=build(args.IndexName),
        KeyConditionExpression=build(args.KeyConditionExpression),
        ExpressionAttributeNames=build(args.ExpressionAttributeNames),
        ExpressionAttributeValues=build(args.ExpressionAttributeValues))
    return Operation(description, run)

def run(client, description):
    res = client.query(**description)
    return response(res)