Firebase Firestore云功能,多功能还是一个大功能更好?

Firebase Firestore云功能,多功能还是一个大功能更好?,firebase,google-cloud-firestore,listener,Firebase,Google Cloud Firestore,Listener,我的问题是,将使用相同.document和.onUpdate的函数组合成一个大函数,还是将它们分割成小的函数模块 例如,我有两个函数,都监听用户的onUpdate: export const firstUserFunction = functions.firestore .document('users/{userId}') .onUpdate((change, context) => { const newValue = <{ role: string }>

我的问题是,将使用相同
.document
.onUpdate
的函数组合成一个大函数,还是将它们分割成小的函数模块

例如,我有两个函数,都监听用户的onUpdate:

export const firstUserFunction = functions.firestore
   .document('users/{userId}')
   .onUpdate((change, context) => { 
   const newValue = <{ role: string }>change.after.data();
   const oldValue = <{ role: string }>change.before.data();
   if ((oldValue.role === '1' && newValue.role === '2')) {
       // do something
   }
})
export const secondUserFunction = functions.firestore
   .document('users/{userId}')
   .onUpdate((change, context) => {
   const newValue = <{ template: string }>change.after.data();
   if (newValue.template === 'x') {
       // do something
   } 
})
export const firstUserFunction=functions.firestore
.document('users/{userId}')
.onUpdate((更改,上下文)=>{
const newValue=change.after.data();
const oldValue=change.before.data();
if((oldValue.role=='1'&&newValue.role=='2')){
//做点什么
}
})
export const secondUserFunction=functions.firestore
.document('users/{userId}')
.onUpdate((更改、上下文)=>{
const newValue=change.after.data();
if(newValue.template==='x'){
//做点什么
} 
})
如果我们看一下定价,那么组合如下功能是有意义的:

export const combinedFunction = functions.firestore
   .document('users/{userId}')
   .onUpdate((change, context) => { 
   const newValue = <{ role: string, template: string }>change.after.data();
   const oldValue = <{ role: string }>change.before.data();

   if ((oldValue.role === '1' && newValue.role === '2')) {
       // do something
   }

   if (newValue.template === 'x') {
       // do something
   } 
})
export const combinedFunction=functions.firestore
.document('users/{userId}')
.onUpdate((更改,上下文)=>{
const newValue=change.after.data();
const oldValue=change.before.data();
if((oldValue.role=='1'&&newValue.role=='2')){
//做点什么
}
if(newValue.template==='x'){
//做点什么
} 
})
而不是小的,但是有大的有什么缺点吗


<>如果组合起来更好,你会考虑什么限制? 这将取决于具体情况。例如,如果总是调用这两个函数,那么将它们组合起来是有意义的,因为它们总是一起运行,一个函数可以工作

但是,如果这两个函数不总是运行,那么将它们拆分是有意义的,因为它们将根据情况运行。例如,在您的情况下,如果函数
firstUserFunction
secondUserFunction
总是同时运行,那么组合起来是有意义的,这样可以节省资金和处理


如果这些信息对你有帮助,请告诉我

我强烈倾向于对每个类型/路径组合只使用触发器。如果单个触发器需要多个效果,我更愿意在自己的代码中通过调用单个云函数中的多个(正常)函数来处理,因为它允许我自己控制流,而不是依赖云函数。但这是个人偏好,没有单一的最佳答案。是的,谢谢,这有助于重新回答我的问题,但我不能把它作为一个答案,因为这篇文章已被锁定为一个意见问题,对不起:)嗨@Aleksanderilkov,当然!没问题,如果你喜欢这个答案,请考虑一下投票吧!谢谢你让我知道!