All Projects → owsas → parse-cloud-class

owsas / parse-cloud-class

Licence: MIT license
Extendable way to set up Parse Cloud classes behaviour

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to parse-cloud-class

Picasso
一款sketch生成代码插件,可将sketch设计稿自动解析成前端代码。
Stars: ✭ 191 (+377.5%)
Mutual labels:  parse, code
godot-engine.code-snapshot
A plugin for Godot Engine which will let you take beautified snapshots of your code within the Editor. Configure the frame as you like, with GDScript syntax already highlighted.
Stars: ✭ 32 (-20%)
Mutual labels:  code, addon
parse-react
[EXPERIMENTAL] React, React Native, and React with SSR (e.g. Next.js) packages to interact with Parse Server backend
Stars: ✭ 58 (+45%)
Mutual labels:  parse, parse-server
Parse Comments
Parse JavaScript code comments. Works with block and line comments, and should work with CSS, LESS, SASS, or any language with the same comment formats.
Stars: ✭ 53 (+32.5%)
Mutual labels:  parse, code
opensource
Collection of Open Source packages by Otherwise
Stars: ✭ 21 (-47.5%)
Mutual labels:  parse, parse-server
parse-server-test-runner
A tool for programmatically starting Parse Server
Stars: ✭ 18 (-55%)
Mutual labels:  parse, parse-server
ParseCareKit
Securely synchronize any CareKit 2.1+ based app to a Parse Server Cloud. Compatible with parse-hipaa.
Stars: ✭ 28 (-30%)
Mutual labels:  parse, parse-server
ConductOfCode
Code examples for the blog
Stars: ✭ 15 (-62.5%)
Mutual labels:  code
Hacktoberfest-2021
An Open Source repository to Teach people How to contribute to open sources.
Stars: ✭ 98 (+145%)
Mutual labels:  code
djangocms-style
django CMS Style is a plugin for django CMS that allows you to create a HTML container containing classes, styles, ids and other attributes.
Stars: ✭ 39 (-2.5%)
Mutual labels:  addon
laravel-blueprint-pestphp-addon
A PestPHP addon for Laravel Shift's Blueprint
Stars: ✭ 30 (-25%)
Mutual labels:  addon
ember-formly
JavaScript powered forms for Ember
Stars: ✭ 24 (-40%)
Mutual labels:  addon
codemirror-colorpicker
colorpicker with codemirror
Stars: ✭ 113 (+182.5%)
Mutual labels:  addon
Splain
small parser to create more interesting language/sentences
Stars: ✭ 15 (-62.5%)
Mutual labels:  parse
SwiftyCodeView
Fully customizable UI Component for verification codes written in swift with RxSwift support!
Stars: ✭ 86 (+115%)
Mutual labels:  code
ax-editor
Ax is a code editor with syntax highlighting that runs in your terminal written completely in Swift.
Stars: ✭ 42 (+5%)
Mutual labels:  code
Bijou.js
Bijou.js: Useful JavaScript snippets in one simple library
Stars: ✭ 30 (-25%)
Mutual labels:  code
kaleidoscope
🍀 A small collection of creative nodes to generate color palette and store values for Blender
Stars: ✭ 99 (+147.5%)
Mutual labels:  addon
eval-estree-expression
Safely evaluate JavaScript (estree) expressions, sync and async.
Stars: ✭ 22 (-45%)
Mutual labels:  parse
wow-api-docs
An in-game graphical browser for Blizzard's API Documentation
Stars: ✭ 16 (-60%)
Mutual labels:  addon

REPO MOVED

This repository has been moved to Otherwise's new monorepo :) https://github.com/owsas/opensource/tree/master/packages/parse-cloud-class Enjoy!

Parse Cloud Class

Travis codecov

Logo
Photo by chuttersnap on Unsplash

Travis tests: https://travis-ci.org/owsas/parse-cloud-class/builds

A new way to define Parse.Cloud events for your classes (DB tables). With this module you can easily:

  • Define minimum values for keys on your classes
  • Define maximum values for keys on your classes
  • Define default values
  • Define required keys
  • Define immutable keys (only editable with the master key)
  • Use addons to easily extend the functionality of your app
  • Create new addons and share them with the community
  • Customize the default behaviour to your own needs

This module is meant to be used with Parse and Parse Server

Installation

> npm install --save parse-server-addon-cloud-class parse

Typescript: This module comes bundled with Intellisense :)

After installing, please make sure to install also parse>1.11.0

Example

A working example can be found here: https://github.com/owsas/parse-cloud-class-example

Supported versions

  • Parse >1.10.0
  • Parse >=2.0
  • Parse >=3.0

New: Configuration with objects

Starting april 2019 (v1.1.0), it's possible to create classes with configuration objects

Example:

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);

As you see, instead of defining beforeSave, we use processBeforeSave. This is because ParseCloudClass uses the beforeSave function to wrap up some extra logic that we may not want to rewrite each time. In the same fashion, we use processBeforeDelete.

With this new functionality, the this keyword inside the beforeFind, processBeforeSave, afterSave, processBeforeDelete and beforeDelete functions refers to the instance itself, which means you can access for example this.requiredKeys, etc.

Basic Usage

/*
* This is the main cloud file for Parse
* cloud/main.js
*/

// with normal ES5
const ParseCloudClass = require('parse-server-addon-cloud-class').ParseCloudClass;

// with typescript or ES6
import { ParseCloudClass } from 'parse-server-addon-cloud-class';

const myConfig = new ParseCloudClass({
  // New items will not be created if they have no 'name' set
  requiredKeys: ['name'],
  
  defaultValues: {
    // All new items will have active: true
    active: true,
    // By default, timesShared will be 0
    timesShared: 0,
  },

  minimumValues: {
    // timesShared cannot go below 0
    timesShared: 0,
  },

  // Keys that are only editable by the master key.
  // Trying to edit apiKey without the master key will throw an error
  immutableKeys: ['apiKey'],
});

// Configure your class to use the configuration
ParseCloudClass.configureClass(Parse, 'MyClass', myConfig);

When you configure your classes to work with ParseCloudClass, they will be attached the following events

  • beforeFind
  • beforeSave
  • beforeDelete
  • afterSave
  • afterDelete

By default, the only event that is going to do something is the beforeSave, that is going to check the minimumValues, defaultValues and requiredKeys

Extending ParseCloudClass

You can easily extend ParseCloudClass in order to define your custom behaviours. In this case, you must have into account the following two extra methods of a ParseCloudClass:

  • processBeforeSave: Here you would define your custom behaviour for beforeSave
  • processBeforeDelete: Here you would define your custom behaviour for beforeDelete
// myCustomFile.js
import { ParseCloudClass } from 'parse-server-addon-cloud-class';

export class MyCustomClass extends ParseCloudClass {
  /*
  * Here you can define your custom minimumValues, 
  * defaultValues and requiredKeys
  */
  requiredKeys = ['title']

  /**
  * @param req {Parse.Cloud.BeforeSaveRequest}
  */
  async processBeforeSave(req) {
    // Make sure the super class validates the required keys,
    // minimum values, executes the addons, etc
    const object = await super.processBeforeSave(req);

    // write your own code here
    ....

    // make sure to return req.object
    return object;
  }
}

You can change the implementation of any method to your needs, but please, call the super class' processBeforeSave if you expect to have requiredKeys checking, minimum values checking, addon functionalities, etcetera.

Decorators

Parse Cloud Class comes with two decorators that you may use in your own applications. Please keep in mind that you must activate enableExperimentalDecorators.

requireLogin decorator

It requires all beforeSave and beforeDelete requests to be made by a registered user or by the master key

requireKey decorator

It pushes required keys to the given class when it is initialized

Example:

@requireKey('myRequiredKey')
export default class MyClass extends ParseClass {
}

This is different from defining the required keys in the class' body, because in that way the previously set required keys would be overriden.

Example:

default class MyClass extends ParseClass {
  public requiredKeys: string[] = ['a', 'b']
}

default class MyOtherClass extends MyClass {
  public requiredKeys: string[] = ['c'] // 'a', 'b' are not set anymore
}

// With requireKey:
@requireKey('c')
export default class MyOtherClass2 extends MyClass {
  // requiredKeys are 'a', 'b', 'c'
}

All the possibilities

interface IParseCloudClass {

  beforeFind(
    req: Parse.Cloud.BeforeFindRequest,
  ): Parse.Query;

  processBeforeSave (
    req: Parse.Cloud.BeforeSaveRequest | IProcessRequest,
  ): Promise<Parse.Object>;

  beforeSave(
    req: Parse.Cloud.BeforeSaveRequest | IProcessRequest,
    // parse sdk > 2.0 does not have the res parameter
    res?: Parse.Cloud.BeforeSaveResponse | IProcessResponse,
  ): Promise<boolean>;

  afterSave (
    req: Parse.Cloud.BeforeDeleteRequest | IProcessRequest,
  ): Promise<Parse.Object>;

  processBeforeDelete (
    req: Parse.Cloud.BeforeDeleteRequest | IProcessRequest,
  ): Promise<Parse.Object>;

  beforeDelete(
    req: Parse.Cloud.BeforeDeleteRequest | IProcessRequest,
    // parse sdk > 2.0 does not have the res parameter
    res?: Parse.Cloud.BeforeDeleteResponse | IProcessResponse,
  ): Promise<boolean>;

  afterDelete (
    req: Parse.Cloud.BeforeDeleteRequest | IProcessRequest,
  ): Promise<Parse.Object>;

}

Note: IProcessRequest is an interface that allows you to do testing

interface IProcessRequest {
  object: Parse.Object;
  user?: Parse.User;
  master?: boolean;
}

Using addons

To use an addon, you would first import it, and then configure your class to use that addon. Example:

// with typescript or ES6
import { ParseCloudClass } from 'parse-server-addon-cloud-class';
import { SomeAddon } from 'some-addon-module';

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);

Take into account that addons are executed in the order in which they were added.

Creating addons

Addons can be created by extending ParseCloudClass and defining new behaviours on:

  • beforeFind
  • beforeSave
  • beforeDelete
  • afterSave
  • afterDelete
  • processBeforeSave
  • processBeforeDelete

Example addon:

// In Javascript
class Addon1 extends ParseCloudClass {
  async processBeforeSave(req) {
    req.object.set('addon1', true);
    return req.object;
  }
}
// In Typescript
class Addon1 extends ParseCloudClass {
  async processBeforeSave(req: Parse.Cloud.BeforeSaveRequest) {
    req.object.set('addon1', true);
    return req.object;
  }
}

Now you can also create addons using the new configuration objects, for example:

const dbAddon = {
  afterSave: async function(req) {
    // replicate data to the other db
    return req.object;
  },
  afterDelete: async function(req) {
    // replicate data to the other db
    return req.object;
  }
}

const addonInstance = ParseCloudClass.fromObject(dbAddon);

Addons

Credits

Developed by Juan Camilo Guarín Peñaranda,
Otherwise SAS, Colombia
2017

License

MIT.

Support us on Patreon

patreon

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].