Reputation: 1472
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
})
}
});
});
Upvotes: 0
Views: 215
Reputation: 599766
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).
Upvotes: 1