Javascript 使用firebase实时数据库云函数增加节点和所有父节点的计数器
我正在使用JSSDK为我的firebase实时数据库(不是firestore)编写云函数。这些函数递增计数器,用于计算每个区域的用户数。我使用的区域是地理区域和嵌套区域(大陆>国家>地区>城市)。将用户插入到给定区域(.onCreate)的用户列表中会触发这些函数。如果用户被插入到某个国家的用户列表中,它将自动添加到父大陆的用户列表中 经过一段时间的修修补补,我只能想出一个“低级”的不可扩展的解决方案(顺便说一句,这是可行的)。这意味着,如果我想将子区域添加到城市,我需要编写附加代码 这是一个问题,因为我希望在将来添加子区域 问:是否有一种方法可以编写一个通用的云函数,该函数增加一个节点及其所有父节点的计数器 我是firebase的新手,非常感谢您的帮助 firebase实时数据库的Json结构 我的云函数用于用户插入-仍然需要为用户删除而编写 我觉得我错过了什么。必须有一种通用的方法来做到这一点Javascript 使用firebase实时数据库云函数增加节点和所有父节点的计数器,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我正在使用JSSDK为我的firebase实时数据库(不是firestore)编写云函数。这些函数递增计数器,用于计算每个区域的用户数。我使用的区域是地理区域和嵌套区域(大陆>国家>地区>城市)。将用户插入到给定区域(.onCreate)的用户列表中会触发这些函数。如果用户被插入到某个国家的用户列表中,它将自动添加到父大陆的用户列表中 经过一段时间的修修补补,我只能想出一个“低级”的不可扩展的解决方案(顺便说一句,这是可行的)。这意味着,如果我想将子区域添加到城市,我需要编写附加代码 这是一个问
{
"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
})
})