Parse-server: Implement global triggers

Created on 31 Jul 2019  路  9Comments  路  Source: parse-community/parse-server

There are situations where it's necessary to be able to execute functions globally.
For example beforeSave, afterSave, etc.

So what I propose is that things such as "*" can be implemented in the class name.

Parse.Cloud.beforeSave("*", (request) => {
    // Do some cool stuffs
}
feature

Most helpful comment

you could do this with inheritance today. You could make a base class that extends ParseObject and then all of your classes can extend your base class. Your base class can implement a before save and after save that does your common stuff. If you want something specific for a class, implement its beforeSave method and be sure to call super.beforeSave(request).

All 9 comments

I don't see any immediate use case for this, beyond maybe logging. Can you provide some details for your use case?

I'm currently tracking createdBy and updatedBy fields in beforeSave triggers. I have to implement it for each class declared in cloud code.
Another's API frameworks implement this kind of "hooks". For this reason, could be a great idea. It's just a simple proposal.

you could do this with inheritance today. You could make a base class that extends ParseObject and then all of your classes can extend your base class. Your base class can implement a before save and after save that does your common stuff. If you want something specific for a class, implement its beforeSave method and be sure to call super.beforeSave(request).

Perfect! Could you provide me an example?
I'll be implementing your solution!

@santiagosemhan I don't have an example to point you to. My response was just from thinking about it and how I would approach it. But I'd be glad to help you with it if you'd like. I would start by figuring out how to get it working with two different classes, so something like this:

  1. create a base class that extends Parse.Object
  2. put a 'beforeSave' function in it and have it console.log('greetings from base class').
  3. create two classes that extend your base class: SubClassA and SubClassB
  4. register those two classes to map to a table in parse. See how to extend Parse.Object in the docs for how to do that.
  5. in one of the two subclasses, implement a beforeSave function and make sure to call super.beforeSave() in that function. In the other subclass, don't bother with a beforeSave to prove that this works
  6. register the before saves like Parse.Cloud.beforeSave('SubClassA', SubClassA.beforeSave) and then the same thing for SubClassB: Parse.Cloud.beforeSave('SubClassB', SubClassB.beforeSave)

That should do it. When you save an instance of SubClassA, it'll call its beforeSave, which will then do any class-specific stuff but also will call super.beforeSave. In SubClassB where you have no class-specific beforeSave code, the super.beforeSave will just be called as a result of inheritance.

If you try it and have code-level questions, I can help you here, or if you are game to put a minimal example in a public GitHub repo, I'll look at it and help you with how to get it running and tested and all that good stuff, which is surprisingly easy but not well documented (maybe this could be the start ;).

When you have a working sample, put the answer on StackOverflow and I'll upvote it :=)

and for the record, I don't think that it would be a bad thing to change the hook implementation to allow a hook function to target more than one class using something like a regex and/or a list to match which classes to apply a hook too. While we're wishing, it would be nice to allow more than one 'beforeSave' observer per class too, but I am not prepared to do that work any time soon myself. So either you give it a try (which I'd help you with and others might too) or in the meantime, you may want to try my suggestion which does require some work, but will work today.

@acinader I did it as you proposed and works as expected. So, if I have time I will post the solution in stackoverflow. But, in other hands, I think that would be good to document this stuffs into the cloud code section.

great. we'd also welcome a pull request on the documentation front if you think you could improve for this case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LtrDan picture LtrDan  路  4Comments

ViolentCrumble picture ViolentCrumble  路  3Comments

jiawenzhang picture jiawenzhang  路  4Comments

mohmagdy picture mohmagdy  路  3Comments

shardul08 picture shardul08  路  3Comments