Closed davit-b closed 1 year ago
This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon. If you wish to exclude this issue from being marked as stale, add the "backlog" label.
Closing this issue as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen. If you wish to exclude this issue from being marked as stale, add the "backlog" label.
Hi Sam,
I want to present my use case, and how I've been using this library. It may be a dumb use case but I am a TS beginner.
I have these two types, here they are as TS native types, but I'm using scale-codec to build them.
and I instantiate my ddb client like so
I make my call to do an UpdateItem operation using
using the helper function
Because the input object is polymorphic, either
type: "user"
ortype: "admin"
, and has different fields, I'm trying to dynamically create theUpdateExpression
, and theExpressionAttributeNames
withExpressionAttributeValues
.When I tried to create the
UpdateExpression
dynamically, the type system was unable to recognizeExpressionAttributeNames
and gave me some type errors. I resorted to settingUpdateExpression
to a string literal, and TS seems to allow me to then set the AttributeNames and AttributeValues dynamically like above.However, when I read the object that is returned after executing the Update promise, and specified in ReturnValues, the type system does not recognize the fields that the intersection of the two tag-to-union types that define
Person
.In this case, it only recognizes
id
,age
,name
,type
, but does not recognizeoccupation
which is unique to User.and then you can see the Typescript error here specifically calls out
occupation
as not existing...but the error shows occupation as existing in the'SerializeObject<UndefinedToOptional<{ id: string; type: "user"; name: string; age: number; occupation: string; }>> | SerializeObject<UndefinedToOptional<{ id: string; type: "admin"; name: string; age: number; role: string; direct_report: string; }>>'
I don't understand this at all.but I have confirmed that the ReturnValues object .Attributes does contain the
occupation
field.What I've resorted to:
So I've concluded that I cannot use this tagged-to-union thing with the UpdateItem operation. I can only use the tag-to-union type with PutItem or with GetItem.
I guess I have to hard-code the update expression in my call to dynamodb. I assume this is best practice? I don't know.
The ask to the library owner.
Is there a better way to model these kinds of polymorphic types and generate dynamic UpdateExpressions that work with the type system and with typesafe-ddb?
Is the specific problem I have shown above about the type system not recognizing the Attributes inside the ReturnValues as problem with the way I have defined the Update command to dynamo?