Split Cloud Code logic into multiple files
Forgive me if this is referenced elsewhere, but I couldn't find any other issues or documentation regarding this.
Right now we have a Cloud Code main.js file that is ~3000 lines long and contains a lot of business logic. I'd like to break this apart into multiple files to make organization easier. I'm guessing you would split the file apart, then in your main.js file would just be a bunch of require statements? How exactly might this work, and is it possible?
Thank you!
Yep thats exactly how you do it. Just require the files you need from your main.js.
The part I wasn't sure about was regarding module.exports and all of that. Do you need to export anything in the split-out files in order for this to work? And when you require them in the main.js file, do you need to assign the require'd object to anything?
You need to export anything you intend to use from main.js. You don't need to export your triggers or Cloud Code functions.
I want to split all the triggers on separate files, but some of the object triggers depend on each other. For example: say I have a RestaurantMenu Object and a Restaurant object. When I save my Restaurant object, I also update the Restaurant object. Do I need a require('Restaurant') in the RestaurantMenu.js file? or in the main.js they all get connected together?