使用Firebase实时数据库云功能的节点和所有父节点的增量计数器

时间:2019-03-26 01:17:56

标签: javascript firebase firebase-realtime-database google-cloud-functions

我正在使用js sdk为我的Firebase实时数据库(不是firestore)编写云函数。这些功能递增计数器,用于计数每个区域的用户数。我正在使用的区域是地理区域和嵌套区域(大陆>国家>地区>城市)。通过将用户插入给定区域(.onCreate)的用户列表来触发功能。如果将某个用户插入某个国家/地区的用户列表,则该用户会自动添加到上级大陆的用户列表中。

修补了一段时间后,我只能想出一个“低级”的不可扩展解决方案(顺便说一句)。这意味着如果要向城市添加子区域,则需要编写附加代码。

这是一个问题,因为我希望将来会添加子区域。

问题:是否可以编写一种通用的云函数来增加节点的计数器及其所有父节点的计数器?

我对Firebase还是很陌生,我们将不胜感激

firebase实时数据库的Json结构

{
  "groups" : {
    "location" : {
      "continents" : {
        "EU" : {
          "countries" : {
            "DE" : {
              "created" : "2019-03-25--20:03:34",
              "flag" : "/flags/de.svg",
              "label" : "Germany",
              "level" : 2,
              "regions" : {
                "BE" : {
                  "cities" : {
                    "Berlin" : {
                      "created" : "2019-03-25--20:03:35",
                      "label" : "Berlin",
                      "level" : 3,
                      "type" : "location",
                      "usercount" : 1,
                      "users" : {
                        "rvDUa5n0oufRFGA9JB7lioom0ac2" : {
                          "created" : "2019-03-25--20:03:55",
                          "label" : "Anonymous"
                        }
                      }
                    }
                  },
                  "created" : "2019-03-25--20:03:35",
                  "label" : "Land Berlin",
                  "level" : 3,
                  "type" : "location",
                  "usercount" : 1,
                  "users" : {
                    "rvDUa5n0oufRFGA9JB7lioom0ac2" : {
                      "created" : "2019-03-25--20:03:52",
                      "label" : "Anonymous"
                    }
                  }
                }
              },
              "type" : "location",
              "usercount" : 1,
              "users" : {
                "rvDUa5n0oufRFGA9JB7lioom0ac2" : {
                  "created" : "2019-03-25--20:03:49",
                  "label" : "Anonymous"
                }
              }
            }
          },
          "created" : "2019-03-25--20:03:33",
          "label" : "Europe",
          "level" : 1,
          "type" : "location",
          "usercount" : 1,
          "users" : {
            "rvDUa5n0oufRFGA9JB7lioom0ac2" : {
              "created" : "2019-03-25--20:03:46",
              "label" : "Anonymous"
            }
          }
        }
      }
    }
  }
}

我的云用于用户插入功能-仍然需要写操作以删除用户

exports.onUserAddToContinent = functions.database
.ref('/groups/location/continents/{continentID}/users/{userID}')
.onCreate((snapshot, context) => {

const continentId = context.params.continentID
const counterRef = admin.database().ref('/groups/location/continents/' + continentId + '/usercount');

return counterRef.transaction(usercount => {
  return (usercount || 0) + 1
  })
})

exports.onUserAddToCountry = functions.database
.ref('/groups/location/continents/{continentID}/countries/{countryID}/users/{userID}')
.onCreate((snapshot, context) => {

    const continentId = context.params.continentID
    const countryId = context.params.countryID
    const counterRef = admin.database().ref('/groups/location/continents/' + continentId + '/countries/' + countryId + '/usercount');

    return counterRef.transaction(usercount => {
      return (usercount || 0) + 1
      })
})

exports.onUserAddToRegion = functions.database
.ref('/groups/location/continents/{continentID}/countries/{countryID}/regions/{regionID}/users/{userID}')
.onCreate((snapshot, context) => {

    const continentId = context.params.continentID
    const countryId = context.params.countryID
    const regionId = context.params.regionID
    const counterRef = admin.database().ref('/groups/location/continents/' + continentId + '/countries/' + countryId + '/regions/' + regionId +'/usercount');

    return counterRef.transaction(usercount => {
      return (usercount || 0) + 1
      })
})

exports.onUserAddToCity = functions.database
.ref('/groups/location/continents/{continentID}/countries/{countryID}/regions/{regionID}/cities/{cityID}/users/{userID}')
.onCreate((snapshot, context) => {

    const continentId = context.params.continentID
    const countryId = context.params.countryID
    const regionId = context.params.regionID
    const cityId = context.params.cityID
    const counterRef = admin.database().ref('/groups/location/continents/' + continentId + '/countries/' + countryId + '/regions/' + regionId +'/cities/'+ cityId + '/usercount');

    return counterRef.transaction(usercount => {
      return (usercount || 0) + 1
      })
})

我觉得我缺少一些东西。必须有一种通用的方法。

0 个答案:

没有答案