GoogleRealtimeDB Advanced
GoogleRealtimeDB Advanced
usersRef.set({
user1: {
username: 'user1',
age: 25
},
user2: {
username: 'user2',
age: 30
}
});
postsRef.push({
title: 'Post Title',
body: 'Post content goes here',
userId: 'user1'
});
function loadPage() {
let query = itemsRef.orderByKey().limitToFirst(pageSize);
if (lastKey) {
query = itemsRef.orderByKey().startAt(lastKey).limitToFirst(pageSize + 1);
}
query.once('value').then(snapshot => {
let items = [];
snapshot.forEach(childSnapshot => {
items.push(childSnapshot.val());
lastKey = childSnapshot.key;
});
// Use items for rendering
});
}
loadPage();
query.once('value').then(snapshot => {
let items = [];
let isNextPage = false;
snapshot.forEach(childSnapshot => {
if (isNextPage) {
items.push(childSnapshot.val());
}
if (childSnapshot.key === lastKey) {
isNextPage = true;
}
});
lastKey = snapshot.val() ? snapshot.val().key : null;
// Use items for rendering
});
}
Previous Page Load
function loadPreviousPage() {
let query = itemsRef.orderByKey().endAt(lastKey).limitToLast(pageSize);
query.once('value').then(snapshot => {
let items = [];
snapshot.forEach(childSnapshot => {
items.unshift(childSnapshot.val()); // Add to the beginning of the array
lastKey = childSnapshot.key;
});
// Use items for rendering
});
}
advertisement
Implementing Complex Queries with .once() and .on() Tutorial
Implementing complex queries in Firebase Realtime Database allows developers to efficiently retrieve
and manipulate data in a structured manner. By utilizing methods like .once() and .on(), developers can
listen for changes in data and respond accordingly. The .once() method retrieves data at a specific
reference point a single time, while .on() sets up a real-time listener that continuously updates as data
changes. These methods can be used in conjunction with query parameters to filter results based on
specified criteria.
Copy
Fetching User Data Once
firebase.database().ref('users').orderByChild('age').equalTo(25).once('value').then((snapsho
t) => { const data = snapshot.val(); console.log(data); });
Listening for Real-time Updates on Products
firebase.database().ref('products').on('value', (snapshot) => { const products =
snapshot.val(); console.log(products); });
profilesRef.child(userId).once('value').then(profileSnapshot => {
const profile = profileSnapshot.val();
ordersRef.orderByChild('userId').equalTo(userId).once('value').then(ordersSnapshot => {
const orders = ordersSnapshot.val();
const mergedData = { profile, orders };
console.log(mergedData);
});
});
function updateUI() {
console.log(combinedData);
// Update your UI with combinedData here
}
exports.onDataAdded = functions.database.ref('/path/to/data/{pushId}').onCreate((snapshot,
context) => {
const data = snapshot.val();
console.log('New data added:', data);
// Perform additional actions here
});
exports.onDataUpdated = functions.database.ref('/path/to/data/{pushId}').onUpdate((change,
context) => {
const beforeData = change.before.val();
const afterData = change.after.val();
console.log('Data updated from', beforeData, 'to', afterData);
// Perform additional actions here
});
exports.onDataDeleted = functions.database.ref('/path/to/data/{pushId}').onDelete((snapshot,
context) => {
const deletedData = snapshot.val();
console.log('Data deleted:', deletedData);
// Perform additional actions here
});
admin.database().ref('/path/to/data').once('value').then(snapshot => {
const data = snapshot.val();
console.log('Exported Data:', data);
});
Import to Firestore
const firestore = admin.firestore();
firestore.collection('new_collection').doc('doc_id').set(data).then(() => {
console.log('Data imported to Firestore successfully');
}).catch(error => {
console.error('Error importing data:', error);
});