A firebase functionfirebase functionCloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. that is triggered whenever a note is created or modified. This function then updates the note within supabase.

  1. With the updated note data from the firebase trigger, a query is made to firebase for the same note within supabase
  2. If the note from firebase was modified_at later than the note from supabase or if it is not a new note, then we skip the following steps.
  3. Otherwise, upsert the updated note into firebase from supabase.
exports.firebase_notes_to_supabase = functions.firestore.document('notes/{note_id}').onWrite(async (change, context) => {
  const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY);
  const note_id = context.params.note_id;
  const note =;
  const fireNote = firebaseNoteToSupabase(note_id, note);

  // Step 1
  const { data: supaNote } = await supabase
    .match({id: note_id})

  // Step 2
  if (!supaNote || new Date(supaNote.modified_at) < new Date(fireNote.modified_at)) {
    // Step 3
    const { data, error } = await supabase
    if (error) {
      console.error({ note_id, error });
      return false;
    console.log({ note_id, data });
    return true;
  return false