dynamodb-toolbox/dynamodb-toolbox

Recent versions do not run onDefault if dependsOn contains undefined parameter

Closed this issue · 2 comments

I create global secondary indexes keys based on attributes given at creation time of my Entities.

For example I have a key layout like this:

G1PK: { 
type: "string", 
dependsOn: ["key1", "key2", "key3", "key4"], 
default: ({ key1, key2, key3, key4 }) => createAStringFunction(key1, key2, key3, key4 ) },

The createAStringFunction always returns a string, and the function expects one or more of the key to either contain a value or to be undefined/null.
In v0.9.2 this G1PK would be kicked off and the string would be inserted at creation time. However, when I run the same code in v0.9.5 (and 0.9.4) the default function is not triggered.

I found out that the difference is that one of the keys (e.g. key4) is undefined. Previously that was not a problem, but with the new version it seems the default function is not triggered. I would not expect this.

In the 0.9.5 changelog it was noted that the outcome of the default function is ignored if it's undefined.
I would expect that the default function to run if one of the inputs is undefined.

Hey @Jimmy89,
in this case I think that dependsOn is redundant, no?
it's a field that you want to only write on creation and not during updates, right? 🙏

Regarding changing the dependsOn logic to allow checking if at least one property changes also makes sense to me but need to give this more thought.
Probably worth creating additional configuration for that as I wouldn't want to accidentally cause cases where some keys would be malformed (e.g. POSTED-AT_undefined#USER_12345).

Hello @naorpeled
Yes, you're right about the assumption that I only want to write at creation time (the default value). I do not use dependsOn to check for changed property (as for that you could use onUpdate: true and pass all properties to calculate the default), but I do use it as on the keys (e.g. key4) also uses a function to calculate the default based on another property.
I evaluated my code and found out that dependsOn only needs to be used for properties that have a default value generated, in which case I can shorten the dependsOn graph. Thank you for that insight.