Support wildcard className in Cloud triggers

I gather that Parse Server cloud functions don’t support wildcard className? At least I can’t find in the docs.

Parse.Cloud.beforeSave("Review", (request) => { ...  })

In my application I have dozens, eventually hundreds of classes. Most of these require some business logic on save. (eg search indexing key fields, checking object relationships, etc)

Currently it seems I have to manually define lists of classes to map classes to callbacks. This is terrible frustrating, slow and fragile/hard to maintain as the application grows.

//- config.js
const classes = [ 'A', 'B', 'C' ]
//- callback.js
config.classes()
  .forEach(className => {
    initialize(className)
  })

function initialize (className) {
  Parse.Cloud.beforeSave(className, async (request) => {
    //- do something useful...
  })
}

I would much prefer to use business logic to dynamically/programmatically decide if a save function needs to be called … eg:

Parse.Cloud.beforeSave("*", (request) => {
   if (!someNecessaryCondition) return
   //- do some application logic
})

This way, I have much simpler and robust way to determine whether a call back applies. In my case, so far, have to map 18 classes to 8 different callbacks and it’s already becoming hard to maintain … and we haven’t launched yet :wink:

Thoughts?

2 Likes

This idea makes sense to me. Would you be willed to work on a PR regarding it?

I believe we have to change here right? İf so i guess this will do the job

if (!applicationId) {
    throw 'Missing ApplicationID';
}
const trigger = get(Category.Triggers, `${triggerType}.${className}`, applicationId);
if (trigger) return trigger;
return get(Category.Triggers, `${triggerType}.*`, applicationId);

Edit: I created a PR for this. See #7360

1 Like

Just an idea, did you try to add a save callback to Parse.Object ?

Parse.cloud.beforeSave(Parse.Object, ..... )

For reference, the issue in which the proper approach to implement wildcard class triggers has been discussed is Support wildcard classname in cloud triggers · Issue #7361 · parse-community/parse-server · GitHub.

It seems to be a straight forward path to extend Cloud Functions to accept a regex as class name. If anyone wants to make a PR, I estimate it to be around 2h work incl. testing.

1 Like