How to import CSV or JSON to firebase cloud firestore

前端 未结 11 997
星月不相逢
星月不相逢 2020-11-29 16:19

Is there a way to import CSV or JSON to firebase cloud firestore like in firebase realtime database?

相关标签:
11条回答
  • 2020-11-29 17:05

    You need a custom script to do that.

    I wrote one based on the Firebase admin SDK, as long as firebase library does not allow you to import nested arrays of data.

    const admin = require('./node_modules/firebase-admin');
    const serviceAccount = require("./service-key.json");
    
    const data = require("./data.json");
    
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "https://YOUR_DB.firebaseio.com"
    });
    
    data && Object.keys(data).forEach(key => {
        const nestedContent = data[key];
    
        if (typeof nestedContent === "object") {
            Object.keys(nestedContent).forEach(docTitle => {
                admin.firestore()
                    .collection(key)
                    .doc(docTitle)
                    .set(nestedContent[docTitle])
                    .then((res) => {
                        console.log("Document successfully written!");
                    })
                    .catch((error) => {
                        console.error("Error writing document: ", error);
                    });
            });
        }
    });
    

    Update: I wrote an article on this topic - Filling Firestore with data

    0 讨论(0)
  • 2020-11-29 17:05

    For reference. I wrote a function that helps to import and export data in Firestore.

    https://github.com/dalenguyen/firestore-import-export

    0 讨论(0)
  • 2020-11-29 17:10
    var admin = require('firebase-admin');
    
    var serviceAccount = require('./serviceAccountKey.json');
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: 'https://csvread-d149c.firebaseio.com'
    });
    
    const csv = require('csv-parser');  
    const fs = require('fs');
    
    const firestore = admin.firestore();
    
    // CSV FILE data.csv
    
    // Name,Surname,Age,Gender
    // John,Snow,26,M  
    // Clair,White,33,F  
    // Fancy,Brown,78,F
    
    fs.createReadStream('data.csv')  
      .pipe(csv())
      .on('data', (row) => {
        console.log(row);
        if(row) {
          firestore.collection('csv').add({
            name: row.Name,
            surname: row.Surname,
            age: row.Age,
            sex: row.Gender
          });
        }
        else {
          console.log('No data')
        }
      })
      .on('end', () => {
        console.log('CSV file successfully processed');
      });
    
    0 讨论(0)
  • 2020-11-29 17:12

    There is not, you'll need to write your own script at this time.

    0 讨论(0)
  • 2020-11-29 17:15

    1 - Importing only collections

    If the names of your collections are not only composed of numbers, then you can define the name of the document as follows.

    
        ...
        ...
    
        } else {
            // Collection's length of is always odd.
            for (const key in data) {
              // // Resolve each collection.
    
              // If key is a number it means that it is not a collection
              // The variable id in this case will be the name of the document field or you 
              // can generate randomly
              let id = !isNaN(key) ? data[key].id : key;
    
              await resolve(data[key], [...path, id]);
            }
          }
        }
    
    

    2 - Import collections and sub-collections

    In the same way as in the example above, the name of the sub-collection can not contain only numbers.

        ...
        ...
    
        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];
    
            // If key is a number it means that it is not a collection
            // The variable id in this case will be the name of the document field or you 
            // can generate randomly
            let id = !isNaN(key) ? data[key].id : key;
    
            // Resolve a colleciton.
            resolve(data[key], [...path, id]);
          }
        }
    
        ...
        ...
    

    Note.: Changes in @Maciej Caputa's code

    0 讨论(0)
提交回复
热议问题