CSV または JSON を Firebase Cloud Firestore にインポートする方法 質問する

CSV または JSON を Firebase Cloud Firestore にインポートする方法 質問する

Firebase Realtime データベースのように、CSV または JSON を Firebase Cloud Firestore にインポートする方法はありますか?

ここに画像の説明を入力してください

ベストアンサー1

一般的な解決策

JSON をアップロードできるスクリプトは数多くありますが、サブコレクションを許可しているものはありません。上記のスクリプトは、あらゆるレベルのネストとサブコレクションを処理します。また、ドキュメントに独自のデータとサブコレクションがある場合も処理します。これは、コレクションがオブジェクトの配列/オブジェクト (空のオブジェクトまたは配列を含む) であるという前提に基づいています。

スクリプトを実行するには、npm と node がインストールされていることを確認してください。次に、コードを として実行しますnode <name of the file>。クラウド関数としてデプロイする必要はないことに注意してください。

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 in the 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);

おすすめ記事