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?
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.
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.
The ParseUser is a local representation of user data that can be saved and retrieved from the Parse cloud.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With