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
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
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
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"]
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
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
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
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