FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, see the v4 EOL announcement and migration guide. Contact support@fauna.com with any questions.

Function

This reference topic applies to FQL v4. Go to this page for the latest FQL v10 reference topics.

Function( name, [database] )
function( name, [database] )
Function( name )
ScopedFunction( name, database )
Function( name, [database] )
Function( name, [database] )

Description

The Function function returns a Reference for the specified function name in the specified child database. If a child database is not specified, the returned function reference belongs to the current database.

When a child database is specified, an admin key must be used to call Function (server or client keys, or ABAC tokens, cannot be used to access a child database).

Also, Go does not support optional function parameters, nor function overloading. To specify a child database using the Go driver, call the ScopedFunction function instead of Function.

Parameters

Parameter Type Definition and Requirements

name

String

The name of a function.

database

Reference

Optional - A Reference to a child database. If not specified, the current database is used.

A Reference to a child database.

Optional - A Reference to a child database. If not specified, the current database is used.

Optional - A Reference to a child database. If not specified, the current database is used.

Optional - A Reference to a child database. If not specified, the current database is used.

Optional - A Reference to a child database. If not specified, the current database is used.

Optional - A Reference to a child database. If not specified, the current database is used.

Returns

A reference to a function with the specified name, in the specified child database (or the current database if database is not specified).

Examples

  1. The following query gets a reference to the function named increment in the current database:

    client.query(
      q.Function('increment')
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    Function("increment")
    result = client.query(
      q.function("increment")
    )
    print(result)
    Ref(id=increment, collection=Ref(id=functions))
    result, err := client.Query(
    	f.Function("increment"))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    {increment 0xc00014c090 0xc00014c090 <nil>}
    try
    {
        Value result = await client.Query(
            Function("increment")
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    RefV(id = "increment", collection = RefV(id = "functions"))
    Function('increment')
    Function("increment")
    Query metrics:
    •    bytesIn:  24

    •   bytesOut:  81

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:   0

    • writeBytes:   0

    •  queryTime: 8ms

    •    retries:   0

  2. The following query gets a Reference to the function named increment in a child database named "child_db":

    client.query(
      q.Function('increment', q.Database('child_db'))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    Function("increment", Database("child_db"))
    result = client.query(
      q.function("increment", q.database("child_db"))
    )
    print(result)
    Ref(id=increment, collection=Ref(id=functions), database=Ref(id=child_db, collection=Ref(id=databases)))
    result, err := client.Query(
    	f.ScopedFunction("increment", f.Database("child_db")))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    {increment 0xc000184180 0xc000184180 0xc0001841e0}
    try
    {
        Value result = await client.Query(
            Function("increment", Database("child_db"))
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    RefV(id = "increment", collection = RefV(id = "functions"), database = RefV(id = "child_db", collection = RefV(id = "databases")))
    Function('increment', Database('child_db'))
    Function("increment", Database("child_db"))
    Query metrics:
    •    bytesIn:  56

    •   bytesOut: 160

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes: 155

    • writeBytes:   0

    •  queryTime: 9ms

    •    retries:   0

  3. The following query demonstrates how to retrieve the function document by its reference:

    client.query(
      q.Get(q.Function('increment'))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    { ref: Function("increment"),
      ts: 1581379483580000,
      name: 'increment',
      body: Query(Lambda("X", Add(Var("X"), 1))) }
    result = client.query(
      q.get(q.function("increment"))
    )
    print(result)
    {'ref': Ref(id=increment, collection=Ref(id=functions)), 'ts': 1603752408980000, 'name': 'increment', 'body': Query({'api_version': '4', 'lambda': 'X', 'expr': {'add': [{'var': 'X'}, 1]}})}
    result, err := client.Query(
    	f.Get(f.Function("increment")))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[body:{[123 34 97 112 105 95 118 101 114 115 105 111 110 34 58 34 52 34 44 34 108 97 109 98 100 97 34 58 34 88 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 88 34 125 44 49 93 125 125]} name:increment ref:{increment 0xc0001441e0 0xc0001441e0 <nil>} ts:1608756885420000]
    try
    {
        Value result = await client.Query(
            Get(Function("increment"))
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    ObjectV(ref: RefV(id = "increment", collection = RefV(id = "functions")),ts: LongV(1603756293670000),name: StringV(increment),body: QueryV(System.Collections.Generic.Dictionary`2[System.String,FaunaDB.Query.Expr]))
    Get(Function('increment'))
    {
      ref: Function("increment"),
      ts: 1624310406060000,
      name: 'increment',
      body: Query(Lambda("X", Add(Var("X"), 1)))
    }
    Query metrics:
    •    bytesIn:  32

    •   bytesOut: 212

    • computeOps:   1

    •    readOps:   1

    •   writeOps:   0

    •  readBytes: 101

    • writeBytes:   0

    •  queryTime: 5ms

    •    retries:   0

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!