firebase functions manage multiple operations on a single database trigger










0















We are using firebase realtime DB and firebase functions. We have wrote a DB trigger for whenever a user is updated. In this case we have a referral system. So whenever a user adds a referrer to his account then this trigger gives some reward to the referrar. Hence the user_update update trigger does the job.



This works well. Now, we need to do one more unrelated activity whenever user is updated. To be specific we want to keep total reward given so far to all the users for analytics purpose.



So, what is the best way to implement two independent operations on a single update trigger?



Technically we can embed one operation call into another but that will make like hell and messy especially if need more operations like that in future.










share|improve this question






















  • You have to decide - do you want "messy", or do you want reduced cost? Is "messy" really all that bad? Can you structure your code to make it less messy?

    – Doug Stevenson
    Nov 14 '18 at 3:40











  • what is the solution with ignoring cost in this case ?

    – Vik
    Nov 14 '18 at 3:43











  • The solution is the one that you prefer. There is no right or wrong way. You're effectively asking for an opinion here.

    – Doug Stevenson
    Nov 14 '18 at 3:43












  • well u havent read the question completely. i suggested one way of embedding calls. i dont even know how to do it as separate calls on a single object property change.

    – Vik
    Nov 14 '18 at 4:48















0















We are using firebase realtime DB and firebase functions. We have wrote a DB trigger for whenever a user is updated. In this case we have a referral system. So whenever a user adds a referrer to his account then this trigger gives some reward to the referrar. Hence the user_update update trigger does the job.



This works well. Now, we need to do one more unrelated activity whenever user is updated. To be specific we want to keep total reward given so far to all the users for analytics purpose.



So, what is the best way to implement two independent operations on a single update trigger?



Technically we can embed one operation call into another but that will make like hell and messy especially if need more operations like that in future.










share|improve this question






















  • You have to decide - do you want "messy", or do you want reduced cost? Is "messy" really all that bad? Can you structure your code to make it less messy?

    – Doug Stevenson
    Nov 14 '18 at 3:40











  • what is the solution with ignoring cost in this case ?

    – Vik
    Nov 14 '18 at 3:43











  • The solution is the one that you prefer. There is no right or wrong way. You're effectively asking for an opinion here.

    – Doug Stevenson
    Nov 14 '18 at 3:43












  • well u havent read the question completely. i suggested one way of embedding calls. i dont even know how to do it as separate calls on a single object property change.

    – Vik
    Nov 14 '18 at 4:48













0












0








0








We are using firebase realtime DB and firebase functions. We have wrote a DB trigger for whenever a user is updated. In this case we have a referral system. So whenever a user adds a referrer to his account then this trigger gives some reward to the referrar. Hence the user_update update trigger does the job.



This works well. Now, we need to do one more unrelated activity whenever user is updated. To be specific we want to keep total reward given so far to all the users for analytics purpose.



So, what is the best way to implement two independent operations on a single update trigger?



Technically we can embed one operation call into another but that will make like hell and messy especially if need more operations like that in future.










share|improve this question














We are using firebase realtime DB and firebase functions. We have wrote a DB trigger for whenever a user is updated. In this case we have a referral system. So whenever a user adds a referrer to his account then this trigger gives some reward to the referrar. Hence the user_update update trigger does the job.



This works well. Now, we need to do one more unrelated activity whenever user is updated. To be specific we want to keep total reward given so far to all the users for analytics purpose.



So, what is the best way to implement two independent operations on a single update trigger?



Technically we can embed one operation call into another but that will make like hell and messy especially if need more operations like that in future.







firebase firebase-realtime-database google-cloud-functions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 3:27









VikVik

1,953124198




1,953124198












  • You have to decide - do you want "messy", or do you want reduced cost? Is "messy" really all that bad? Can you structure your code to make it less messy?

    – Doug Stevenson
    Nov 14 '18 at 3:40











  • what is the solution with ignoring cost in this case ?

    – Vik
    Nov 14 '18 at 3:43











  • The solution is the one that you prefer. There is no right or wrong way. You're effectively asking for an opinion here.

    – Doug Stevenson
    Nov 14 '18 at 3:43












  • well u havent read the question completely. i suggested one way of embedding calls. i dont even know how to do it as separate calls on a single object property change.

    – Vik
    Nov 14 '18 at 4:48

















  • You have to decide - do you want "messy", or do you want reduced cost? Is "messy" really all that bad? Can you structure your code to make it less messy?

    – Doug Stevenson
    Nov 14 '18 at 3:40











  • what is the solution with ignoring cost in this case ?

    – Vik
    Nov 14 '18 at 3:43











  • The solution is the one that you prefer. There is no right or wrong way. You're effectively asking for an opinion here.

    – Doug Stevenson
    Nov 14 '18 at 3:43












  • well u havent read the question completely. i suggested one way of embedding calls. i dont even know how to do it as separate calls on a single object property change.

    – Vik
    Nov 14 '18 at 4:48
















You have to decide - do you want "messy", or do you want reduced cost? Is "messy" really all that bad? Can you structure your code to make it less messy?

– Doug Stevenson
Nov 14 '18 at 3:40





You have to decide - do you want "messy", or do you want reduced cost? Is "messy" really all that bad? Can you structure your code to make it less messy?

– Doug Stevenson
Nov 14 '18 at 3:40













what is the solution with ignoring cost in this case ?

– Vik
Nov 14 '18 at 3:43





what is the solution with ignoring cost in this case ?

– Vik
Nov 14 '18 at 3:43













The solution is the one that you prefer. There is no right or wrong way. You're effectively asking for an opinion here.

– Doug Stevenson
Nov 14 '18 at 3:43






The solution is the one that you prefer. There is no right or wrong way. You're effectively asking for an opinion here.

– Doug Stevenson
Nov 14 '18 at 3:43














well u havent read the question completely. i suggested one way of embedding calls. i dont even know how to do it as separate calls on a single object property change.

– Vik
Nov 14 '18 at 4:48





well u havent read the question completely. i suggested one way of embedding calls. i dont even know how to do it as separate calls on a single object property change.

– Vik
Nov 14 '18 at 4:48












1 Answer
1






active

oldest

votes


















1














You have two options, either use 1 realtime database trigger like now and put the logic in that function. You can make it clean and tidy by putting all the logic in separate functions that this trigger just calls



Or you can simply create another trigger exactly how you did this time and just change its export name e.g. like below. With this method all it means is you have 2 functions being called so doubling the cost.



exports.userUpdate = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );



exports.userUpdateSecond = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );






share|improve this answer























  • hey Jack if i go with option one which is keeping two logics into a single trigger to save on cost then how can that structure look like? the way i think of doing that is like making first call to db query and in the response make another call. This ensures that both gets called always. But this makes it all messy due to embedding a unrelated logic into another. is there a way to somehow keep the two calls separate in the same function and ensure both gets executed?

    – Vik
    Nov 14 '18 at 17:34











  • Really depends on how many calls you think you will be doing. Cloud functions have a very generous free tier so if you wont explode that just separate it and make it easier on yourself. Or if in the same trigger thats just a javascript question of how you separate code. Just put two functions inside the trigger, have them both return and put those separate functions in different files

    – Jack Woodward
    Nov 15 '18 at 9:24











  • any example of separating the two calls within same trigger?

    – Vik
    Nov 15 '18 at 17:08










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292765%2ffirebase-functions-manage-multiple-operations-on-a-single-database-trigger%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You have two options, either use 1 realtime database trigger like now and put the logic in that function. You can make it clean and tidy by putting all the logic in separate functions that this trigger just calls



Or you can simply create another trigger exactly how you did this time and just change its export name e.g. like below. With this method all it means is you have 2 functions being called so doubling the cost.



exports.userUpdate = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );



exports.userUpdateSecond = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );






share|improve this answer























  • hey Jack if i go with option one which is keeping two logics into a single trigger to save on cost then how can that structure look like? the way i think of doing that is like making first call to db query and in the response make another call. This ensures that both gets called always. But this makes it all messy due to embedding a unrelated logic into another. is there a way to somehow keep the two calls separate in the same function and ensure both gets executed?

    – Vik
    Nov 14 '18 at 17:34











  • Really depends on how many calls you think you will be doing. Cloud functions have a very generous free tier so if you wont explode that just separate it and make it easier on yourself. Or if in the same trigger thats just a javascript question of how you separate code. Just put two functions inside the trigger, have them both return and put those separate functions in different files

    – Jack Woodward
    Nov 15 '18 at 9:24











  • any example of separating the two calls within same trigger?

    – Vik
    Nov 15 '18 at 17:08















1














You have two options, either use 1 realtime database trigger like now and put the logic in that function. You can make it clean and tidy by putting all the logic in separate functions that this trigger just calls



Or you can simply create another trigger exactly how you did this time and just change its export name e.g. like below. With this method all it means is you have 2 functions being called so doubling the cost.



exports.userUpdate = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );



exports.userUpdateSecond = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );






share|improve this answer























  • hey Jack if i go with option one which is keeping two logics into a single trigger to save on cost then how can that structure look like? the way i think of doing that is like making first call to db query and in the response make another call. This ensures that both gets called always. But this makes it all messy due to embedding a unrelated logic into another. is there a way to somehow keep the two calls separate in the same function and ensure both gets executed?

    – Vik
    Nov 14 '18 at 17:34











  • Really depends on how many calls you think you will be doing. Cloud functions have a very generous free tier so if you wont explode that just separate it and make it easier on yourself. Or if in the same trigger thats just a javascript question of how you separate code. Just put two functions inside the trigger, have them both return and put those separate functions in different files

    – Jack Woodward
    Nov 15 '18 at 9:24











  • any example of separating the two calls within same trigger?

    – Vik
    Nov 15 '18 at 17:08













1












1








1







You have two options, either use 1 realtime database trigger like now and put the logic in that function. You can make it clean and tidy by putting all the logic in separate functions that this trigger just calls



Or you can simply create another trigger exactly how you did this time and just change its export name e.g. like below. With this method all it means is you have 2 functions being called so doubling the cost.



exports.userUpdate = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );



exports.userUpdateSecond = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );






share|improve this answer













You have two options, either use 1 realtime database trigger like now and put the logic in that function. You can make it clean and tidy by putting all the logic in separate functions that this trigger just calls



Or you can simply create another trigger exactly how you did this time and just change its export name e.g. like below. With this method all it means is you have 2 functions being called so doubling the cost.



exports.userUpdate = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );



exports.userUpdateSecond = functions.database.ref('/users/uid').onUpdate(async (change, context) => /* LOGIC */ );







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 10:39









Jack WoodwardJack Woodward

61139




61139












  • hey Jack if i go with option one which is keeping two logics into a single trigger to save on cost then how can that structure look like? the way i think of doing that is like making first call to db query and in the response make another call. This ensures that both gets called always. But this makes it all messy due to embedding a unrelated logic into another. is there a way to somehow keep the two calls separate in the same function and ensure both gets executed?

    – Vik
    Nov 14 '18 at 17:34











  • Really depends on how many calls you think you will be doing. Cloud functions have a very generous free tier so if you wont explode that just separate it and make it easier on yourself. Or if in the same trigger thats just a javascript question of how you separate code. Just put two functions inside the trigger, have them both return and put those separate functions in different files

    – Jack Woodward
    Nov 15 '18 at 9:24











  • any example of separating the two calls within same trigger?

    – Vik
    Nov 15 '18 at 17:08

















  • hey Jack if i go with option one which is keeping two logics into a single trigger to save on cost then how can that structure look like? the way i think of doing that is like making first call to db query and in the response make another call. This ensures that both gets called always. But this makes it all messy due to embedding a unrelated logic into another. is there a way to somehow keep the two calls separate in the same function and ensure both gets executed?

    – Vik
    Nov 14 '18 at 17:34











  • Really depends on how many calls you think you will be doing. Cloud functions have a very generous free tier so if you wont explode that just separate it and make it easier on yourself. Or if in the same trigger thats just a javascript question of how you separate code. Just put two functions inside the trigger, have them both return and put those separate functions in different files

    – Jack Woodward
    Nov 15 '18 at 9:24











  • any example of separating the two calls within same trigger?

    – Vik
    Nov 15 '18 at 17:08
















hey Jack if i go with option one which is keeping two logics into a single trigger to save on cost then how can that structure look like? the way i think of doing that is like making first call to db query and in the response make another call. This ensures that both gets called always. But this makes it all messy due to embedding a unrelated logic into another. is there a way to somehow keep the two calls separate in the same function and ensure both gets executed?

– Vik
Nov 14 '18 at 17:34





hey Jack if i go with option one which is keeping two logics into a single trigger to save on cost then how can that structure look like? the way i think of doing that is like making first call to db query and in the response make another call. This ensures that both gets called always. But this makes it all messy due to embedding a unrelated logic into another. is there a way to somehow keep the two calls separate in the same function and ensure both gets executed?

– Vik
Nov 14 '18 at 17:34













Really depends on how many calls you think you will be doing. Cloud functions have a very generous free tier so if you wont explode that just separate it and make it easier on yourself. Or if in the same trigger thats just a javascript question of how you separate code. Just put two functions inside the trigger, have them both return and put those separate functions in different files

– Jack Woodward
Nov 15 '18 at 9:24





Really depends on how many calls you think you will be doing. Cloud functions have a very generous free tier so if you wont explode that just separate it and make it easier on yourself. Or if in the same trigger thats just a javascript question of how you separate code. Just put two functions inside the trigger, have them both return and put those separate functions in different files

– Jack Woodward
Nov 15 '18 at 9:24













any example of separating the two calls within same trigger?

– Vik
Nov 15 '18 at 17:08





any example of separating the two calls within same trigger?

– Vik
Nov 15 '18 at 17:08

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292765%2ffirebase-functions-manage-multiple-operations-on-a-single-database-trigger%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線