missatrox44 / server-rotation-crab-city

https://crab-city.vercel.app
MIT License
0 stars 0 forks source link

firebase function - clear database at midnight #33

Closed missatrox44 closed 1 year ago

missatrox44 commented 1 year ago

Steps

  1. Set up Firebase Cloud Functions: If you haven't already, you'll need to set up Firebase Cloud Functions in your Firebase project. You can refer to the Firebase documentation for guidance on getting started.

  2. Create a scheduled function: In your Cloud Functions code, you can create a scheduled function that triggers at midnight using a cron-like syntax. You can use a package like node-cron or firebase-functions to achieve this.

const functions = require('firebase-functions'); const admin = require('firebase-admin'); const cron = require('node-cron');

admin.initializeApp();

// Schedule a function to run at midnight in the Central Time Zone exports.deleteDatabaseAtMidnight = functions.pubsub .schedule('0 0 *') // Trigger at 00:00 UTC (midnight) .timeZone('America/Chicago') // Set the Central Time Zone .onRun(async (context) => { try { // Get a reference to the root of your Realtime Database const rootRef = admin.database().ref();

  // Delete the entire database
  await rootRef.remove();

  console.log('Database deleted successfully.');
} catch (error) {
  console.error('Error deleting the database:', error);
}

});

  1. Deploy the Cloud Function: Deploy the Cloud Function to your Firebase project using the Firebase CLI. Run the following command in your project's root directory:

firebase deploy --only functions

missatrox44 commented 1 year ago

need to pay to use firebase functions and im a cheapo!