Create a function

Creating and updating a function requires:

Problem

You need to create a user-defined function in the current database.

Solution

Use the CreateFunction function:

Let’s create a function to allow authors to create new blog posts. We’ll set it up to allow the author to provide a title and body. This way authors are constrained by what data they can store on a post document.

Copied!
client.query(
  q.({
    name: 'create_post',
    body: q.(
      q.(
        ['title', 'body'],
        q.(
          q.('posts'),
          {
            data: {
              title: q.('title'),
              body: q.('body'),
            },
          },
        ),
      )
    ),
  })
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  ref: ("create_post"),
  ts: 1622572663690000,
  name: 'create_post',
  body: ((["title", "body"], (("posts"), {"data": {"title": ("title"), "body": ("body")}})))
}
Query metrics:
  •    bytesIn:  227

  •   bytesOut:  327

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    1

  •  readBytes:   24

  • writeBytes:  455

  •  queryTime: 40ms

  •    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!