Skip to content

Instantly share code, notes, and snippets.

@tettoffensive
Forked from brunobraga95/copyFirestoreDB.js
Last active August 12, 2019 18:03
Show Gist options
  • Select an option

  • Save tettoffensive/7a8819c93aace8089e8f9237c7c2fc98 to your computer and use it in GitHub Desktop.

Select an option

Save tettoffensive/7a8819c93aace8089e8f9237c7c2fc98 to your computer and use it in GitHub Desktop.

Revisions

  1. tettoffensive revised this gist Aug 12, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions copyFirestoreDB.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    const firebase = require('firebase-admin');

    var serviceAccountSource = require("./source.json"); // source DB key
    var serviceAccountDestination = require("./destination.json"); // destiny DB key
    var serviceAccountDestination = require("./destination.json"); // destination DB key

    const sourceAdmin = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccountSource)
    });

    const destinyAdmin = firebase.initializeApp({
    const destinationAdmin = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccountDestination)
    }, "destination");

  2. @brunobraga95 brunobraga95 revised this gist Jan 29, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions copyFirestoreDB.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    const firebase = require('firebase-admin');

    var serviceAccountSource = require("./meu-projeto-incrivel-origem.json"); // source DB key
    var serviceAccountDestination = require("./meu-projeto-incrivel-destino.json"); // destiny DB key
    var serviceAccountSource = require("./source.json"); // source DB key
    var serviceAccountDestination = require("./destination.json"); // destiny DB key

    const sourceAdmin = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccountSource)
    @@ -11,7 +11,7 @@ const destinyAdmin = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccountDestination)
    }, "destination");

    /* this schema is how your DB is organized. You don't have to care about the Documents
    /* this schema is how your DB is organized in a tree structure. You don't have to care about the Documents
    but you do need to inform the name of your collections and any subcollections, in this
    case we have two collections called users and groups, the all have their documents, but
    the collection users has its own subcollections, friends and groups, which again have their
  3. @brunobraga95 brunobraga95 created this gist Jan 29, 2018.
    59 changes: 59 additions & 0 deletions copyFirestoreDB.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    const firebase = require('firebase-admin');

    var serviceAccountSource = require("./meu-projeto-incrivel-origem.json"); // source DB key
    var serviceAccountDestination = require("./meu-projeto-incrivel-destino.json"); // destiny DB key

    const sourceAdmin = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccountSource)
    });

    const destinyAdmin = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccountDestination)
    }, "destination");

    /* this schema is how your DB is organized. You don't have to care about the Documents
    but you do need to inform the name of your collections and any subcollections, in this
    case we have two collections called users and groups, the all have their documents, but
    the collection users has its own subcollections, friends and groups, which again have their
    own subcollection, messages.
    */
    const schema = {
    users: {
    friends: {
    messages: {},
    },
    groups: {
    messages: {},
    },
    },
    groups: {},
    };

    var source = sourceAdmin.firestore();
    var destination = destinationAdmin.firestore();

    const copy = (sourceDBrep, destinationDBref, aux) => {
    return Promise.all(Object.keys(aux).map((collection) => {
    return sourceDBrep.collection(collection).get()
    .then((data) => {
    let promises = [];
    data.forEach((doc) => {
    const data = doc.data();
    promises.push(
    destinationDBref.collection(collection).doc(doc.id).set(data).then((data) => {
    return copy(sourceDBrep.collection(collection).doc(doc.id),
    destinationDBref.collection(collection).doc(doc.id),
    aux[collection])
    })
    );
    })
    return Promise.all(promises);
    })
    }));
    };

    copy(source, destination, aux).then(() => {
    console.log('copied');
    });