Skip to content

theashraf/parse-cloud-classes

Repository files navigation

Parse Cloud Classes

npm license npm semantic-release code style: prettier

Easily extend parse cloud triggers behaviour

An easy way to extend parse cloud triggers behaviour

Installation

npm i parse-cloud-classes

Example

A working example can be found here

Supported versions

  • parse-server >= 3.0.0
  • node >= 6.4

Basic Usage

//parse cloud code file
const ParseClass = require('parse-cloud-classes')
const Joi = require('joi')

//validation service
const validation = async req => {
	const { object } = req
	const { error } = Joi.validate(object.toJSON(), validationSchema)
	if (error) throw error.details[0].message
	return req.object
}
//sendWelcomeMail service
const sendWelcomeMail = async req => {
	const { object } = req
	if (!object.existed()) {
		//send welcome email here
	}
}
//analytics service
analytics = {
	afterDelete: async req => {
		const analytic = new Parse.Object('Analytic')
		analytic.set('event', 'deleted')
		analytic.set('object', {
			className: req.object.className,
			id: req.object.id,
		})
		await analytic.save({}, { userMasterKey: true })
	},
	afterSave: async req => {
		const analytic = new Parse.Object('Analytic')
		analytic.set('event', 'created')
		analytic.set('object', {
			className: req.object.className,
			id: req.object.id,
		})
		await analytic.save({}, { userMasterKey: true })
	},
}

new ParseClass(Parse.User)
	.beforeSave([validation])
	.afterSave([analytics.afterSave, sendWelcomeMail])
	.afterDelete([analytics.afterDelete])
	.beforeDelete([
		async req => {
			if (!req.master) throw 'unauthorized'
			return req.object
		},
	])

Development setup

npm install && npm run dev

Contribution

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/fooBar), or hotfix branch (git checkout -b hotfix/fooBar)
  3. Commit your changes (npm run cz)
  4. Push to the feature branch (git push origin feature/fooBar), or hotfix branch (git push origin hotfix/fooBar)
  5. Create a new Pull Request

License

This project is licensed under the MIT License, See LICENSE for more information

About

Easily extend parse cloud triggers behaviour

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published