Skip to content

Instantly share code, notes, and snippets.

@jofftiquez
Forked from katowulf/firebase_copy.js
Last active April 7, 2024 13:29
Show Gist options
  • Save jofftiquez/f60dc81b39d77cd4eb1f5b5cbe6585ad to your computer and use it in GitHub Desktop.
Save jofftiquez/f60dc81b39d77cd4eb1f5b5cbe6585ad to your computer and use it in GitHub Desktop.
Firebase realtime database - how to copy or move data to a new path?
function copyFbRecord(oldRef, newRef) {
return Promise((resolve, reject) => {
oldRef.once('value').then(snap => {
return newRef.set(snap.val());
}).then(() => {
console.log('Done!');
resolve();
}).catch(err => {
console.log(err.message);
reject();
});
});
}
function moveFbRecord(oldRef, newRef) {
return Promise((resolve, reject) => {
oldRef.once('value').then(snap => {
return newRef.set(snap.val());
}).then(() => {
return oldRef.set(null);
}).then(() => {
console.log('Done!');
resolve();
}).catch(err => {
console.log(err.message);
reject();
});
})
}
@jofftiquez
Copy link
Author

@gigocabrera I haven't explored Firestore yet. 😅

@savageangelz
Copy link

Hello, I'm kinda new to firebase functions, I know how to deploy it and everything but how does one use this? I assume you need to call this function inside from firebase function that starts with 'exports'? I'm trying to make a function where a record or child 'A' that has a set of data to a new path and when one value inside child 'A' changes, which is to my knowledge by using ()onWrite event, but my problem are defining and the structure of the firebase function, can anyone provide me an example of such function?

@jofftiquez
Copy link
Author

@savageangelz can we see what you've already done?

@ilyasderaiya
Copy link

Can I get a Java Implementation of this?

@xpratov
Copy link

xpratov commented Apr 7, 2024

Hello, how do you do? 💎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment