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.
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 |
---|---|
Definition for the UDF, represented as a
|
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.
Return value
Type | Description |
---|---|
Represents a FunctionDef document that doesn’t exist. |
function.definition.exists()
Returns true
if the UDF exists. Otherwise, returns an
error with the unbound_variable
error code.
function.definition.replace()
Replaces a function definition.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
data |
Object containing valid
|
Yes |
The replacement function definition, represented as a
|
Return value
Type | Description |
---|---|
Definition for the updated function, represented as a
|
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.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
data |
Object containing valid
|
Yes |
The replacement UDF definition, represented as a
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 |
---|---|
Definition for the updated UDF, represented as a
|
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!