0

I am using this library https://geofirestore.com/ to create a document in a collection if no other document already exists within the given coordinates. Now, should I use this library's method for transactions in the Cloud function or directly in the app?

I personally don't see a reason to do it in the Cloud, tho I might be wrong so I am asking for your opinion.

const geocollection = GeoFirestore.collection('chats');
const query = geocollection.limit(1).near({ center: new firebase.firestore.GeoPoint(latitude, longitude), radius: 0.03 });

GeoFirestore.runTransaction((transaction) => {
  const geotransaction = new GeoTransaction(transaction);
  return geotransaction.get(query).then((sfDoc) => {
    if (sfDoc.exists) {
      sfDoc.docs.forEach(documentSnapshot => {
        if (documentSnapshot.exists) {
          firestore().collection('chats')
            .doc(documentSnapshot.id)
            .get()
            .then((res) => {                  
                // insert new document
                geocollection.add({
                  name: 'Geofirestore',
                  uid: device_id,                     
                  coordinates: new firebase.firestore.GeoPoint(latitude, longitude),
                  created: currenttime
                })                  
            })
        }
      });
    } else {
      geocollection.add({
        name: 'Geofirestore',
        uid: device_id,           
        coordinates: new firebase.firestore.GeoPoint(latitude, longitude),
        created: currenttime
      })
    }
  });
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
showtime
  • 1
  • 1
  • 17
  • 48

1 Answers1

1

If you want the location of the document to be unique in its collection, consider using that location as the basis for the document ID, or some value derived from the location (such as a hash, or the geohash that Geofirestore uses).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • hmm, never thought about that. What if someone else wants to create a document within that radius , wont that create another unique hash? Or will the hash be the same? – showtime Dec 11 '21 at 23:37
  • The key should be the exact thing that you want to be unique. Geohashes can have an arbitrary precision so you could match to your requirements, but other schemes are also possible. The important thing is that IDs are pretty much the only way to guarantee uniqueness in the presence of potential bad actors. See for example https://stackoverflow.com/questions/47543251/firestore-unique-index-or-unique-constraint, https://stackoverflow.com/questions/47405774/cloud-firestore-enforcing-unique-user-names and more from https://www.google.com/search?q=%5Bgoogle-cloud-firestore%5D+unique+value – Frank van Puffelen Dec 11 '21 at 23:39
  • I am not sure if I got it right. So I can create a unique key for the given point radius, so that no one else can create a document within that location? – showtime Dec 11 '21 at 23:44
  • I think you might have misunderstood the question a little. I am using a radius of 30 meters there. So basically I dont want two users to create documents within that radius. One one user should be allowed to create a document within that 30 meters radius. Geohash will not be the same for both documents when they create them. Therefore I cant use them as constraints. – showtime Dec 11 '21 at 23:58