Storing Date Recurrence

Hello! I’m hoping for some advice on how to best work with recurring dates. In my app, I’ve got the concept of things that occur with a specific frequency (think calendar settings - day of week, intervals, etc.). I’m planning on using https://www.npmjs.com/package/rrule to help me manage this - it takes some options and can generate a list of dates that fall inside the “set” of rules you define, so that you can easily tell if a given date has an occurrence of the event.

Obviously I can’t use that lib in FQL, but I’d really like to handle the logic in UDFs anyway - basically I need to calculate whether or not a set of documents have a recurrence that means they occur on a specific date.

I’m contemplating creating a collection with a unique key that is the value of rrule’s toString method, and the value is the array of dates from its all. That way I could pull in the list of dates with an index for the recurrence string and do my date calculations (checking if a date was in the array). I’d essentially be caching the value of a recurrence as a document, and then pulling it up when needed. And, anytime I created a new recurrence rule, I’d need to add it to the cache. They’re idempotent, so I’d never need to edit them once added. The recurrence would never be more than a single year, so never more than 365 entries in the array.

I guess my question is - is this crazy? Would the performance impact of that lookup be too much to be used in a UDF that gets called with some frequency? Anyone else have a different idea of how to approach this? Thanks for any help!

That doesn’t sound too crazy to me.

A possible alternative could be to store store the key:values in the UDF as an object and Select them. This would be efficient if the values do not need to be updated often (like once a day for the entire app world of users).

Did you know that a UDF can update UDFs? So, one idea might be to somehow run UDF on an interval that would update that UDF, or tack it on to some other function.

Did you know that UDFs can update themselves?!! If we’re looking for possibly crazy, but efficient then you can try to pull that off.

As a proof of concept

Multiple calls to the ‘test’ function here, result in 1, 2, 3, ...

// test body
Query(
  Lambda(
    [],
    Let(
      {
        value: 1,
        result: Var("value"), // some logic working on stored date
        updatedFn: If(
          conditionsAreRight,
          Call(Function("updateTest"), Add(Var("value"), 1)),
          false
        )
      },
      Var("result")
    )
  )
)
// updateTest body
Query(
  Lambda(
    "newValue",
    Update(Function("test"), {
      body: Query(
        Lambda(
          [],
          Let(
            {
              value: Var("newValue"),
              result: Var("value"), 
              updatedFn: If(
                conditionsAreRight,
                Call(Function("updateTest"), Add(Var("value"), 1)),
                false
              )
            },
            Var("result")
          )
        )
      )
    })
  )
)

Wow, that’s a really intriguing idea! I hadn’t thought of storing the values inside the UDF itself. I suppose the best/only way to do that is within the Let binding as you showed above? And then you can Select the values from the binding. Definitely a promising approach.

1 Like