Cloud code structure

I am using Parse Cloud Code for a social mobile application. I want to make cloud code scalable, but Parse has some rules that I must obey. The structure is similar:

cloud/ main.js other.js otherfile/ someother.js ... ... 

only main.js is a necessity, and mobile clients can only call functions inside main.js.

In my clients, I use MVC as an architecture, but I'm not sure which architecture I should use in my cloud code. As my cloud code architecture should have.

Is there a common backend architecture that I can use?

+6
source share
3 answers

I myself created the structure. But it certainly can be improved.

I tried to make my main.js simple. I added only the names of functions that will be called outside of the cloud code.

 // Include all of the modules var module1 = require('cloud/folder1/file1.js'); var module2 = require('cloud/folder1/file2.js'); var module3 = require('cloud/folder2/file1.js'); var backgroundjob = require('cloud/backgroundjob/background.js'); Parse.Cloud.job("startBackgroundJob", backgroundjob.startBackgroundJob); Parse.Cloud.define("do_this_stuff", module1.thisfunction); Parse.Cloud.define("do_this_stuff2", module1.notthisfunction); Parse.Cloud.define("do_that_stuff", module2.thatfunction); Parse.Cloud.define("do_dat_stuff", module3.datfunction); 

In file1.js, I wrote the following functions.

 // Include libraries var utils = require("cloud/utils/utils.js"); var _ = require('underscore'); // Export Modules module.exports = { thisfunction: function (request, response) { addComment(request, response); }, thatfunction: function (request, response) { getComment(request, response); }, }; function addComment(request, response) { // write your code here var stuff = utils.callThisFunction(param); // This is the usage of another function in another file response.success("Comment added"); // or error but do not forget this } function getComment(request, response) { // write your code here response.success("Got Comment"); // or error but do not forget this } 

I exported the modules as shown because it makes the code more readable. I can just take a look at the top of the code and see what functions I can use from this file. You can use the document export style .

 exports.addComment = function(request, response) { // your code response.success(); } 
+7
source

Inside the cloud code, main.js lives as it is. All cloud code functions live inside this single file. No modulation or additional architecture.

Parse.Cloud.run (name, data, parameters) is the only way to call Parse Cloud functions.

R

0
source

You can split your code into modules by creating a new module next to main.js , say services.js

And we will require it in main.js

 require("cloud/services.js"); 

Finally, all the cloud functions defined in this file will be available to you, as in main.js This is because Parse runs everything in this file when you require it, which essentially means that you just took all this code into a separate file.

0
source

Source: https://habr.com/ru/post/981492/


All Articles