0

In Firestore I managed to store my large JSON database with the help of the first solution of this thread (the code I'm referring to is below the link): How to import CSV or JSON to firebase cloud firestore

const admin = require('../functions/node_modules/firebase-admin');
const serviceAccount = require("./service-key.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<your-database-name>.firebaseio.com"
});

const data = require("./fakedb.json");

/**
 * Data is a collection if
 *  - it has a odd depth
 *  - contains only objects or contains no objects.
 */
function isCollection(data, path, depth) {
  if (
    typeof data != 'object' ||
    data == null ||
    data.length === 0 ||
    isEmpty(data)
  ) {
    return false;
  }

  for (const key in data) {
    if (typeof data[key] != 'object' || data[key] == null) {
      // If there is at least one non-object item then it data then it cannot be collection.
      return false;
    }
  }

  return true;
}

// Checks if object is empty.
function isEmpty(obj) {
  for(const key in obj) {
    if(obj.hasOwnProperty(key)) {
      return false;
    }
  }
  return true;
}

async function upload(data, path) {
  return await admin.firestore()
    .doc(path.join('/'))
    .set(data)
    .then(() => console.log(`Document ${path.join('/')} uploaded.`))
    .catch(() => console.error(`Could not write document ${path.join('/')}.`));
}

/**
 *
 */
async function resolve(data, path = []) {
  if (path.length > 0 && path.length % 2 == 0) {
    // Document's length of path is always even, however, one of keys can actually be a collection.

    // Copy an object.
    const documentData = Object.assign({}, data);

    for (const key in data) {
      // Resolve each collection and remove it from document data.
      if (isCollection(data[key], [...path, key])) {
        // Remove a collection from the document data.
        delete documentData[key];
        // Resolve a colleciton.
        resolve(data[key], [...path, key]);
      }
    }

    // If document is empty then it means it only consisted of collections.
    if (!isEmpty(documentData)) {
      // Upload a document free of collections.
      await upload(documentData, path);
    }
  } else {
    // Collection's length of is always odd.
    for (const key in data) {
      // Resolve each collection.
      await resolve(data[key], [...path, key]);
    }
  }
}

resolve(data);

The problem is, I want to fetch data with an "id" from my angular app from Firestore. Right now, the sub-folders that were created in Firestore go from 0 to n. Instead, what I want is: assign the "id"s of the array objects as the subfolder names in Firestore. This is a general javascript question I guess. I'm just not familiar with the syntax used in the provided code. I'm also not coding for a long time and hope someone can help me out.

This is the structure of my JSON database

{
    "customCollection": [{
            "p": "6",
            "c": "",
            "m": 0.1,
            "id": "60.1" //this id should be the subfolder's name in Firestore
        },
        {
            "p": "5",
            "c": "",
            "m": 0.1,
            "id": "50.1"
        }]
   }

So in Firestore, the structure should be:

customCollection -> 60.1 -> data of first object

customCollection -> 50.1 -> data of second object

instead of:

customCollection -> 0 -> data of first object

customCollection -> 1 -> data of second object

Thank you in advance for any help!

Rodrigo García
  • 1,357
  • 15
  • 26
Alex G.
  • 117
  • 2
  • 9

1 Answers1

0

The solution to this question was provided by "rgrcnh" in the link I posted:

This is a small modification that copies the 'id' of the document, if exists, to its path. Otherwise it will use the "for"'s index.

...   ...   } else {
    // Collection's length of is always odd.
    for (const key in data) {
      // Resolve each collection.
      if (data[key]['id']!==undefined)
        await resolve(data[key], [...path,data[key]['id']])
      else 
        await resolve(data[key], [...path, key]);
    }   } }

resolve(data);
Alex G.
  • 117
  • 2
  • 9