aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

[Request] DataStore models should have a sync status field #6534

Open swyxio opened 4 years ago

swyxio commented 4 years ago

Is your feature request related to a problem? Please describe.

it's a very common need to want to indicate the sync status of datastore items. i understand its a distsys issue. but we need to offer some sort of documentation or blessed path for people to show whether something has been "synced to cloud" or is really "local only and can be lost" eg during a model migration.

we seem to have this process here that quite honestly i dont understand (what am i executing "against the remote state"?): https://docs.amplify.aws/lib/datastore/sync/q/platform/js#update-and-delete-with-predicate

Either we do it for people and warn of edge cases, or they do it themselves and do it badly.

Describe the solution you'd like

check a field or run a method. MYMODELINSTANCE.isSynced() === true or false

Describe alternatives you've considered

firebase uses a CompletionListener callback https://stackoverflow.com/a/48565275/1106414

Additional context

Jameson says:

The trouble is - which version has been synced to the cloud? It is, at its core, the classic cache coherency problem. Even to answer “is it still on the server (not deleted),” you’d have to go ask the server, to be sure. But, for the purposes of the UI: when you save to DataStore, mark an ID in a Set. Then, listen to Hub, for an event that lets you know it has been published. When it is, remove from set, and update UI from Set. This will be right most of the time, assuming a 1 client 1 server relationship for a given ID.

Amplify.Hub.subscribe(
    HubChannel.DATASTORE,
    event -> {
        return DataStoreChannelEventName.PUBLISHED_TO_CLOUD.toString().equals(event.getName());
    },
    event -> {
        ModelWithMetadata<?> mwm = (ModelWithMetadata<?>) event.getData();
        set.put(mwm.getModelId());
    invalidateModel(set);
    }
);
swyxio commented 4 years ago

this exists in Hub but is not yet documented https://github.com/aws-amplify/amplify-js/issues/4808#issuecomment-640574885

nubpro commented 4 years ago

In my opinion, I think DataStore.observe should indicate whether the record is a REMOTE or LOCAL. I've feedback this suggestion to @manueliglesias under #6322 and I believe he's working towards the same goal.

Yeah, could we get the Hub listener for DataStore documented please :)

mauerbac commented 4 years ago

Confirmed docs have been added -> https://docs.amplify.aws/lib/datastore/datastore-events/q/platform/js

swyxio commented 4 years ago

@mauerbac the docs are a stopgap - this is a feature request for a first class api that doesnt involve Hub.

nubpro commented 4 years ago

Is this feature request dead in the water?

swyxio commented 4 years ago

imo, no - @mauerbac do you mind reopening this? the documentation was only one small part. the developer experience is still not good enough if we are asking people to listen to Hub for such a basic item. i think you and i are agreed on that, remains to be seen if the maintainers agree.

swyxio commented 4 years ago

we touched upon this in today's Amplify With Friends livestream. usecase from user:

The use case: On facebook, when you're offline and u try to post smth, it will indicate it will be posted later

image

meducati commented 4 years ago

Late to the party, but was just searching the docs for exactly this feature. Hope we can get this implemented.

Use case: My client want to go offline for long periods of time on his tablet. He will stay signed in but open/close the (web-)app frequently. When he eventually logs out, it would be great with an easy request to the local Datastore to make sure that there are no pending items that are waiting to try synchronization to DynamoDB.