Ross
06/12/2022, 3:23 PM.database.ref(`tasks/{itemId}`)
.onWrite(async (change, context) => {
/**
* We use a Firestore transaction to create a "lock" at a DB location
* for a given `itemId`
*/
const { timestamp, eventId } = context;
const { itemId } = context.params;
const timestampRef = firestore
.collection(`typesenseLocks_tasks`)
.doc(itemId);
await admin.firestore().runTransaction(async transaction => {
const dataChangedTimestamp = new Date(timestamp).getTime();
const lastUpdatedTimestampDoc = await transaction.get(timestampRef);
const lastUpdatedData = lastUpdatedTimestampDoc.data();
/**
* If this is the first time this document was changed (no previous locks),
* or the last-stored lock timestamp is older than the current event's timestamp,
* prepare a payload and send to Typesense.
*/
if (
(!lastUpdatedData?.timestamp ||
dataChangedTimestamp > lastUpdatedData.timestamp) &&
lastUpdatedData?.eventId !== eventId
) {
// Send to Typesense
await updateTypesense(change, indexer, itemId);
// Finalize Transaction
transaction.set(timestampRef, {
timestamp: dataChangedTimestamp,
eventId
});
} else {
/**
* Do nothing, current event is older than last-indexed event already recorded, can be safely discarded
*/
}
});
Ross
06/12/2022, 9:57 PMKishore Nallan
06/13/2022, 4:44 AMJason Bosco
06/13/2022, 7:58 PMFirestore event triggers are not guaranteed to happen in the order the DB changes didThis is surprising to hear! Do you know if this documented in the Firebase docs by any chance?
Ross
06/13/2022, 8:00 PMRoss
06/13/2022, 8:00 PMRoss
06/13/2022, 8:01 PMJason Bosco
06/13/2022, 8:01 PMRoss
06/13/2022, 8:01 PMRoss
06/13/2022, 8:02 PMRoss
06/13/2022, 8:03 PMJason Bosco
06/13/2022, 8:03 PMJason Bosco
06/13/2022, 8:03 PMinstead of "send to typesense, and use the data from this event"Ah
Ross
06/13/2022, 8:03 PMRoss
06/13/2022, 8:04 PMRoss
06/13/2022, 8:05 PMRoss
06/13/2022, 8:07 PMfor (let i = 0; i < 300; i++) {
promises.push(ref.child("tasksOpen").child(taskId).child("name").set(`A ${i}`));
console.log("Set name to", i);
}
await Promise.all(promises);
Jason Bosco
06/13/2022, 8:07 PMRoss
06/13/2022, 8:08 PMRoss
06/13/2022, 8:08 PMRoss
06/13/2022, 8:09 PMRoss
06/14/2022, 12:27 AMJason Bosco
06/14/2022, 12:55 AMhttps://typesense-community.slack.com/archives/C01P749MET0/p1655150586446689?thread_ts=1655047418.496499&cid=C01P749MET0Following up on this idea ^ What if in the function:
await updateTypesense(change, indexer, itemId);
instead of using the change
object provided by Firestore, we query Firestore for the latest version of that document by ID and insert that into Typesense?Ross
06/14/2022, 12:56 AMRoss
06/14/2022, 12:57 AMRoss
06/14/2022, 12:57 AMJason Bosco
06/14/2022, 12:58 AMRoss
06/14/2022, 12:58 AMRoss
06/14/2022, 12:59 AMRoss
06/14/2022, 12:59 AMJason Bosco
06/14/2022, 1:00 AMRoss
06/14/2022, 1:01 AMRoss
06/14/2022, 1:01 AMJason Bosco
06/14/2022, 1:03 AMRoss
06/14/2022, 1:04 AMRoss
06/14/2022, 1:04 AMJason Bosco
06/14/2022, 1:06 AMRoss
06/14/2022, 1:07 AMJason Bosco
06/14/2022, 1:09 AMJason Bosco
06/14/2022, 1:10 AM