Closed emmanuelgeoffray closed 2 years ago
Good news! This feature is already built in. I just haven't updated the documentation, yet. There are multiple syntaxes already supported.
save(prop)
save([prop1, prop2])
save({ key: value })
I'm going to leave this open until I rewrite the docs.
This is good news ! It makes it very convenient and this is the kind of features that makes it very easy to use feathers-vuex and feather-spinia.
But I've tried save(prop)
again and it patched the object with all props.
Do you have a test for this ?
I believe so. You can check here: https://github.com/marshallswain/feathers-pinia/blob/main/tests/handle-clones.test.ts
The array syntax is being tested, here: https://github.com/marshallswain/feathers-pinia/blob/main/tests/handle-clones.test.ts#L45
This is now explicitly documented with more clarity, here: https://feathers-pinia.pages.dev/guide/use-clones.html#automatic-patch-diffing
It would be nice to have this on modelInstance.save
too !
Correct me if I'm wrong but right now this works only with save_user
, not modelInstance.save
.
The diffing feature is unique to useClones; however, you can already pass an object to params.data when calling instance.patch. That object will be used for the request data.
I do like the idea of enabling a diffing feature on patch, though, using the same logic as in useClones (meaning diff deep and send top-level keys)
I actuallly used to diff in feathers-vuex but I ended up removing the feature because it was pretty slow on Vuex-stored data. With Vue 3 it would be fast. Since the first argument to patch is the params object, we have to put it in a key of params I think.
Instance.patch({ diff: key | key[] | object})
It would also work for instance.save, since that just passes the params to patch
Actually, maybe we can use params.data with that’s same API. Also, this diffing algorithm is smart enough that with a small tweak it could be enabled by default on all patch requests that include params.data. It could be disabled by passing params.diff = false. I like where this is heading.
Actually we can have two algorithms:
Nah. Just the first/current algorithm. The saveWith API can be used to make sure other keys are always passed if needed.
The diff
property in params for patch and save seems to be the right implementation.
Easy to understand and use !
For MongoDB, making a request with dotted syntax could actually work for only sending nested data:
await save_user(['name', 'profile.photo'])
// Sends out this
{
'name': 'foo',
'profile.photo': 'url'
}
As the
save_handler
, it would be nice to have asave('name')
option to send aPATCH
with an object containing only the mentioned property.