capacitor-community / contacts

Contacts Plugin for Capacitor
https://capacitor-community.github.io/contacts/
119 stars 52 forks source link

RFC: support for updating a contact #86

Open tafelnl opened 1 year ago

tafelnl commented 1 year ago

Updating a contact may be considered a fairly basic feature that should probably be implemented. However, it is not as straight forward as one might think.


If you are in need of this functionality, please +1 this post by means of a GitHub reaction (so not with a comment).


Approach 1

The easiest approach would be to implement a PUT instead of PATCH (REST-API analogy). That means that the contact will get completely overwritten with the newly defined values. Essentially, this would be the same as removing the contact and creating a new one.

This is quite easy to implement, but the developer experience, and (maybe moreover) the user experience is sub-optimal to say the least.

If the developer only wants to update a single contact's phone number, he would still have to retrieve all of that user's data, and consequently put all that data into the updateContact method. Not very ideal.

If the developer forgets to do above said things, a user might end up with a contact with only the newly updated phone number, with all "old" data deleted. Not very user friendly.


Approach 2

It would be possible to do a "partial PUT". This would entail that the developer does not have to supply all data, but just the data for the object he is updating. So if, for example, the developer wants to update a contact's phone number, he will only have to supply all the other phone numbers that are supposed to be kept.

This approach has a better developer experience than approach # 1, but it's still not very ideal.


Approach 3

Arguably the best approach is this: make it possible to retrieve the IDs of a contact's phone, email, postal address etc. This way a developer can just specify which object (phone, email, postal address) object should be updated, by simply providing the correct id.

Still this has some quirks that should be thought about. For example:

The developer wants to updated the postal address with id 123. But he only wants to update the street, not all the other fields... What should be done now? Does the developer have to specify all the other "old" fields, so we can overwrite the whole object. Or does the API only update the given fields. But in the latter case, how would it be possible to delete / empty fields?

Of course, this approach, will also take the longest to develop and test.


All in all, still some questions. Unfortunately, I don't have the time, nor the need, to implement this. But of course, suggestions, comments, PRs, or any input for that matter, is very welcome.

jameshhood commented 1 year ago

I'm not sure about the underlying code but since most of the data is already present when you get a contact; I'm sure we could just update what we need and approach 1 would at least get us around having to store, delete, and create the entire contact again. Not sure if that would even work if there are notes and other things associated with it.

I'm currently making a support contact in my app and for now I only need a single number but I would like to be able to manage that number and potentially add more numbers to that contact based on certain departments.

tafelnl commented 1 year ago

However because of the drawbacks mentioned, I'm not in favor of using that approach. Moreover it would be kind of misleading to a developer and one might not be aware of the caveats. And essentially this approach is already possible by using the current API, by means of just getting, deleting and creating a contact.