P5JS-Google-Firebase-Realtime-Database

From Digipool-Wiki
Jump to: navigation, search

// Configuration of Google Realtime Database at Firebase
// Worked on January 4, 2024.
//
// Google Firebase Spark Plan (No-cost)
// Simultaneous connections: 100
// GB stored: 1 GB 
// GB downloaded: 10 GB/month
// One databases per project
//
/*
 Useful Commands:
 push(parent, value) =	Generates a new child location using a unique key
 set(ref, value) = Writes/Overwrite data to this Database location.
 val() = Read data from the Database
*/
/*
Include these two lines in your HTML header:
<script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-database.js"></script>
*/

var firebaseConfig = {
  apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  authDomain: "xxxxxxxx.firebaseapp.com",
  databaseURL: "https://xxxxxxxxxxxxxxxxx.firebasedatabase.app",
  projectId: "xxxxxxxx",
  storageBucket: "xxxxxxxx.appspot.com",
  messagingSenderId: "xxxxxxxx",
  appId: "x:xxxxxxx:web:xxxxxxxxx",
  measurementId: "xxxxxxxxxx"
};

// Firebase initialisieren
firebase.initializeApp(firebaseConfig);

// Datenreferenz
var database = firebase.database();

// Daten, die gespeichert werden sollen
var data = {
  name: "Max Mustermann",
  email: "max.mustermann@example.com",
  age: 30,
};

// Abspeichern der Daten in Firebase
database.ref("users").push(data, function (error) {
  if (error) {
    console.log("Fehler beim Speichern der Daten: " + error);
  } else {
    console.log("Daten erfolgreich gespeichert.");
  }
});