Function code changes after save

Hi. When I create function via web dashboard, this part (this is only extract of the whole function):

...
            Merge(
              Select(["data"], Var("ad")),
              {
                favouriteBy: null,
                favourite: ContainsValue(Ref(Collection("users"), Var("userId")), Select(["data", "favouriteBy"], Var("ad")))
              },
              Lambda(
                ['key', 'a', 'b'],
                If(
                  Not(Equals(Var("key"), "favouriteBy")),
                  Var("b"),
                  null
                )
              )
            )
...

is saved as

...
            Lambda(
              Lambda(
                ["key", "a", "b"],
                If(Not(Equals(Var("key"), "favouriteBy")), Var("b"), null)
              ),
              undefined
            )
...

See how Merge got converted to weird looking Lambda? Why is that?

Funny thing is that when called, the function works well (as intended in my original pre-saved version).

Hi Petr,

This is an issue in the JavaScript driver (which is what the dashboard uses) when rendering the Merge expression. We’re working on a fix for it but can’t say when it will be available. But as you’ve observed, this only effects how the function is displayed and not its operation.

Is there anything else we can do for you or should we mark this post as Solved?

Cory

Thank you for explanation, Cory.

Just one more question. Is there a way to get the current function code which was saved?

Because now I have to keep a copy of the function aside which is quite impractical.

Since functions are stored with a reference just like any other structure in Fauna, you can use Get() to retrieve the details of the function. See our docs on the Function() built-in for more details but here’s a simple example using FQL:

Get(Function('yourFunction'))

Well unfortunately, calling that in webshell returns the wrong code as well (I’d guess it’s the same issue with JS driver as you described).

What is stranger is that calling q.Get(q.Function('yourFunction')) via Node.js JS driver (npm package) returns correct code. But it is returned in some kind of internal format and not the one I’d be able to easily copy&edit&paste.

So, is Node.js JS driver different from the one used in dashboard? And is there another way to get to a textual representation of my code?

q.Get(q.Function('yourFunction')) by itself returns an Expr class instance, which is made up of more Expr's. But what you see when you console.log or view the values in the console windows is the pretty, formated representation of the FQL query. It appears the bug is simply not printing the query properly, even though the underlying data is correct.

Until the bug is fixed you cannot copy-paste the printed output from the js driver or the web shell (because the web shell used the js driver).

This is not helpful for rapid prototyping in the shell, but I prefer, and strongly recommend to everybody, that all definitions be saved in (revision controlled) source code with a script that can bootstrap/update the database when changes are necessary. Until the bug is fixed, you might write a nodejs script to update things as required.

Some examples and discussion. There’s also fauna-schema-migrate to use or just be inspired by.

Like I said this does not help in the shell. It’s all work-around. Even though I’ve been working through various ways to store db definitions in code, I am constantly in the shell playing with ideas, so the bug is a bummer.

Thanks, ptpaterson. That’s exactly to the point what I’m struggling with now! :slight_smile:

I do have DB initialization script and that’s certainly a great approach, but I do struggle with aforementioned fast prototyping, which is what I do several times before I decide to save final version of my function in the initialization script.

Anyway, thanks for detailed explanation. Keeping my fingers crossed for you to fix it quickly.

1 Like