CreateFunction

// Not available in this language yet.
CreateFunction( param_object )
CreateFunction( param_object )
CreateFunction( param_object )
# Not available in this language yet.
// Not available in this language yet.

Description

The CreateFunction operation adds a new user-defined function with the specified parameters.

Parameters

Argument Type Definition and Requirements

param_object

Object

The param_object fields are described below.

param_object

Field Name Field Type Definition and Requirements

name

The logical name of the function.

body

The Fauna Query Language instructions to be executed.

data

Optional - This is user-defined metadata for the user-defined function. It is provided for the developer to store information at the function level.

role

String or Role Reference

Optional - Specifies the role that should be used when the user-defined function is called, which would typically be used to provide privilege escalation when current privileges would otherwise be too restrictive.

To use a built-in role, specify one of the strings admin, server, server-readonly, client. To use a user-defined role, specify a Role reference, e.g. Role("admin").

role can only be set by users with a privileged role, such as admin, server, or a user-defined role that grants write privilege for functions.

Use role carefully! Setting role to admin, server, or any other privileged role gives your function permission to create/modify/remove any documents when invoked by any caller. Users that can write functions can adjust role to change the function’s privileges.

Returns

An object containing the metadata of CreateFunction operations.

Field Name Field Type Definition and Requirements

ref

The reference is an automatically-generated, unique identifier within the database to identify the function that was created.

name

The logical name of the function created.

role

String or Role Reference

The role that should be used when the function is called. Only included when role is specified in param_object.

ts

The timestamp, with microsecond resolution, associated with the creation of the function.

body

The Fauna Query Language instructions to be executed.

Examples

The following query creates a user-defined function called "double":

// Not available in this language yet.
{
  "ref": { "@ref": "functions/double" },
  "class": { "@ref": "functions" },
  "ts": 1509244540046619,
  "name": "double",
  "body": {
    "@query": {
      "lambda": "x",
      "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
    }
  }
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "create_function": {
            "object": {
              "name": "double",
              "body": {
                "query": {
                  "lambda": "x",
                  "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
                }
              }
            }
          }
        }'
HTTP/1.1 201 Created
{
  "resource": {
    "ref": { "@ref": "functions/double" },
    "class": { "@ref": "functions" },
    "ts": 1509244540046619,
    "name": "double",
    "body": {
      "@query": {
        "lambda": "x",
        "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
      }
    }
  }
}
result, _ := client.Query(
    f.CreateFunction(f.Obj{
        "name": "double",
        "body": f.Query(f.Lambda("x", f.Add(f.Var("x"), f.Var("x")))),
    }),
)

fmt.Println(result)
map[
  ref:{double 0xc4202310a0 <nil>}
  ts:1509244540046619
  body:{[123 34 108 97 109 98 100 97 34 58 34 120 34 44 34 101 120 112 114 34 58 123 34 97 100 100 34 58 91 123 34 118 97 114 34 58 34 120 34 125 44 123 34 118 97 114 34 58 34 120 34 125 93 125 125]}
]
System.out.println(
   client.query(
      CreateFunction(
         Obj( "name", Value("double"),
            "body" , Query(Lambda(Value("x"), Add(Var("x"), Var("x"))))
         )
      )
   ).get()); 
{
  ref: ref(id = "double", collection = ref(id = "functions")),
  ts: 1527869785204010,
  name: "double",
  body: QueryV({lambda=x, expr={add=[{var=x}, {var=x}]}})
}
client.query(
  q.CreateFunction({
    name: 'double',
    body: q.Query(
      q.Lambda('x', q.Add(q.Var('x'), q.Var('x')))
    ),
  })
)
.then((ret) => console.log(ret))
{ ref: Ref(id=double, collection=Ref(id=functions)),
  ts: 1527275011179623,
  name: 'double',
  body: Query("[object Object]") }
# Not available in this language yet.
{
  "ref": { "@ref": "functions/double" },
  "class": { "@ref": "functions" },
  "ts": 1509244540046619,
  "name": "double",
  "body": {
    "@query": {
      "lambda": "x",
      "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
    }
  }
}
client.query(
   CreateFunction(
      Obj( "name", Value("double"),
         "body" , Query(Lambda(Value("x"), Add(Var("x"), Var("x"))))
      )
   )
).get(); 
{
  "ref": { "@ref": "functions/double" },
  "class": { "@ref": "functions" },
  "ts": 1509244540046619,
  "name": "double",
  "body": {
    "@query": {
      "lambda": "x",
      "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
    }
  }
}

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!