Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Firestore replication ignoring initialCheckpoint #6850

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

calvinturbo
Copy link
Contributor

@calvinturbo calvinturbo commented Feb 12, 2025

This PR contains:

  • a bugfix for the Firestore replication ignoring the initialCheckpoint variable, causing a new replication to replicate all documents

Describe the problem you have without this PR

  • Firestore replication useless if the collection you wanna replicate has many documents

Todos

Example how to 'build' the initialCheckpoint variable, something you could put in the docs:

 // 1. Query Firestore for the "boundary" document.
    let initialCheckpoint: any = null;
    try {
      const q = query(
          collection(this.firestore, 'collection'), // Your Firestore collection
          where('serverTimestamp', '>=', twoMonthsAgo), // Filter: serverTimestamp>= 2 months ago
          orderBy('serverTimestamp', 'asc'), // Order by createdAt ascending
          limit(1) // Get only the first (oldest) matching document
      );
      const querySnapshot = await getDocs(q);

      if (!querySnapshot.empty) {
        // 2. Construct the initialCheckpoint.
        const docData = querySnapshot.docs[0].data();
        initialCheckpoint = {
          id: querySnapshot.docs[0].id,  // Document ID
          serverTimestamp: docData['serverTimestamp'].toDate().toISOString(), // Server timestamp
        };
        console.log("Calculated initialCheckpoint:", initialCheckpoint);
      } else {
        // No documents within the last two months.  Handle as needed.
        // Option 1: Start with no checkpoint (sync all)
        // initialCheckpoint = null;
        // Option 2:  Don't start replication, show a message, etc.
        console.log("No documents found within the last two months.");
        return; // Or handle appropriately
      }


    } catch (error) {
      console.error("Error querying Firestore:", error);
      return; // Handle the error appropriately
    }

Line was missing from replication-firestore file
@pubkey
Copy link
Owner

pubkey commented Feb 12, 2025

Thank you for the fix. Can you add a test for this?

@pubkey pubkey merged commit 4abf3e4 into pubkey:master Feb 14, 2025
20 checks passed
pubkey added a commit that referenced this pull request Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants