Skip to main content

Firebase

Firebase is an app development platform that helps you build and grow apps and games users love.

Credential configuration

The form fields are the fields of an object provided by Firebase, to connect by your app. So, first of all you need to create a project in firebase, and then you need to create an app in this project. If you want to find more info about creating a project and an app, you may find more information here.

Once you have the app, you will see it on the project settings section, in General tab. Click on the app you want to use and you'll be able to see a section called SDK setup and configuration, where you can find firebaseConfig object. Fill the form fields with this object values.

In the extra options field you can pass most of the params you can find here.

Here you have an example of a filled credential configuration form in YepCode:

Firebase snippets available in editor

note

The title is the triggering text for YepCode to autocomplete the script

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);
danger

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"));