Skip to main content

Set up custom cloud code

Prerequisites

Before getting started, make sure you have followed this section Run parse-server locally and have a server already set up locally.

Step 1: Add custom cloud code.

Inside a self-hosted server you will have to use the parse-server syntax.
Parse server cloud code documentation.

By default the cloud code is located in src/cloud/main.ts.

Begin by creating a new file inside your cloud folder and give it the name you want. For this example we will use cloud.ts.

Inside your newly created file src/cloud/cloud.tsadd your cloud funtictions.

declare const Parse: any;

Parse.Cloud.define('Hello', () => {
return `Hello! Cloud functions are cool!`;
});

Parse.Cloud.define('SayMyName', (request: any) => {
return `Hello ${request.params.name}! Cloud functions are cool!`;
});

You can now import this file in your main.ts

declare const Parse: any;
import './generated/evmApi';
import './generated/solApi';

//import your file
import './cloud';


//.........

Step 2: Testing

You can now build the project and start it locally.

npm run build

Start the project.

npm run start

On the client side, call the cloud function using the Moralis v1 syntax.

const response = await Moralis.Cloud.run("Hello");
console.log(response);

Video Tutorial

Set up jobs with parse-server

We will add custom jobs to our self-hosted server.

First, before creating a job we need to install the required package:

npm install parse-server-jobs-scheduler

Back inside your src/cloud/ folder, create a new file called jobs.ts and add the following code:

declare const Parse: any;

import Scheduler from 'parse-server-jobs-scheduler';
const scheduler = new Scheduler();

// Recreates all crons when the server is launched
scheduler.recreateScheduleForAllJobs();

// Recreates schedule when a job schedule has changed
Parse.Cloud.afterSave('_JobSchedule', async (request: any) => {
scheduler.recreateSchedule(request.object.id);
});

// Destroy schedule for removed job
Parse.Cloud.afterDelete('_JobSchedule', async (request: any) => {
scheduler.destroySchedule(request.object.id);
});

Import the new file inside src/cloud/main.ts

declare const Parse: any;
import './generated/evmApi';
import './generated/solApi';
import './cloud';

//import your file
import './jobs';

Inside your custom cloud.ts file lets create a job:

declare const Parse: any;

Parse.Cloud.define('Hello', () => {
return `Hello! Cloud functions are cool!`;
});

Parse.Cloud.define('SayMyName', (request: any) => {
return `Hello ${request.params.name}! Cloud functions are cool!`;
});

Parse.Cloud.job('newJob', () => {
console.log('The code inside newJob has beed executed');
return 'test';
});

You can now rebuild and start your server.

npm run build

Start the project.

npm run start

Now head over http://localhost:1337/dashboard/ and select Schedule a job to test it.

After you chose the job and the desire interval you can now click schedule:

If Start immediately was selected your job will start right away and repeat based on the selected time interval.