Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Cloud Code Structure

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?

like image 894
Thellimist Avatar asked Jan 24 '15 19:01

Thellimist


People also ask

What is parse cloud?

Parse Cloud Code is the easy way to implement some custom logic outside our web or mobile application like custom validation, pre-filtering resource collections or sending a text notification when some conditions are met.

How do I deploy back4app?

To deploy your cloud and public folders to your connected back4app app hit the command b4a deploy . You have now a cloud function deployed and ready to be used. To check your functions go to your App Dashboard click on Cloud Code Functions menu inside the Core section.

What is parse user?

The ParseUser is a local representation of user data that can be saved and retrieved from the Parse cloud.


1 Answers

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();
}
like image 107
Thellimist Avatar answered Oct 01 '22 04:10

Thellimist