tywalch/electrodb

Setting initial value for attribute AND performing ADD operation within single `update` call on an Entity object

aorumbayev opened this issue · 4 comments

Suppose there is the following update call in dynamo db client:

table.update_item(
    Key={
        'key': my_key
    },
    UpdateExpression="SET my_value = if_not_exists(my_value, :initial_value) + :increment",

    ExpressionAttributeValues={
        ':inc': initial_value,
        ':increment': 0,
    },
    ReturnValues="UPDATED_NEW"
)

If i were to translate that to ElectroDB syntax, it would not be possible to have both:

SomeEntity.update({ userID, assetID })
      .data((attr, op) => {
        op.ifNotExists(attr.my_value, initial_value);
        op.Add(attr.my_value, increment);
      }).go()

Within the same statement, during execution it will generate errors since attr.my_value will be referenced in UpdateExpression twice. The only way to resolve this at the moment seems to be to overwrite the UpdateStatement completely using the params object inside go method. But that seems rather hacky way to deal with that.

Hence my main question is, is there any other way to use electroDB syntax that will result in a desired UpdateExpression statement that sets initial value (if not exists) and then perform add?

Version of electrodb used

2.9.3

Hi @aorumbayev 👋

Thanks for reaching out! This is actually going to be included in the next update!

@tywalch amazing! Thanks for quick reply! What's the best way to track when that release is out?

I just linked this issue to the PR that it will get wrapped into

Merged this with 2.10.0, here is a playground showing it's use -- add and subtract now accept a third parameter allowing you to specify a defaultValue. Let me know if you have any issues!