awslabs / dynamodb-data-mapper-js

A schema-based data mapper for Amazon DynamoDB.
https://awslabs.github.io/dynamodb-data-mapper-js/
Apache License 2.0
817 stars 106 forks source link

[feature request] use custom expression on update operation #174

Open haruharuharuby opened 4 years ago

haruharuharuby commented 4 years ago

Description: Currently update operation only accept annotated model. I'd like to request that Custom expression can be specified on update operation.

Abstract example: This example, some attribute increments by atomic counter.

const ownModel = new OwnModel()
ownModel.key = 'xxxx'
const expr = new MathematicalExpression('attribute_name', '+', '1')
datMapper.update<model.OwnModel>(
  ownModel,
  { customExpression: expr }
)
jeskew commented 4 years ago

Hi @haruharuharuby,

There's an executeUpdateExpression method to support this type of update.

haruharuharuby commented 4 years ago

@jeskew okay. thanks. I could not find this document.

kpenergy commented 3 years ago

Can anyone help with an example of how to use the executeUpdateExpression method to achieve the functionality mentioned in the description please?

Here's what I've cobbled together so far from the docs...

My table, called my-table, contains the following entry to be updated:

{
  "MyHashKey": {  // This is my hash/primary key
    "S": "my-hash-key-value"
  },
  "MyRangeKey": { // This is my sort/range key
    "S": "my-range-key-value"
  },
  "SomeValue": { // this is the value to be updated
    "N": "0"
  }
}

My code that attempts to execute an update expression

@table("my-table")
export class MyModel {
    @hashKey()
    MyHashKey!: string;

    @rangeKey()
    MyRangeKey!: string;

    @attribute()
    SomeValue!: number;
}

const updateExpr = new UpdateExpression();
updateExpr.set("SomeValue", new MathematicalExpression("SomeValue", '+', 1));

const key = {
    "MyHashKey": { S: "my-hash-key-value" } ,
    "MyRangeKey": { S: "my-range-key-value" }
};

await mapper.executeUpdateExpression(updateExpr, key, MyModel);

However this seems to be throwing the following exception:

ValidationException: The provided expression refers to an attribute that does not exist in the item

Any help getting a working example would be appreciated.

hariharasudhan-nineleaps commented 2 years ago

Did you find the solution to this issue?

jeskew commented 2 years ago

There is an example of how to use executeUpdateExpression in the tests.

hariharasudhan-nineleaps commented 2 years ago

Does set allows us increase count of an existing attribute only, in my code someValue attribute is not exists before so i am getting error like this

No item with the key found in table