marshallswain / feathers-pinia

Connect your Feathers API to the elegant data store for Vue
53 stars 22 forks source link

save('name') feature #55

Closed emmanuelgeoffray closed 2 years ago

emmanuelgeoffray commented 2 years ago

As the save_handler, it would be nice to have a save('name') option to send a PATCH with an object containing only the mentioned property.

marshallswain commented 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.

marshallswain commented 2 years ago

I'm going to leave this open until I rewrite the docs.

emmanuelgeoffray commented 2 years ago

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 ?

marshallswain commented 2 years ago

I believe so. You can check here: https://github.com/marshallswain/feathers-pinia/blob/main/tests/handle-clones.test.ts

marshallswain commented 2 years ago

The array syntax is being tested, here: https://github.com/marshallswain/feathers-pinia/blob/main/tests/handle-clones.test.ts#L45

marshallswain commented 2 years ago

This is now explicitly documented with more clarity, here: https://feathers-pinia.pages.dev/guide/use-clones.html#automatic-patch-diffing

emmanuelgeoffray commented 2 years ago

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.

marshallswain commented 2 years ago

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)

marshallswain commented 2 years ago

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})

marshallswain commented 2 years ago

It would also work for instance.save, since that just passes the params to patch

marshallswain commented 2 years ago

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.

marshallswain commented 2 years ago

Actually we can have two algorithms:

marshallswain commented 2 years ago

Nah. Just the first/current algorithm. The saveWith API can be used to make sure other keys are always passed if needed.

emmanuelgeoffray commented 2 years ago

The diff property in params for patch and save seems to be the right implementation. Easy to understand and use !

marshallswain commented 2 years ago

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'
}