aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
89 stars 79 forks source link

How to update row using increment either way set or add in amplify gen2? data client #2879

Closed jaimeardp closed 1 month ago

jaimeardp commented 1 month ago

Amplify CLI Version

10.8.1

Question

Environment information

System:
  OS: Windows 11 10.0.22631
  CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1255U
  Memory: 1.11 GB / 15.73 GB
Binaries:
  Node: 20.16.0 - C:\Program Files\nodejs\node.EXE
  Yarn: undefined - undefined
  npm: 10.8.1 - C:\Program Files\nodejs\npm.CMD
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/backend: 1.0.4
  @aws-amplify/backend-cli: 1.1.1
  aws-amplify: 6.4.0
  aws-cdk: 2.149.0
  aws-cdk-lib: 2.149.0
  typescript: 5.5.3
AWS environment variables:
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
  AWS_STS_REGIONAL_ENDPOINTS = regional

Actually, searching in documentation, I found a simple example of update, just only "replace" old value by new value.

import { generateClient } from 'aws-amplify/data';
import { type Schema } from '../amplify/data/resource';

const client = generateClient<Schema>();

const todo = {
  id: 'some_id',
  content: 'Updated content',
};

const { data: updatedTodo, errors } = await client.models.Todo.update(todo);

But my use case is a increment, because i need to update column like views or counter in post. I understand that i can first to use "client.models.Blog.get" and after to use "client.models.Blog.update". But then is needed to perfom both requests for to update one simple "Atomic counters" in DynamoDB ?

Please if you face use case, recommend me, thanks.

I hope this in amplify gen2 data client.

aws dynamodb update-item \
--table-name counter \
--key '{"pk":{"S":"abc123"}}' \
--update-expression "ADD quantity :change" \
--expression-attribute-values file://values.json

json file


{
    ":change" : { "N" : "-5" }
}
chrisbonifacio commented 1 month ago

Hi @jaimeardp in order to implement an atomic counter with Amplify Gen 2, I would recommend adding a custom mutation to your schema and execute the DynamoDB operation in an AppSync JS resolver.

Please refer to our docs on custom operations and let us know if you run into any issues along the way: https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/

chrisbonifacio commented 1 month ago

Hi 👋 Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.

Thank you!

github-actions[bot] commented 1 month ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.