# Feature Request: Register JSONata function

**URL:** <https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381>\
**Category:** Feature Requests\
**Created:** [29 January 2019 21:27 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381 "2019-01-29T21:27:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [29 January 2019 21:27 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/1 "2019-01-29T21:27:28Z")

</div>

I would like to be able to register JSONata functions. My use case: I want a simple wrapper around momment.js available in my JSONata expressions ... I can see how being able to register a JSONata function could make a good plugin/extension point for Node-Red.

I'm proposing three additions:

- `RED.util.registerJSONataFunction (name, function, signature="", snipt="")`
- `RED.util.removeJSONataFunction (name)`
- `RED.util.getJSONataFunctions()` returns array of objects that have the functions and the meta-data for the functions for use in JSONata expressions

prepareJSONataExpression would have to be updated to call getJSONataFunctions (or maybe a helper function). Ideally the expr.assign and expr.registerFunction function calls in prepareJSONataExpression would be removed and registerJSONataFunction would be called for flowContext, globalContext, env & clone.

Notes:

- I am assuming that in a follow up we would want update the JSONata editor to do a call a service to get the snipts for the registered functions so that they show along with the other functions in the editor
- It might make sense if "removeJSONataFunction" replaced the function with a dummy function that logged an error ("JSONata function {name} has been removed") and updated the snipt so that it said something like "This function has been been removed." ... next time node red started it would be gone.

I would be willing to work on a pull request for this, and would be happy with any guidance or feedback.

---

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [20 February 2019 19:42 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/2 "2019-02-20T19:42:46Z")

</div>

Any feed back on this? I would be willing to work on a pull request for this, but I would probably need a bit of guidance on how best to add state to the `RED.util` object/namespace.

Thanks in advance for any feedback!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 February 2019 21:00 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/3 "2019-02-20T21:00:26Z")

</div>

Hi @dustinw

how do you envisage this being used in general? Would node modules register the functions alongside any nodes they register? Would they be defined statically - ie as part of registering the module into the runtime - or that they could be added/removed dynamically whilst the flow is running?

If it's the static approach, where would `removeJSONataFunction` get used? At what point in the node lifecycle?

Or would these functions be registered through settings.js - much as you can add things to the Function node via functionGlobalContext. That would reduce the portability of flows that used custom functions - as they would rely on things in the settings file that don't get exported with the flow.

I think there is an interesting idea to pursue here - just need to explore how we'd expect it to be used which will help define the specific apis needed to support it.

I'm on vacation the rest of this week; I'll be online a bit, but replies may be a bit limited until next week.

---

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [20 February 2019 22:03 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/4 "2019-02-20T22:03:58Z")

</div>

> [@knolleary](#):
>
> Would node modules register the functions alongside any nodes they register? Would they be defined statically - ie as part of registering the module into the runtime ...

That would be the primary use-case ... example: i.e. install the node-red-contrib-moment module and it installs some nodes and some JSONata functions. Uninstall that module and the additional JSONata functions go away.

> [@knolleary](#):
>
> Or would these functions be registered through settings.js - much as you can add things to the Function node via functionGlobalContext.

I would like that to also be an option for advance users ... example: while I am waiting for my patch to be accepted to the node-red-contrib-moment module, I call the registerJSONataFunction in my settings.js file.

I might be missing something, but I think that would require an additional change: I don't see any events being emitted in the run-time during the start method that could be subscribed to (idealy we could subscribe to an on start event and an on Flowsloaded event).

> [@knolleary](#):
>
> If it's the static approach, where would `removeJSONataFunction` get used? At what point in the node lifecycle?

I haven't fully reviewed the module lifecycle yet. I was trying to line it up to the design work in [Design: Node module lifecycle · node-red/node-red Wiki · GitHub](https://github.com/node-red/node-red/wiki/Design:-Node-module-lifecycle) ... so in the module's remove function once it's available.

Thanks for your time! I hope you have a great vacation. I am looking forward to getting into this further once you have time.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [20 February 2019 22:39 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/5 "2019-02-20T22:39:16Z")

</div>

Are there security concerns that need to be considered here - If these are globally registered functions - who can register them ? Will there be name collisions ? Can someone maliciously redefine a function ?

---

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [21 February 2019 17:05 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/6 "2019-02-21T17:05:50Z")

</div>

> [@dceejay](#):
>
> If these are globally registered functions - who can register them ? Will there be name collisions ? Can someone maliciously redefine a function ?

Great questions!

Regarding who can register them ... are there any controls on similar functions like "RED.httpAdmin.get/post/" & remove? It would be a similar risk level to those functions. I was assuming at this point that any nodes/modules added, and any code in the settings.js file would have to be trusted.

As for name collisions ... that would definitely be an issue. There are number of ways to handle it: I was originally assuming that we would either ignore any additional calls to registerJSONataFunction with the same Name (maybe logging a "$name is already defined" type warning), or just keep overwriting the same key with the updated function, but other options might be requiring some a module level prefix, or adding an incrementing suffix to the function name.

---

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [7 March 2019 17:29 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/7 "2019-03-07T17:29:56Z")

</div>

Would it make sense for me to start a pull request for this? We could further discuss this in the context of a pull request.

Thanks!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 March 2019 17:42 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/8 "2019-03-07T17:42:56Z")

</div>

Hi @dustinw - I think we still need to iterate on the design and requirements before we starting reviewing any code changes.

We've being putting into place a bit more of a formalised way for design discussions around new features. The goal being to keep discussions in one place and help us to track what is being worked on that perhaps hasn't reached the point of having a code PR.

If you're willing to help test the process, please raise a PR against [https://github.com/node-red/designs](https://github.com/node-red/designs) with a new proposal - the readme explains what to do. We can then use that PR to review and iterate on the proposal before any code is written.

---

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [7 March 2019 18:05 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/9 "2019-03-07T18:05:44Z")

</div>

Sure thing. That makes sense. I'll create a proposal following that process.

---

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [7 March 2019 18:54 UTC](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381/10 "2019-03-07T18:54:47Z")

</div>

Minor suggestion: You should consider prefixing the repo name ... i.e. [https://github.com/node-red/node-red-designs](https://github.com/node-red/node-red-designs).
