Function metadata

Creating and updating a function requires:

Problem

You need to store additional metadata within the function document.

Solution

Set the data field in a function:

try
{
    Value result = await client.Query(
        Update(
            Function("create_post"),
            Obj("data", Obj("env", "test"))
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "create_post", collection = RefV(id = "functions")),ts: LongV(1635293366450000),name: StringV(create_post),body: QueryV(System.Collections.Generic.Dictionary`2[System.String,FaunaDB.Query.Expr]),data: ObjectV(env: StringV(test)))
result, err := adminClient.Query(
	f.Update(
		f.Function("create_post"),
		f.Obj{"data": f.Obj{"env": "test"}},
	))

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 91 34 116 105 116 108 101 34 44 34 98 111 100 121 34 44 34 97 117 116 104 111 114 34 93 44 34 101 120 112 114 34 58 123 34 99 114 101 97 116 101 34 58 123 34 99 111 108 108 101 99 116 105 111 110 34 58 34 112 111 115 116 115 34 125 44 34 112 97 114 97 109 115 34 58 123 34 111 98 106 101 99 116 34 58 123 34 100 97 116 97 34 58 123 34 111 98 106 101 99 116 34 58 123 34 97 117 116 104 111 114 34 58 123 34 118 97 114 34 58 34 97 117 116 104 111 114 34 125 44 34 98 111 100 121 34 58 123 34 118 97 114 34 58 34 98 111 100 121 34 125 44 34 116 105 116 108 101 34 58 123 34 118 97 114 34 58 34 116 105 116 108 101 34 125 125 125 125 125 125 125]} data:map[env:test] name:create_post ref:{create_post 0xc000109c80 0xc000109c80 <nil>} ts:1635293376660000]
adminClient.query(
  q.Update(
    q.Function('create_post'),
    { data: { env: 'test' } }
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  ref: Function("create_post"),
  ts: 1635293426560000,
  name: 'create_post',
  body: Query(Lambda(["title", "body", "author"], Create(Collection("posts"), {"data": {"title": Var("title"), "body": Var("body"), "author": Var("author")}}))),
  data: { env: 'test' }
}
result = adminClient.query(
  q.update(
    q.function("create_post"),
    {"data": {"env": "test"}}
  )
)
print(result)
{'ref': Ref(id=create_post, collection=Ref(id=functions)), 'ts': 1635293430960000, 'name': 'create_post', 'body': Query({'api_version': '4', 'lambda': ['title', 'body', 'author'], 'expr': {'create': {'collection': 'posts'}, 'params': {'object': {'data': {'object': {'title': {'var': 'title'}, 'body': {'var': 'body'}, 'author': {'var': 'author'}}}}}}}), 'data': {'env': 'test'}}
Update(
  Function('create_post'),
  { data: { env: 'test' } }
)
{
  ref: Function("create_post"),
  ts: 1635293600520000,
  name: 'create_post',
  body: Query(Lambda(["title", "body", "author"], Create(Collection("posts"), {data: {title: Var("title"), body: Var("body"), author: Var("author")}}))),
  data: { env: 'test' }
}
Query metrics:
  •    bytesIn:   92

  •   bytesOut:  384

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    1

  •  readBytes:  513

  • writeBytes:  246

  •  queryTime: 26ms

  •    retries:    0

Discussion

You can add the metadata while creating or updating a function document.

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!