合并/清理 Firestore 索引

问题描述 投票:0回答:1

我的应用程序目前在 Firestore 中有 154 个索引。随着新功能的添加,它们随着时间的推移而生成......并且随着时间的推移,一些草率的流程允许某些 Firebase 项目添加其他项目尚未添加的索引(通过错误消息中的自动创建 URL)。

因此,我希望聚合所有 Firebase 项目中的所有索引定义 - 这是一个白标签应用程序,相同的代码,并且每个项目的大部分配置相同......这是“清理和同步”时间。

我正在寻找一种方法来聚合我拥有的十几个

firestore.index.json
文件,方式如下:

  1. 不会丢失任何索引
  2. 可能会识别冗余索引以将其过滤掉

我编写了以下代码来“展平”并对不同的Firestore索引JSON文件进行排序,所以至少我可以

diff
两个项目的配置。

import fs from 'fs';

const filename = process.argv[2];
const fileContent = fs.readFileSync(filename, 'utf-8');
const jsonData = JSON.parse(fileContent);

/* the jsonData has the structure:
  {
    "indexes": [
      {
        "collectionGroup": "act",
        "queryScope": "COLLECTION",
        "fields": [
          {
            "fieldPath": "actNumber",
            "order": "ASCENDING"
          },
          {
            "fieldPath": "actStatus.name",
            "order": "ASCENDING"
          },
        ]
      }
    ]

Now convert each element of the array into a one-line string of format: "collectionGroup,fieldPath:order,fieldPath:order,...", sort, and output to console
*/

jsonData.indexes
  .map((index) => {
    const fieldStrings = index.fields.map((field) => `${field.fieldPath}:${field.order}`);
    return `${index.collectionGroup},${fieldStrings.join(',')}`;
  })
  .sort()
  .forEach((line) => console.log(line));

但我不确定如何/在哪里寻求“合并”索引的方向,或者是否可能。

firebase google-cloud-firestore
1个回答
0
投票

我还没有完全解决我的OP,但这是我暂时采用的解决方案。我把它写在了这篇博文中

© www.soinside.com 2019 - 2024. All rights reserved.