Ejemplo n.º 1
0
def create(table_name: str,
           hash_key: str,
           range_key: str = None,
           gsi: str = None,
           lsi: str = None):
    """Creates an Operation that will create a table when run.

    Args:
        table_name (str): The name of the table to create
        hash_key (str): The name of the hash key to use for the table
        range_key (str, optional): If provided, will be used as the name of the range key attribute for the table
        gsi (str, optional): If provided, will be used as the name for a global secondary index
        lsi (str, optional): If provided, will be used as the name for a local secondary index

    """
    build = ab.builder(table_name=table_name,
                       hash_key=hash_key,
                       range_key=range_key,
                       gsi=gsi,
                       lsi=lsi)
    description = shake(
        TableName=build(args.TableName),
        KeySchema=build(args.KeySchema),
        AttributeDefinitions=build(args.AttributeDefinitions),
        ProvisionedThroughput=build(args.ProvisionedThroughput),
        LocalSecondaryIndexes=build(args.LocalSecondaryIndexes),
        GlobalSecondaryIndexes=build(args.GlobalSecondaryIndexes))
    return Operation(description, run)
Ejemplo n.º 2
0
def describe(table_name: str):
    """Creates an Operation that will describe a table when run.

    Args:
        table_name (str): The name of the table to describe

    """
    build = ab.builder(table_name=table_name)
    description = shake(TableName=build(args.TableName))
    return Operation(description, run)
Ejemplo n.º 3
0
def scan(table_name: str):
    """Creates an Operation that will scan through every item in a table when run.

    Args:
        table_name (str): The name of the table to scan

    """
    build = ab.builder(table_name=table_name)
    description = shake(TableName=build(args.TableName))
    return Operation(description, run)
Ejemplo n.º 4
0
def add(table_name: str, item: dict, auto_id: bool = None):
    """Creates an Operation that will add an item to a table when run.

    Args:
        table_name (str): The name of the table to add the item to.
        item (dict): The item to add to the table.

    """
    build = ab.builder(table_name=table_name, attributes=item, auto_id=auto_id)
    description = shake(TableName=build(args.TableName),
                        Item=build(args.Item),
                        ReturnValues='ALL_OLD')
    return Operation(description, run)
Ejemplo n.º 5
0
def find(table_name: str, key: dict):
    """Creates an Operation that will find an item in a table when run.

    Args:
        table_name (str): The name of the table to find the item in
        key (dict): An object with a key, value that will be used as the identifier for the item
            Examples:
                - { 'username': '******' }
                - { 'id': '9snw82h', 'type': 'car' } -> when table uses hash key and range key

    """
    build = ab.builder(table_name=table_name, key=key)
    description = shake(TableName=build(args.TableName), Key=build(args.Key))
    return Operation(description, run)
Ejemplo n.º 6
0
def delete(table_name: str, key: dict, conditions: Condition = None):
    """Creates an Operation that will create a table when run.

    Args:
        table_name (str): The name of the table to delete an item from
        key (dict): An object with a key, value that will be used as the identifier of the item to delete
            Examples:
                - { 'username': '******' }
                - { 'id': '9snw82h', 'type': 'car' } -> when table uses hash key and range key
        conditions (Condition, optional): If provided, will be used to create a condition for the delete
            Example:
                - delete('products', { 'id': 12 }, conditions: attr('title').equals('Old Buggy'))

    """
    build = ab.builder(table_name=table_name, key=key, conditions=conditions)
    description = shake(TableName=build(args.TableName),
                        Key=build(args.Key),
                        ConditionExpression=build(args.ConditionExpression),
                        ExpressionAttributeValues=build(
                            args.ExpressionAttributeValues))
    return Operation(description, run)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
def test_builder_creates_data():
    build = ab.builder(table_name='products',
                       key={'id': 13},
                       attributes={'items': ['glow', 'dust']},
                       gsi=[{
                           'name': 'global_index',
                           'hash_key': 'state:int'
                       }],
                       conditions=attr('price').gt(10))

    result = build(lambda r: r)

    expected_item = {
        "alias": "#items",
        "func": None,
        "key": ":items",
        "original": "items",
        "value": {
            "L": [{
                "S": "glow"
            }, {
                "S": "dust"
            }]
        }
    }

    assert result.get('attributes').get('values')[0] == expected_item

    expected_gsi = {
        "hash_key": {
            "name": "state",
            "type": "N"
        },
        "name": "global_index"
    }

    assert result.get('gsi')[0] == expected_gsi
Ejemplo n.º 9
0
def update(table_name: str,
           key: dict,
           attributes: dict,
           conditions: Condition = None):
    """Creates an Operation that will update any items that match the given key with the given
    attributes when run.

    Args:
        table_name (str): The name of the table to update items in
        key (dict): The key that represents an identifier for the item/s to update
            Examples:
                - { 'username': '******' }
                - { 'id': '9snw82h', 'type': 'car' } -> when table uses hash key and range key
        attributes (dict): An arbitrary object of key values to set on the item when updating
            Examples:
                - { 'color': 'red', 'size': 'large' }
                - { 'username': '******' }
        conditions (Condition, optional): A condition object that should be used to filter which items are updated
            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,
                       key=key,
                       attributes=attributes,
                       conditions=conditions)
    description = shake(
        TableName=build(args.TableName),
        Key=build(args.Key),
        ConditionExpression=build(args.ConditionExpression),
        UpdateExpression=build(args.UpdateExpression),
        ExpressionAttributeNames=build(args.ExpressionAttributeNames),
        ExpressionAttributeValues=build(args.ExpressionAttributeValues),
        ReturnValues='ALL_NEW')
    return Operation(description, run)