patch nested object
how can I write a mutation to patch
value
without having to input all the fields?
for example
12 Replies
We used to allow this but the behavior was too surprising. The way to do this (which won’t be any slower btw) is to do a db.get, then set the name, then do a db.replace
would it be a good idea if i change the schema to
?
why would i need
db.replace
instead of spreading the old value
and updating it with patch
?Spreading and using patch also works
patch(itemId, {value: {...oldValue, name:newName,}))
Yup that works too
what about
value: v.id("itemValue")
?
that would be patchable without get
would that be inconvenient in some way?You mean store the value in another table and just patch the id? That also works, you’d just need to do a db.get when you need to fetch it. For small bits of data with a 1:1 mapping I like to just nest the state but anything’s possible
ok i see, thanks
i'm getting blocked because my schema is using unions of differente item types and each item type has a different value schema
so when i try to patch or replace it says that the argument of type tag: "PvcWindow"; value {... } is not assignable to parameter of type value (of Complmento)
attempt
Is it possible you’re spreading the “old”
value
that has fields that don’t belong with the new tag?
You could destructure oldItem.value to get only the fields that should be carried over to the replaced/patched value
.yeah seems like there is a disconnect with the fields. The destructuring doesn't allow 3 fields that have unions themselves
is there a special way to declare a variable that is a literal? i'm just doing a string
The destructuring doesn't allow 3 fields that have unions themselvesIf these are TypeScript errors hopefully the typechecker is correct and indeed some variants of the union might not have those fields. Are you expecting oldItem to have the same tag PvcWindow? If yes you could assert that (via an if statement for example) and TypeScript should narrow the type of oldItem.
is there a special way to declare a variable that is a literal? i'm just doing a stringThere should be nothing special about literals and variable declaration.
the
if
got rid of the TS error. the patch worked, thanks