parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Split Cloud Code logic into multiple files

Open Vortec4800 opened this issue 10 years ago • 4 comments

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!

Vortec4800 avatar Apr 25 '16 18:04 Vortec4800

Yep thats exactly how you do it. Just require the files you need from your main.js.

drew-gross avatar Apr 25 '16 18:04 drew-gross

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?

Vortec4800 avatar Apr 25 '16 19:04 Vortec4800

You need to export anything you intend to use from main.js. You don't need to export your triggers or Cloud Code functions.

drew-gross avatar Apr 25 '16 19:04 drew-gross

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?

Abderezai avatar Jul 08 '16 21:07 Abderezai