{"id":20759833,"url":"https://github.com/owsas/parse-cloud-class","last_synced_at":"2025-04-30T05:21:12.514Z","repository":{"id":37271518,"uuid":"109433357","full_name":"owsas/parse-cloud-class","owner":"owsas","description":"Extendable way to set up Parse Cloud classes behaviour","archived":false,"fork":false,"pushed_at":"2023-01-03T15:17:34.000Z","size":1034,"stargazers_count":41,"open_issues_count":17,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-15T04:49:41.389Z","etag":null,"topics":["addon","cloud","code","javascript","parse","parse-server"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/owsas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-03T19:09:46.000Z","updated_at":"2023-10-02T06:19:43.000Z","dependencies_parsed_at":"2023-02-01T07:00:44.222Z","dependency_job_id":null,"html_url":"https://github.com/owsas/parse-cloud-class","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/owsas%2Fparse-cloud-class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/owsas%2Fparse-cloud-class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/owsas%2Fparse-cloud-class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/owsas%2Fparse-cloud-class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/owsas","download_url":"https://codeload.github.com/owsas/parse-cloud-class/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251645915,"owners_count":21620838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["addon","cloud","code","javascript","parse","parse-server"],"created_at":"2024-11-17T10:08:29.054Z","updated_at":"2025-04-30T05:21:12.493Z","avatar_url":"https://github.com/owsas.png","language":"TypeScript","funding_links":["https://patreon.com/owsas"],"categories":[],"sub_categories":[],"readme":"# REPO MOVED\nThis repository has been moved to Otherwise's new monorepo :) https://github.com/owsas/opensource/tree/master/packages/parse-cloud-class Enjoy! \n\n# Parse Cloud Class \n\n![Travis](https://travis-ci.org/owsas/parse-cloud-class.svg?branch=master) [![codecov](https://codecov.io/gh/owsas/parse-cloud-class/branch/master/graph/badge.svg)](https://codecov.io/gh/owsas/parse-cloud-class)\n\n![Logo](./repo/logo.jpg)  \nPhoto by [chuttersnap](https://unsplash.com/photos/9AqIdzEc9pY?utm_source=unsplash\u0026utm_medium=referral\u0026utm_content=creditCopyText) on [Unsplash](https://unsplash.com)\n\nTravis tests: https://travis-ci.org/owsas/parse-cloud-class/builds \n\nA new way to define Parse.Cloud events for your classes (DB tables). With this module you can easily:\n\n* Define minimum values for keys on your classes\n* Define maximum values for keys on your classes\n* Define default values\n* Define required keys\n* Define immutable keys (only editable with the master key)\n* Use addons to easily extend the functionality of your app\n* Create new addons and share them with the community\n* Customize the default behaviour to your own needs\n\nThis module is meant to be used with [Parse](http://docs.parseplatform.org/) and [Parse Server](https://github.com/parse-community/parse-server)\n\n## Installation\n`\u003e npm install --save parse-server-addon-cloud-class parse`\n\n__Typescript__: This module comes bundled with Intellisense :)\n\nAfter installing, please make sure to install also `parse\u003e1.11.0`\n\n## Example\nA working example can be found here: https://github.com/owsas/parse-cloud-class-example\n\n## Supported versions\n* Parse \u003e1.10.0\n* Parse \u003e=2.0\n* Parse \u003e=3.0\n\n## New: Configuration with objects\nStarting april 2019 (v1.1.0), it's possible to create classes with configuration objects\n\nExample:\n```js\nconst ParseCloudClass = require('parse-server-addon-cloud-class').ParseCloudClass;\n\n// Create a new configuration object to define the class behaviour.\n// All attributes are optional\nconst gamePoint = {\n  requiredKeys: ['points'], // all objects saved must have the points attribute\n  defaultValues: { points: 20 }, // by default, all new objects will have 20 points (if it was not set at the time of creation) \n  minimumValues: { points: 10 }, // minimum 10 points\n  maximumValues: { points: 1000 }, // maximum 1000 points\n  immutableKeys: ['points'], // once set, the points can't be changed (only master can do that)\n  beforeFind: function(req) {\n    // Do something here\n    return req.query;\n  },\n  processBeforeSave: async function(req) {\n    // Do something here\n    return req.object;\n  },\n  afterSave: async function(req) {\n    // Do something here\n    return req.object;\n  },\n  processBeforeDelete: async function(req) {\n    // Do something here\n    return req.object;\n  },\n  afterDelete: async function(req) {\n    // Do something here\n    return req.object;\n  }\n}\n\n// Create an instance\nconst gamePointClass = ParseCloudClass.fromObject(gamePoint);\n\n// Configure the class in the main.js cloud file\nParseCloudClass.configureClass(Parse, 'GamePoint', gamePointClass);\n```\n\nAs 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`.\n\nWith 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.\n\n## Basic Usage\n```js\n/*\n* This is the main cloud file for Parse\n* cloud/main.js\n*/\n\n// with normal ES5\nconst ParseCloudClass = require('parse-server-addon-cloud-class').ParseCloudClass;\n\n// with typescript or ES6\nimport { ParseCloudClass } from 'parse-server-addon-cloud-class';\n\nconst myConfig = new ParseCloudClass({\n  // New items will not be created if they have no 'name' set\n  requiredKeys: ['name'],\n  \n  defaultValues: {\n    // All new items will have active: true\n    active: true,\n    // By default, timesShared will be 0\n    timesShared: 0,\n  },\n\n  minimumValues: {\n    // timesShared cannot go below 0\n    timesShared: 0,\n  },\n\n  // Keys that are only editable by the master key.\n  // Trying to edit apiKey without the master key will throw an error\n  immutableKeys: ['apiKey'],\n});\n\n// Configure your class to use the configuration\nParseCloudClass.configureClass(Parse, 'MyClass', myConfig);\n```\n\nWhen you configure your classes to work with ParseCloudClass, they will be attached the following events\n* `beforeFind` \n* `beforeSave`\n* `beforeDelete`\n* `afterSave`\n* `afterDelete`\n\nBy default, the only event that is going to do something is the `beforeSave`, that is going to check the `minimumValues`, `defaultValues` and `requiredKeys`\n\n## Extending ParseCloudClass\n\nYou 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:\n* `processBeforeSave`: Here you would define your custom behaviour for `beforeSave`\n* `processBeforeDelete`: Here you would define your custom behaviour for `beforeDelete`\n\n```js\n// myCustomFile.js\nimport { ParseCloudClass } from 'parse-server-addon-cloud-class';\n\nexport class MyCustomClass extends ParseCloudClass {\n  /*\n  * Here you can define your custom minimumValues, \n  * defaultValues and requiredKeys\n  */\n  requiredKeys = ['title']\n\n  /**\n  * @param req {Parse.Cloud.BeforeSaveRequest}\n  */\n  async processBeforeSave(req) {\n    // Make sure the super class validates the required keys,\n    // minimum values, executes the addons, etc\n    const object = await super.processBeforeSave(req);\n\n    // write your own code here\n    ....\n\n    // make sure to return req.object\n    return object;\n  }\n}\n```\n\nYou 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.\n\n### Decorators\n\nParse Cloud Class comes with two decorators that you may use in your own applications. Please keep in mind that you must activate `enableExperimentalDecorators`.\n\n#### requireLogin decorator\nIt requires all `beforeSave` and `beforeDelete` requests to be made by a registered user or by the master key\n\n\n\n#### requireKey decorator\nIt pushes required keys to the given class when it is initialized\n\nExample:\n\n```ts\n@requireKey('myRequiredKey')\nexport default class MyClass extends ParseClass {\n}\n```\n\nThis is different from defining the required keys in the class' body, because\nin that way the previously set required keys would be overriden.\n\nExample: \n\n``` ts\ndefault class MyClass extends ParseClass {\n  public requiredKeys: string[] = ['a', 'b']\n}\n\ndefault class MyOtherClass extends MyClass {\n  public requiredKeys: string[] = ['c'] // 'a', 'b' are not set anymore\n}\n\n// With requireKey:\n@requireKey('c')\nexport default class MyOtherClass2 extends MyClass {\n  // requiredKeys are 'a', 'b', 'c'\n}\n```\n\n### All the possibilities\n\n```ts\ninterface IParseCloudClass {\n\n  beforeFind(\n    req: Parse.Cloud.BeforeFindRequest,\n  ): Parse.Query;\n\n  processBeforeSave (\n    req: Parse.Cloud.BeforeSaveRequest | IProcessRequest,\n  ): Promise\u003cParse.Object\u003e;\n\n  beforeSave(\n    req: Parse.Cloud.BeforeSaveRequest | IProcessRequest,\n    // parse sdk \u003e 2.0 does not have the res parameter\n    res?: Parse.Cloud.BeforeSaveResponse | IProcessResponse,\n  ): Promise\u003cboolean\u003e;\n\n  afterSave (\n    req: Parse.Cloud.BeforeDeleteRequest | IProcessRequest,\n  ): Promise\u003cParse.Object\u003e;\n\n  processBeforeDelete (\n    req: Parse.Cloud.BeforeDeleteRequest | IProcessRequest,\n  ): Promise\u003cParse.Object\u003e;\n\n  beforeDelete(\n    req: Parse.Cloud.BeforeDeleteRequest | IProcessRequest,\n    // parse sdk \u003e 2.0 does not have the res parameter\n    res?: Parse.Cloud.BeforeDeleteResponse | IProcessResponse,\n  ): Promise\u003cboolean\u003e;\n\n  afterDelete (\n    req: Parse.Cloud.BeforeDeleteRequest | IProcessRequest,\n  ): Promise\u003cParse.Object\u003e;\n\n}\n```\n\nNote: IProcessRequest is an interface that allows you to do testing\n\n```ts\ninterface IProcessRequest {\n  object: Parse.Object;\n  user?: Parse.User;\n  master?: boolean;\n}\n```\n\n\n## Using addons\n\nTo use an addon, you would first import it, and then configure your class\nto use that addon. Example:\n\n```js\n// with typescript or ES6\nimport { ParseCloudClass } from 'parse-server-addon-cloud-class';\nimport { SomeAddon } from 'some-addon-module';\n\nconst myConfig = new ParseCloudClass();\n\n// use the addon\nmyConfig.useAddon(SomeAddon);\n\n// you can use any number of addons\nmyConfig.useAddon(SomeOtherAddon);\n\n// Configure your class to use the configuration\nParseCloudClass.configureClass(Parse, 'MyClass', myConfig);\n```\n\nTake into account that addons are executed in the order in which they were added.\n\n## Creating addons\n\nAddons can be created by extending ParseCloudClass and defining new behaviours on:\n* `beforeFind` \n* `beforeSave`\n* `beforeDelete`\n* `afterSave`\n* `afterDelete`\n* `processBeforeSave`\n* `processBeforeDelete`\n\n### Example addon:\n\n```js\n// In Javascript\nclass Addon1 extends ParseCloudClass {\n  async processBeforeSave(req) {\n    req.object.set('addon1', true);\n    return req.object;\n  }\n}\n```\n\n```ts\n// In Typescript\nclass Addon1 extends ParseCloudClass {\n  async processBeforeSave(req: Parse.Cloud.BeforeSaveRequest) {\n    req.object.set('addon1', true);\n    return req.object;\n  }\n}\n```\n\nNow you can also create addons using the new configuration objects, for example:\n\n```js\nconst dbAddon = {\n  afterSave: async function(req) {\n    // replicate data to the other db\n    return req.object;\n  },\n  afterDelete: async function(req) {\n    // replicate data to the other db\n    return req.object;\n  }\n}\n\nconst addonInstance = ParseCloudClass.fromObject(dbAddon);\n```\n\n\n## Addons\n\n* Algolia Search: https://github.com/owsas/parse-server-addon-cloud-algolia \n\n\n## Credits\n\nDeveloped by Juan Camilo Guarín Peñaranda,  \nOtherwise SAS, Colombia  \n2017\n\n## License \n\nMIT.\n\n## Support us on Patreon\n[![patreon](./repo/patreon.png)](https://patreon.com/owsas)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fowsas%2Fparse-cloud-class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fowsas%2Fparse-cloud-class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fowsas%2Fparse-cloud-class/lists"}