Store function as value in document

Making it possible to store directly the reference to a function or the function itself as a part of the value in a document

{
  cmsFunction: Function.byName("activate_numberOfShareholdersQuestions")
}

or

{
  sell: (document) => {}
}

Fauna passes the reference to the document automatically to the function if the function includes the parameter “document” (like in ABAC)

(document) => {
 // activate_numberOfShareholdersQuestions
}

Use Case 1:

We have CMS(Content management system) functions specific to single documents. With that change it would become easier working with functions in the context of documents.

Instead of doing this as today:

Document (Collection Answer)

{
  cmsFunction:"activate_numberOfShareholdersQuestions"
}

Function activate_numberOfShareholdersQuestions

() => {
  let document = Answer.byId("56546545546456564") // hardcoded reference to the connected document
  
}

Client

let cmsFunction = Function.byName(Answer.byId("56546545546456564").cmsFunction)

We could simplify it like that

Document (Collection Answer)

{
  cmsFunction: Function.byName("activate_numberOfShareholdersQuestions")
}

Function activate_numberOfShareholdersQuestions

(document ) => {
  // no hardcoded reference needed  
}

Client

Answer.byId("56546545546456564").cmsFunction()

Use Case 2

Scoped Functions - Keeps functions organized and structured in a scaled environment.

e.g.

Car.byId("34223424").sell()
Car.sell()

Are you trying to store arbitrary lambdas, or do you want the document to be passed in as a hidden “self” argument? I think the two would be very different implementations.

For example, maybe we could allow instance methods to be defined on the Collection itself, rather than store lambdas in each document. what do you think about that?

If we permit storing lambda in a document field, it likely wouldn’t have any special access to the document. I don’t think we want to get into Javascript this territory :stuck_out_tongue: especially if we are able to defined methods on the Collection.

The 2nd one (passing the document as a “self” argument). And inside, making it possible to call arbitrary lambdas if needed. I think with that, we get the best of both worlds.

(document) => {
 callArbitraryFunction()
}

The response I left here may also be relevant: Domains/Namespaces