Estoy intentando migrar mis Lambdas v2 a v3 y tengo algunos problemas para incrementar un valor. Aquí está mi método v2:
const params = { TableName: process.env.USER_TABLE_NAME!, Key: { UID }, UpdateExpression: 'SET #numRatings = #numRatings + :increment', ExpressionAttributeNames: { '#numRatings': 'numRatings' }, ExpressionAttributeValues: { ':increment': 1 }, ReturnValues: 'ALL_NEW' }; return await DB.update(params).promise();método v3:
const params = { TableName: process.env.USER_TABLE_NAME!, Key: { UID }, UpdateExpression: 'set numRatings = numRatings + :increment', ExpressionAttributeValues: { ':increment': 1 }, ReturnValues: 'ALL_NEW' } as unknown as UpdateItemCommandInput; try { const response = await dbClient.send(new UpdateItemCommand(params)); console.log('response', response); } catch (error) { console.error('error', error); }Estoy usando TypeScript y VS Code da este error:
Types of property 'Key' are incompatible. Type '{ UID: string; }' is not comparable to type '{ [key: string]: AttributeValue; }'. Property 'UID' is incompatible with index signature. Type 'string' is not comparable to type 'AttributeValue'CloudWatch da este error inútil:
No se puede leer la propiedad '0' de indefinido
Estoy realmente confundido ya que, por lo que puedo decir, he seguido la documentación correctamente, su equivalente de "parámetros" se define así:
export const params = { TableName: "TABLE_NAME", Key: { primaryKey: "VALUE_1", // For example, 'Season': 2. sortKey: "VALUE_2", // For example, 'Episode': 1; (only required if table has sort key). }, // Define expressions for the new or updated attributes UpdateExpression: "set ATTRIBUTE_NAME_1 = :t, ATTRIBUTE_NAME_2 = :s", // For example, "'set Title = :t, Subtitle = :s'" ExpressionAttributeValues: { ":t": "NEW_ATTRIBUTE_VALUE_1", // For example ':t' : 'NEW_TITLE' ":s": "NEW_ATTRIBUTE_VALUE_2", // For example ':s' : 'NEW_SUBTITLE' }, ReturnValues: "ALL_NEW" };Mis disculpas si son demasiados ejemplos de código, pero quería ser exhaustivo. UID es una cadena. Gracias de antemano por cualquier ayuda
Las claves y ExpressionAttributeValues necesitan su tipo de Dynamo definido de la siguiente manera:
... Key: { UID: { S: UID } }, ... ExpressionAttributeValues: { ':val': { N: '1' } },