Updating fields
#
If not present, all the required parent field's will be created when updating a nested field#
Trying to update a field on a non-existing record (key_value) will create the record.You have multiple operations at your disposition :
#
Creating the table for our example#
1 - Updating a single simple fieldUpdating a field can only be done by selecting a record with its primary key value through the key_value
parameter
(primary key values are unique in DynamoDB).
Specify the field you want to update with the field_path
.
And the value to update with value_to_set
.
If the value you tried to update did not conform to your table model, the data validation will fail, no database
operation will be sent, the field will not be updated, and an update_success
of False
will be returned.
#
2 - Updating a nested map fieldTo navigate into a 'child' field, separate the name of fields with a '.' in the field_path.
#
3 - Updating a nested dict fieldTo update either an item inside a dictionary or one of its nested fields, use the {{yourFieldKeyName}} selector. Then pass the value that will be populated as the key_name with the query_kwargs parameter.
Defining a custom keyName value for your dictionary fields is recommended (for example, friendId), but is optional. By default the keyName will be the field name appended with the word 'key'. The default keyName of a field name 'friends' would be 'friendsKey' and a field named 'achievements' would create the key 'achievementsKey'
#
4 - Updating a deeply nested dict fieldYou can have multiple attributes in your query_kwargs in order to navigate into deeply nested fields. You do not need to avoid defining key names that might overlap when creating your table model, which would make you enable to define different values for the overlapping keys in your query_kwargs. If this does happen to you, the keyName exist purely client side, is not stored in your database, and can be changed at any time.
#
5 - Updating multiple fields at onceYou can use update_multiple_fields to update multiple fields at once with a single database operation.
Like update_field, you select the record you want to update with its primary key value passed in the key_value
parameter.
Specify the different fields you want to update by passing a list of FieldSetter.
Similarly to update_field, each FieldSetter requires a field_path and value_to_set parameter, and has an optional query_kwargs parameter.
You can update any field at any location while they are still in the same record. All of your updates will be grouped in a single database operation.
If any of your setters are being 'detected' as invalid client side (ie, the value_to_set is invalid) no operation will
be sent an update_success
of False
will be returned.
If one of your setters fails after being sent to your database (this is rare, and the main reason is if you tried to navigate into an existing field with an invalid value, for example trying to navigate into a field defined as a dict in your database, but a list is present instead in your database), your setters will usually be atomic, none of them will be executed and update_success of false will be returned. The only exception to this atomicity is if your update operation was over 400kb in size (the max operation size of DynamoDB), in which case StructNoSQL divide your update into multiple operations where the first part of an operation can actually have been executed and will not be rollback if a later part of your update fails.
#
6 : Updating a single field and returning its old valueYou can use update_field_return_old to update a single field value, and return its old value, with only one database operation.
If no existing value was found in the field to update, the old_field_value will be None
.
All of the ways to select nested fields in the above examples can be used here.
The typing of update_success and old_field_value is optional, and is done in separated lines of code, because Python does not support typing while unpacking a tuple.
#
7 : Updating multiple fields at once and returning their old valuesYou can use update_mutliple_fields_return_old to update a single field value, and return their old values, with only one database operation.
Like the previous operations, you select the record you want to update with its primary key value passed in the
key_value
parameter.
Similarly to update_mutliple_fields, you use the FieldSetter to specify the different fields to
update. But instead of wrapping them in a list, wrap them in a dictionary, where the keys you define will be used as the
keys to construct the old_fields_values
object in which the old field's values will be returned. Pass this
dictionary to the setters
parameter.
No matter what, old_fields_values
will always be a dictionary containing as keys all the names of the
fields you tried to update.
Even if the operation failed, the dictionary will be returned with a False
value for each
field.
You can safely access the fields values with brackets instead of using the .get
function on your dictionary.