CreateFunction

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

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

String

The logical name of the function.

body

Object

The Fauna Query Language instructions to be executed.

data

Object

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 ref

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

Reference

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

name

String

The logical name of the function created.

role

String or Role ref

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

ts

Long

The timestamp associated with the creation of the function.

body

Object

The Fauna Query Language instructions to be executed.

Examples

The query below creates a user-defined function called "double".

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "create_function": {
            "object": {
              "name": "double",
              "body": {
                "query": {
                  "lambda": "x",
                  "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
                }
              }
            }
          }
        }'
// Not available in this language yet.
System.out.println(
   client.query(
      CreateFunction(
         Obj( "name", Value("double"),
            "body" , Query(Lambda(Value("x"), Add(Var("x"), Var("x"))))
         )
      )
   ).get());
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)
client.query(
   CreateFunction(
      Obj( "name", Value("double"),
         "body" , Query(Lambda(Value("x"), Add(Var("x"), Var("x"))))
      )
   )
).get();
# Not available in this language yet.
# Not available in this language yet.
// Not available in this language yet.
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))
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" } ] }
      }
    }
  }
}
{
  "ref": { "@ref": "functions/double" },
  "class": { "@ref": "functions" },
  "ts": 1509244540046619,
  "name": "double",
  "body": {
    "@query": {
      "lambda": "x",
      "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
    }
  }
}
{
  ref: ref(id = "double", collection = ref(id = "functions")),
  ts: 1527869785204010,
  name: "double",
  body: QueryV({lambda=x, expr={add=[{var=x}, {var=x}]}})
}
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]}
]
{
  "ref": { "@ref": "functions/double" },
  "class": { "@ref": "functions" },
  "ts": 1509244540046619,
  "name": "double",
  "body": {
    "@query": {
      "lambda": "x",
      "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
    }
  }
}
{
  "ref": { "@ref": "functions/double" },
  "class": { "@ref": "functions" },
  "ts": 1509244540046619,
  "name": "double",
  "body": {
    "@query": {
      "lambda": "x",
      "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
    }
  }
}
{
  "ref": { "@ref": "functions/double" },
  "class": { "@ref": "functions" },
  "ts": 1509244540046619,
  "name": "double",
  "body": {
    "@query": {
      "lambda": "x",
      "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
    }
  }
}
{
  "ref": { "@ref": "functions/double" },
  "class": { "@ref": "functions" },
  "ts": 1509244540046619,
  "name": "double",
  "body": {
    "@query": {
      "lambda": "x",
      "expr": { "add": [ { "var": "x" }, { "var": "x" } ] }
    }
  }
}
{ ref: Ref(id=double, collection=Ref(id=functions)),
  ts: 1527275011179623,
  name: 'double',
  body: Query("[object Object]") }

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!