Hey when reading the guide, there is a section tha...
# guide
n
Hey when reading the guide, there is a section that talks about updating a dynamodb record with the following object, I seem to be getting the problem where If i don't include an attribute it actually gets updated to null rather than doing nothing as I would hope. Does anyone know how I would have this setup so that it only updates the attributes which I provide?
r
You can marshal the object before writing, using, using v3 of the dynamodb sdk there's a marshal option to remove empty attributes
n
Hmmm i am trying to use marshall from @aws-sdk/util-dynamodb is this the correct usage? Still seems to be the same problem. Or would that option go somewhere else?
r
Yeah, that's v3. The marshall option I think you need is
removeUndefinedValues: true
and I think you can remove the || clause
n
Still seem to get the same behaviour when adding that option 🤦‍♂️
Actually scratch that was some caching issue, should i not be using the document client when using marshall? Seems to store things in the table in dynamo attribute syntax rather than a plain string 🤔
r
We're using the DynamoDBClient
n
This one?
Also seems I can solve my problem by constructing the update string + update object. Just seems like I shouldn't have to do something like this...
r
No, that's the v2 one.
Copy code
import {
  DynamoDBClient,
  DynamoDBClientConfig,
  GetItemCommand,
  PutItemCommand,
  QueryCommand,
  DeleteItemCommand,
  QueryCommandInput,
} from '@aws-sdk/client-dynamodb';

import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
n
Seem to run into the issue for both document client & dynamo client that when I send a request with a missing attribute you just get the following error
r
Ah, I think you'll need to use the attribute_exists function "Comparison Operator and Function Reference - Amazon DynamoDB" https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html
n
Hey ross, i tried a while to get that working but didn't have any luck, it seems like that function is used to check if attributes exist in the dynamo record itself, whereas my main concern is about if the attribute exists in the TS object which I'm trying to use to update the record. Appreciate all your help with this, but in the end I'm still a bit new to dynamo and so am unsure of what i'm doing wrong with the other approach, have opted for just having a helper function to generate an
updateExpression
/
expressionAttributeValues
which will only update the property's listed in an update object.
r
That's very neat. I've seen similar done by others. I'll be honest, I wouldn't have reached for reduce myself, I'd probably have used a forEach or map and built the object but this makes me think I should be using reduce more 🙂