Esempio n. 1
0
def create_table(
    table_name: str,
    key_schema: list,
    attribute_definition: list,
    lsi=[],
    gsi=[],
) -> dict:
    dynamodb = get_resource()
    ddl = {
        "TableName": table_name,
        "KeySchema": key_schema,
        "AttributeDefinitions": attribute_definition,
        "BillingMode": "PAY_PER_REQUEST",
    }
    if lsi:
        ddl["LocalSecondaryIndexes"] = lsi
    if gsi:
        ddl["GlobalSecondaryIndexes"] = gsi
    try:
        response = dynamodb.create_table(**ddl)
    except Exception as e:
        print(response)
        raise e

    return response
Esempio n. 2
0
def scan_table(
    table_name: str,
    limit: int,
    filter_expression: dict,
) -> dict:
    dynamodb = get_resource()
    dynamodb_table = dynamodb.Table(table_name)
    try:
        response = dynamodb_table.scan(
            Limit=limit,
            **filter_expression,
        )
        response_items = response["Items"]
        count = response.get("Count")
        while count < limit and response.get("LastEvaluatedKey"):
            response = dynamodb_table.scan(
                TableName=table_name,
                ExclusiveStartKey=response.get("LastEvaluatedKey"),
                Limit=(limit - count),
                FIlterExpression=filter_expression,
            )
            count += response.get("Count")
            response_items.append(response["Items"])

    except Exception as e:
        print(response)
        raise e

    return response_items
Esempio n. 3
0
def put_item(table_name: str, payload: dict) -> dict:
    dynamodb = get_resource()
    try:
        dynamodb_table = dynamodb.Table(table_name)
        response = dynamodb_table.put_item(Item=payload)
    except Exception as e:
        raise e

    return response
Esempio n. 4
0
def get_item(table_name: str, key_clause: dict) -> dict:
    dynamodb = get_resource()
    try:
        dynamodb_table = dynamodb.Table(table_name)
        response = dynamodb_table.get_item(Key=key_clause)
    except Exception as e:
        raise e

    return response["Item"]
Esempio n. 5
0
def drop_table(table_name: str, ) -> dict:
    dynamodb = get_resource()
    dynamodb_table = dynamodb.Table(table_name)
    try:
        response = dynamodb_table.delete()
    except Exception as e:
        print(response)
        raise e

    return response
Esempio n. 6
0
def update_item(table_name: str, update_expression: dict) -> dict:
    dynamodb = get_resource()
    dynamodb_table = dynamodb.Table(table_name)
    try:
        response = dynamodb_table.update_item(**update_expression)
    except Exception as e:
        print(response)
        raise e

    return response
Esempio n. 7
0
def delete_item(table_name: str, pkey: str, skey=None) -> dict:
    dynamodb = get_resource()
    try:
        dynamodb_table = dynamodb.Table(table_name)
        for key in dynamodb_table.key_schema:
            if key["KeyType"] == "HASH":
                pkey_name = key["AttributeName"]
            else:
                skey_name = key["AttributeName"]
        key_clause = {pkey_name: pkey}
        if skey:
            key_clause[skey_name] = skey
        response = dynamodb_table.delete_item(Key=key_clause)
    except Exception as e:
        raise e

    return response
Esempio n. 8
0
def query_item(
    table_name: str, key_conditions: dict, limit: int, index=None,
) -> dict:
    dynamodb = get_resource()
    dynamodb_table = dynamodb.Table(table_name)
    try:
        if index:
            response = dynamodb_table.query(
                Limit=limit, **key_conditions, IndexName=index,
            )
        else:
            response = dynamodb_table.query(Limit=limit, **key_conditions)
        response_items = response["Items"]
        count = response.get("Count")
        while count < limit and response.get("LastEvaluatedKey"):
            if index:
                response = dynamodb_table.query(
                    Limit=(limit - count),
                    **key_conditions,
                    IndexName=index,
                    ExclusiveStartKey=response.get("LastEvaluatedKey"),
                )
            else:
                response = dynamodb_table.query(
                    Limit=(limit - count),
                    **key_conditions,
                    ExclusiveStartKey=response.get("LastEvaluatedKey"),
                )
            count += response.get("Count")
            response_items.append(response["Items"])

    except Exception as e:
        print(response)
        raise e

    return response_items