Description
Hello!
First of all excuse me as I didn't find another way to share this, but I consider it may be useful for your parse-server
applications.
I created an addon called parse-server-addon-cloud-class
, available now on npm, which helps you organize and extend your parse-server
's Cloud Code. Please check the repo here: https://github.com/owsas/parse-cloud-class
With this module you can easily:
- Define minimum values for keys on your classes
- Define maximum values
- Define default values
- Define required keys
- Define immutable keys (only editable with the master key)
- Use addons to easily extend the funcionality of your app
- Create new addons and share them with the community
- Customize the default behaviour to your own needs
Classes
It helps you organize your code for each of your Parse classes, like so:
// GamePoint.js
class GamePoint extends ParseCloudClass {
requiredKeys = ['player']; // All points must have a player
minimumValues = {
credits: 10, // minimum ammount of 10 credits can be earned
}
maximumValues = {
credits: 100, // limit the ammount to maximum a 100 credits
}
defaultValues = {
credits: 20, // by default, give 20 credits
}
async afterSave(request) {
const obj = await super.afterSave(request);
// log something to Google Analytics ...
return obj;
}
}
module.exports = GamePoint;
Using
After writing your classes that extend ParseCloudClass
, you can import and use them in your Cloud Code's main.js
file, like this:
const { ParseCloudClass } = require('parse-server-addon-cloud-class');
const GamePoint = require('./GamePoint');
const gamePoint = new GamePoint();
ParseCloudClass.configureClass(Parse, 'GamePoint', gamePoint);
This way, every GamePoint element will run your custom code using ParseCloudClass
.
Addons
You can create your own addons or use community created ones to extend the functionality for your classes. This may open your parse-server
for new integrations. For example, using Google Analytics on afterSave
, logging errors to an error tracking service, replicating your data to any database or running your custom business logic.
// main.js
// with typescript or ES6
import { ParseCloudClass } from 'parse-server-addon-cloud-class';
// import a community created addon
import { SomeAddon } from 'some-addon-module';
// Create the instance
const myConfig = new ParseCloudClass();
// use the addon
myConfig.useAddon(SomeAddon);
// you can use any number of addons
myConfig.useAddon(SomeOtherAddon);
// Configure your class to use the configuration
ParseCloudClass.configureClass(Parse, 'MyClass', myConfig);
If you like it, give parse-server-addon-cloud-class a star 😄