capacitor-firebase icon indicating copy to clipboard operation
capacitor-firebase copied to clipboard

feat(firestore): collection snapshots doc changes type

Open jongbonga opened this issue 2 years ago • 0 comments

Plugin(s)

  • [ ] Analytics
  • [ ] App
  • [ ] App Check
  • [ ] Authentication
  • [ ] Crashlytics
  • [X] Cloud Firestore
  • [ ] Cloud Messaging
  • [ ] Performance
  • [ ] Remote Config

Current problem

Currently, the CollectionSnapshotListener returns all the doc changes, as it should, but there is no built-in way to determine what update was performed (added, modified, removed) like the JS SDK exposes here.

Preferred solution

Attach docChanges to the snapshots to view which action was performed on each documents

const addCollectionSnapshotListener = async () => {
  const callbackId = await FirebaseFirestore.addCollectionSnapshotListener(
    {
      reference: 'users',
      compositeFilter: {},
      queryConstraints: []
    },
    (event, error) => {
      if (error) {
        console.error(error);
      } else {
        event.snapshots.docChanges.forEach(change => {
          if (change.type === 'added') {
            console.log('New city: ', change.doc.data());
          }
          if (change.type === 'modified') {
            console.log('Modified city: ', change.doc.data());
          }
          if (change.type === 'removed') {
            console.log('Removed city: ', change.doc.data());
          }
        });
      }
    }
  );
  return callbackId;
};

Alternative options

Return only the document which was updated and what change was performed on it

Additional context

I'd like to know which action was performed on which document instead of doing complicated loops to detected the change

jongbonga avatar Oct 17 '23 19:10 jongbonga