Here is an example usage of Firebase 10 API inside Electronjs environment.
Here we open the get_collection function to the renderer process of the electronjs through preload script.
let window = new BrowserWindow({
width: 1366,
height: 768,
webPreferences: {
nodeIntegration: true,
worldSafeExecuteJavascript: true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
const { initializeApp } = require('firebase/app');
const { getFirestore, collection, getDocs } = require('firebase/firestore/lite');
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "ddsggfg-sadasda",
authDomain: "dhdfgdg.firebaseapp.com",
projectId: "adasdasd",
storageBucket: "dhdfgdg.appspot.com",
messagingSenderId: "104005267328",
appId: "1:104005267328:web:4gr1633155d15t8b13d21d"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
contextBridge.exposeInMainWorld('firebase', {
firestore: {
get_collection: async (name) =>
{
try{
const selected_collection = collection(db, name);
const collection_snapshot = await getDocs(selected_collection);
const collection_data = collection_snapshot.docs.map(doc => doc.data());
console.log("Collection data:", collection_data);
return collection_data;
}
catch(error)
{
console.error('Error fetching collection:', error);
throw error;
}
}
},
})
window.firebase.firestore.get_collection('chats');