function.definition

We recommend you use FSL to create and update user-defined functions (UDFs). See Function schema.

Get or update a function’s definition, represented as a Function document.

Signature

<Function>.definition: FunctionDef

Description

The definition property is a user-defined functions (UDFs)'s schema, represented as a Function document. The document is an FQL definition of the FSL function schema.

Definition properties

You can use dot or bracket notation to access specific fields in the definition. See Access definition properties.

Definition methods

The definition property supports several methods for managing a function’s definition. See Methods.

Return value

Type Description

FunctionDef

Definition for the UDF, represented as a Function document.

Examples

Basic example

// Get the definition for
// the `getOrCreateCart()` UDF.
getOrCreateCart.definition
{
  name: "getOrCreateCart",
  coll: Function,
  ts: Time("2099-10-03T20:30:59.360Z"),
  body: <<-END
    (id) => {
      let customer = Customer.byId(id)!
      if (customer!.cart == null) {
        Order.create({
          status: "cart",
          customer: Customer.byId(id),
          createdAt: Time.now(),
          payment: {
          }
        })
      } else {
        customer!.cart
      }
    }
  END
}

Access definition properties

Use dot or bracket notation to access specific fields in the definition:

// Access the `body` field for
// the `getOrCreateCart()` UDF.
getOrCreateCart.definition.body
// Only returns the `body` field.
<<-END
  (id) => {
    let customer = Customer.byId(id)!
    if (customer!.cart == null) {
      Order.create({
        status: "cart",
        customer: Customer.byId(id),
        createdAt: Time.now(),
        payment: {
        }
      })
    } else {
      customer!.cart
    }
  }
END

Methods

The definition property supports several methods for managing a UDF’s definition. Using these methods is equivalent to making an unstaged schema change.

function.definition.delete()

Deletes a function definition.

Signature

delete() => NullFunctionDef

Parameters

None.

Return value

Type Description

NullFunctionDef

Represents a FunctionDef document that doesn’t exist.

Examples

getOrCreateCart.definition.delete()
Function.byName("getOrCreateCart") /* deleted */

function.definition.exists()

Returns true if the UDF exists. Otherwise, returns an error with the unbound_variable error code.

Signature

exists() => true

Parameters

None.

Return value

Type Description

true

Indicates the UDF exists.

Examples

// The `getOrCreateCart()` UDF exists.
getOrCreateCart.definition.exists()
true

For a function that doesn’t exist:

// The `Foo` UDF doesn't exist.
Foo.definition.exists()
unbound_variable

error: Unbound variable `Foo`
at *query*:1:1
  |
1 | Foo.definition.exists()
  | ^^^
  |

function.definition.replace()

Replaces a function definition.

Signature

replace(data: { *:Any }) => FunctionDef

Parameters

Parameter Type Required Description

data

Object containing valid Function document fields.

Yes

The replacement function definition, represented as a Function document. Fields not present, except immutable metadata fields, are removed.

Return value

Type Description

FunctionDef

Definition for the updated function, represented as a Function document.

Examples

getOrCreateCart.definition.replace({
  body: <<-END
    (id) => {
      let customer = Customer.byId(id)!
      if (customer!.cart == null) {
        Order.create({
          status: "cart",
          customer: Customer.byId(id),
          createdAt: Time.now(),
          payment: {
          }
        })
      } else {
        customer!.cart
      }
    }
  END
})
{
  name: "getOrCreateCart",
  coll: Function,
  ts: Time("2099-10-03T20:30:59.360Z"),
  body: <<-END
    (id) => {
      let customer = Customer.byId(id)!
      if (customer!.cart == null) {
        Order.create({
          status: "cart",
          customer: Customer.byId(id),
          createdAt: Time.now(),
          payment: {
          }
        })
      } else {
        customer!.cart
      }
    }
  END
}

function.definition.update()

Updates a UDF definition.

Signature

update(data: { *:Any }) => FunctionDef

Parameters

Parameter Type Required Description

data

Object containing valid Function document fields.

Yes

The replacement UDF definition, represented as a Function document.

During the update, fields from the object are copied to the document, creating new fields or updating existing fields. The operation is similar to a merge.

Only the fields included in the Object are updated, all other document fields aren’t updated. Fields with nested Objects in the Object are merged with the identically named nested Object in the document.

Return value

Type Description

FunctionDef

Definition for the updated UDF, represented as a Function document.

Examples

getOrCreateCart.definition.update({
  name: "fetchOrCreateCart"
})
{
  name: "fetchOrCreateCart",
  coll: Function,
  ts: Time("2099-10-04T14:12:57.660Z"),
  body: <<-END
    (id) => {
      let customer = Customer.byId(id)!
      if (customer!.cart == null) {
        Order.create({
          status: "cart",
          customer: Customer.byId(id),
          createdAt: Time.now(),
          payment: {
          }
        })
      } else {
        customer!.cart
      }
    }
  END
}

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!