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

Create a cloud function to resync Firestore and MySQL #190

Open
dev-mansonthomas opened this issue Jun 3, 2023 · 0 comments
Open

Create a cloud function to resync Firestore and MySQL #190

dev-mansonthomas opened this issue Jun 3, 2023 · 0 comments

Comments

@dev-mansonthomas
Copy link
Owner

Turn this function, that generates SQL into a cloud function

async function run() {
  try {
    const batchSize = 1000; // Define the batch size for each page
    let nextPageToken = undefined; // Token to fetch the next page of users

    const queryPromises = [];

    while (true) {
      const result = await admin.auth().listUsers(batchSize, nextPageToken);
      const users = result.users;

      const batchPromises = users.map(async (user) => {
        const queteurDoc = await db.collection("queteurs").doc(user.uid).get();
        const queteurData = queteurDoc.data();
        if (queteurData) {
          const providerId = user.providerData.length > 0 ? user.providerData[0].providerId : null;
          const queteur_id = queteurData['queteur_id'];
          const ul_id = queteurData['ul_id'];
          const email = queteurData['email'];
          const first_name = queteurData['first_name'];
          const last_name = queteurData['last_name'];

          return `UPDATE queteur q SET q.firebase_uid='${user.uid}', q.firebase_sign_in_provider='${providerId}' WHERE q.id = ${queteur_id} AND q.ul_id = ${ul_id}; -- ${first_name} ${last_name}`;
        }

        return null;
      });

      queryPromises.push(...batchPromises);

      nextPageToken = result.pageToken; // Set the token for the next page

      if (!nextPageToken) {
        break; // Exit the loop if there are no more pages
      }
    }

    const results = await Promise.all(queryPromises);
    results.forEach((query) => {
      if (query !== null) {
        console.log(query);
      }
    });
  } catch (error) {
    console.log("Error retrieving data:", error);
  }
}

run().catch((error) => {
  console.log("Error:", error);
});

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

No branches or pull requests

1 participant