Parse Cloud Code Structure
Asked Answered
B

3

9

I'm using Parse Cloud Code for a social mobile application. I want to make the cloud code scalable but Parse has some rules which I must obey. The structure is like:

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

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

In my clients I'm using MVC as a architecture but I'm not sure what kind of architecture I should use in my cloud code. How should my cloud code architecture be.

Is there a general backend architecture which I can use?

Breakthrough answered 24/1, 2015 at 19:57 Comment(0)
B
14

I made a structure myself. But it surely can be improved.

I tried to make my main.js simple. I only added the functions names which will be called outside of 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 functions as following.

// 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 modules as shown because it makes the code more readable. I can just look at the top of the code and see what functions I can use from this file. You may use the docs export style.

exports.addComment = function(request, response) {
    // your code
    response.success();
}
Breakthrough answered 7/4, 2015 at 19:37 Comment(1)
This was very helpful! I went a step further and did the following: ``` var CloudFunctions = require('./source/cloud-functions.js'); .each(.keys(CloudFunctions), function (functionName) { Parse.Cloud.define(functionName, CloudFunctions[functionName]); }); ``` This way you don't need to define each one. Works for Jobs, saves/deletes etc tooDixon
C
0

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

And require it in main.js

require("cloud/services.js");

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

Caramelize answered 15/4, 2015 at 11:45 Comment(1)
I was planning on making a similar comment. I have cloud functions defined in several different modules and both Android and iOS apps find them. I realized at Main.js line 5000 or so that modules were the way to go!Blackface
P
-1

Within cloud code, main.js lives as is. All cloud code functions live inside that single file. There is no modulation or additional architecture.

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

R

Pavonine answered 6/4, 2015 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.