UpdateRequest¶
An UpdateRequest
is used to perform an update_item
transaction with DynamoDB.
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html
-
class
cerami.request.
UpdateRequest
(client, tablename='', request_attributes=None, reconstructor=None)¶ A class to perform the update request
-
__init__
(client, tablename='', request_attributes=None, reconstructor=None)¶ constructor for base request
- Parameters
client – A
boto3.client('dynamodb')
tablename – the name of the table to perform the request on
request_attributes –
a dict of SearchAttribute objects whose keys represent options that can be passed to client upon execution For example, it may include a FilterExpression key whose value is a SearchAttribute that resolves to a string of filters. This is typically None but can be used to manually build requests:
Parent.scan.filter(Parent.name == 'Mom').project(Parent.name) # The search_attributes can be manually specified ScanRequest(client=client, tablename='parents', search_attributes={ 'FilterExpression': SearchAttribute('name = Mom'), 'ProjectionExpression': SearchAttribute('name'), })
reconstructor – a Reconstructor object
-
add
(datatype, value)¶ Add an ADD statement to the UpdateExpression
Amazon recommends only using with numbers and sets
The UpdateExpression is a comma separated list of instructions to perform on the table. ADD can be used to change the value of the Number or add to a Set.
- Parameters
datatype – a DynamoDataType object to change
value – the value to change the datatype For Numbers the value should be a number, it can be negative to subtract For Sets, the value should be an array
- Returns
the caller of the method. This allows for chaining
For example:
Person.update.key(Person.email == "test@test.com").add(Person.age, 10).build() { "TableName": "people", "ReturnValues": "ALL_NEW", "Key": { "email": { "S": "test@test.com" } }, "UpdateExpression": "ADD #__age :_age_dzszf", "ExpressionAttributeNames": { "#__age": "age" }, "ExpressionAttributeValues": { ":_age_dzszf": { "N": "10" } } }
-
add_attribute
(attr_class, name, value)¶ add a search attribute to a to the request_attributes dict
All search attributes must be unique keys. When the key already exists, it will update that value by calling
add()
. Depending on theattr_class
, this can overwrite, append, change the value of the existing search attribute.- Parameters
attr_class – a SearchAttribute class
name – the name of the attribute
value – the value that will be added to the SearchAttribute
For example:
Person.scan.limit(10) # or ... Person.scan.add_attribute(SearchAttribute, 'Limit', 10)
-
build
()¶ build the dict used by dynamodb
- Returns
a dict whose keys matching the keys of the request_attributes and whose values are string versions of each attribute
For example:
User.scan.filter(User.email == 'test@test.com').build() { "TableName": "Users", "FilterExpression": "#__email = :_email_dpxqm", "ExpressionAttributeNames": { "#__email": "email" }, "ExpressionAttributeValues": { ":_email_dpxqm": { "S": "test@test.com" } } }
-
delete
(datatype, value)¶ Add a DELETE statement to the UpdateExpression
The UpdateExpression is a comma separated list of instructions to perform on the table. DELETE can be used to remove one or more elements from a Set only.
- Parameters
datatype – a Set DynamoDatatype object
value – an array of values to remove from the set.
- Returns
the caller of the method. This allows for chaining
For example:
Person.update \ .key(Person.email == "test@test.com") \ .delete(Person.tags, ["cool", "awesome"]) \ .build() { "TableName": "people", "ReturnValues": "ALL_NEW", "Key": { "name": { "S": "test@test.com" } }, "UpdateExpression": "DELETE #__tags :_tags_bttwj", "ExpressionAttributeNames": { "#__tags": "tags" }, "ExpressionAttributeValues": { ":_tags_bttwj": { "SS": [ "cool", "awesome" ] } } }
-
execute
()¶ perform the update_item request
Returns: a SaveResponse object built from the update_item response
For example:
Person.update \ .key(Person.email == 'test@test.com') \ .set(Person.name, 'Mommy') \ .execute()
-
key
(*expressions)¶ return a new Request setup with the Key attribute
Adds the Key to the request_attributes dict
- Parameters
*expressions – a list of
BaseExpressions
- Returns
the caller of the method. This allows for chaning
For example:
Person.get.key(Person.email == 'test@test.com').build() { "TableName": "people", "Key": { "email": { "S": "test@test.com" } } }
-
remove
(*datatypes)¶ Add a REMOVE statement to the UpdateExpression
The UpdateExpression is a comma separated list of instructions to perform on the table. REMOVE will delete the specified datatypes from the record. REMOVE can also be used to remove elements from a List datatype when used with List.index()
- Parameters
*datatypes – a list of DynamoDataType objects
- Returns
the caller of the method. This allows for chaining
For example:
Person.update \ .key(Person.email == "test@test.com") \ .remove(Person.name) \ .build() { "TableName": "people", "ReturnValues": "ALL_NEW", "Key": { "email": { "S": "test@test.com" } }, "UpdateExpression": "REMOVE #__name :_name_butcw", "ExpressionAttributeNames": { "#__name": "name" } }
-
returns
(value)¶ return the Request setup with ReturnValues attribute
Adds the ReturnValues to the request attributes dict
- Parameters
value – NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW
- Returns
the caller of the method. This allows for chaining
For example:
from cerami.request.return_values import UPDATED_NEW Person.update \ .key(Person.email == 'test@test.com') \ .set(Person.name, 'new name') \ .returns(UPDATED_NEW) \ .build() { "TableName": "people", "ReturnValues": "UPDATED_NEW", "Key": { "email": { "S": "test@test.com" } }, "UpdateExpression": "SET #__name = :_name_zhvzz", "ExpressionAttributeNames": { "#__name": "name" }, "ExpressionAttributeValues": { ":_name_zhvzz": { "S": "new name" } } }
-
set
(expression_or_datatype, value=None)¶ Add a SET statement to the UpdateExpression
The UpdateExpression is a comma separated list of instructions to perform on the table. SET will change the value or put the value if it does not exist.
Sorry, i dont know how to do method overloading in python yet, so were stuck with this garbage..
- Parameters
datatype_or_expresion – a DynamoDataType or BaseExpresion object to change
value – the value to change the datatype
- Returns
the caller of the method. This allows for chaining
For example:
Person.update \ .key(Person.email == "test@test.com") \ .set(Person.name, "Zac") \ .execute() Person.update \ .key(Person.email == "test@test.com") \ .set(Person.age.add(10)) .execute()
-
update_expression
(action, expression)¶ return a Request setup with the update expression attributes
Adds the UpdateExpression, ExpressionAttributeNames and ExpressionAttributeValues to the request_attributes dict
- Parameters
action – ADD | SET | DELETE | UPDATE
expression – a BaseExpression object
- Returns
the caller of the method. This allows for chaining
-