snippets_js
Integration
New integration from credential
const firebase = yepcode.integration.firebase("credential-slug");
New integration from plain authentication data
const { initializeApp } = require("firebase/app");
const firebaseConfig = {
apiKey: "firebase-app-apiKey",
authDomain: "firebase-app-authDomain",
projectId: "firebase-app-projectId",
storageBucket: "firebase-app-storageBucket",
messagingSenderId: "firebase-app-messagingSenderId",
appId: "firebase-app-appId"
};
const firebase = initializeApp(firebaseConfig);
warning
When you init the credential, it creates some background resources. So you should free it at the end of your processes. Otherwise, they will not end until you kill it! Adding this code will free the resources and make the process end in a normal way:
const { deleteApp } = require("firebase/app");
deleteApp(firebase);
Insert Item into Realtime Database
Insert item into Realtime database
const { getDatabase, ref, set } = require("firebase/database");
const db = getDatabase(firebase);
await set(ref(db, `path-to-the-object`), {
// Object content
});
Get Item from Realtime Database
Get item from Realtime database
const { getDatabase, ref, get, child } = require("firebase/database");
const db = getDatabase(firebase);
const dbRef = ref(db);
get(child(dbRef, `path-to-the-object`)).then((snapshot) => {
if (snapshot.exists()) {
console.log(snapshot.val());
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
Insert Item into Firestore
Insert item into firestore
const { getFirestore, collection, addDoc } = require("firebase/firestore");
const db = getFirestore(firebase);
await addDoc(collection(db, "collection-name"), {
// The object data
}).then(console.log).catch(console.error);
List Items from Firestore
List items from firestore
const { getFirestore, collection, getDocs } = require("firebase/firestore");
const db = getFirestore(firebase);
const querySnapshot = await getDocs(collection(db, "collection-name"));
querySnapshot.forEach((doc) => {
console.log(doc.id);
console.log(doc.data());
});
Delete Item from Firestore
Delete item from firestore
const { getFirestore, deleteDoc, doc } = require("firebase/firestore");
const db = getFirestore(firebase);
await deleteDoc(doc(db, "collection-name", "document-id"));