-1

I am searching for a way to store conditions in mongodb, to be queried and checked, then do something as a result of the condition check.

First off here is an example of the event object that I am considering

{
  "name": "My Event",
  "created": 1490726092221,
  "startDate": 1490726092221,
  "endDate": 1490726097810,
  "notifications": [
    {
      "message": "{event.Name} Created", // message template
      "status": 0, // 0=initialized 1=failed 2=sent
      "sendDate": null, // date that the notification was sent
      "sentTo": ["c2a34dfg32c1d4583e73a123"] //members to send notification to
      "criteria": {
        "script": "event.created >= 0 && this.status < 2"
      }
    }
  ],
  "members": [
    {
      "_id": "c2a34dfg32c1d4583e73a123" // Reference to the user
    }
  ]
}

The use case, I want to have customizated notifications for an event. So if an event is scheduled it could have notifications for when it is created, when the event start date is within a few days, when a member joins etc. While I could code all of these into javascript functions and correspond to them by an enum for the notification criteria, or have hooks for when certain events happen, this seems like a strict approach.

What I am envisioning is possibly a scripting language that can be stored as a string on the document, which can be queried and evaluated, which will return a boolean to trigger the notification or not.

The script would need to have the event as an input variable, as well as a few special input variables to be available to the script.

This could be done with javascript and eval() but that scares me. Are there any other tools that can be used for this use case? Or, are there any suggestions for a better approach to this problem?

Nick Bolles
  • 439
  • 5
  • 14
  • You can store name of the js script in your document, which you can call with parameters (even the documents own fields) upon retrieving the document, criteria: { script: "someScript.js" } like `require("/../notification-scripts/" + doc.criteria.script)()` – Talha Awan Mar 28 '17 at 20:03
  • Thats a decent idea. It still requires having hard coded scripts though. my goal is to have a user-created set of notifications. Which is hard to do safely, and I may end up ditching – Nick Bolles Mar 28 '17 at 20:07

1 Answers1

0

Sounds like you are building a workflow engine with a MongoDB back end. I would start by researching things like the ones listed in this answer Workflow engine in Javascript

Community
  • 1
  • 1
Jason Livesay
  • 6,317
  • 3
  • 25
  • 31
  • Could you give some more insight into how workflow engines would be used in my use case? – Nick Bolles Mar 28 '17 at 22:58
  • You really need to read about workflow engines and understand them well. If you had done that, you would not have asked that question. So what I can do is reiterate, read in detail about workflow engines, so you do not reinvent the wheel without evening knowing wheels exist. I don't appreciate having my time wasted with another question just because you were not willing to google 'workflow engine'. So I am downvoting your question. – Jason Livesay Mar 28 '17 at 23:18
  • You entire comment back was a waste of time. I have researched it and have learned about FSM in-depth in my courses. I have read about workflow engines and I don't understand the connection to my application, so I then asked a follow up question to help guide me some more. I don't doubt your suggestion is applicable, but I just don't understand it after some research. Please don't assume what I have done before responding to a comment rudely. I may just still not understand, which is precisely what this site is for. Regardless, thank you for your answer and comments. – Nick Bolles Mar 29 '17 at 06:43