Library to help defining Cloud Code events

#1

Hello!

I wanted to share with you a library that I created to help defining functions and working with Cloud Code. So often it happens that you want to prevent an object from being created without some attributes, you want to define default / minimum / maximum values, among others.

Example usage:

const ParseCloudClass = require('parse-server-addon-cloud-class').ParseCloudClass;

// Create a new configuration object to define the class behaviour.
// All attributes are optional
const gamePoint = {
  requiredKeys: ['points'], // all objects saved must have the points attribute
  defaultValues: { points: 20 }, // by default, all new objects will have 20 points (if it was not set at the time of creation) 
  minimumValues: { points: 10 }, // minimum 10 points
  maximumValues: { points: 1000 }, // maximum 1000 points
  immutableKeys: ['points'], // once set, the points can't be changed (only master can do that)
  beforeFind: function(req) {
    // Do something here
    return req.query;
  },
  processBeforeSave: async function(req) {
    // Do something here
    return req.object;
  },
  afterSave: async function(req) {
    // Do something here
    return req.object;
  },
  processBeforeDelete: async function(req) {
    // Do something here
    return req.object;
  },
  afterDelete: async function(req) {
    // Do something here
    return req.object;
  }
}

// Create an instance
const gamePointClass = ParseCloudClass.fromObject(gamePoint);

// Configure the class in the main.js cloud file
ParseCloudClass.configureClass(Parse, 'GamePoint', gamePointClass);

The library is called Parse Cloud Class, and it’s available in Github here: https://github.com/owsas/parse-cloud-class. It’s also published on npm as parse-server-addon-cloud-class, so you can test it out very easily.

Right now I’m the only maintainer and would love to hear from you if you like this library. I’m open to new ideas to improve its current state :slight_smile: Just add an issue, open a PR or comment here and we can start discussing about it

Thank you so much!
Juan

3 Likes
#2

This looks awesome, I’m definitely going to try it out!

1 Like
#3

Looks like a great repo. You can add it to community highlight section.

There are PR examples.

#4

I’ve actually already added it :sweat_smile:, but there are some layout issue that need sorting out.

#5

Thanks for your ideas

It’s amazing, I want to add some default value for parse-server class recently before, because I want to use some attribute but it is not exists.