Token metadata

Problem

Store additional metadata within a token document.

Solution

Set the data field in a token:

try
{
    Value result = await client.Query(
        Update(
            Ref(Tokens(), "1"),
            Obj(
                "data", Obj(
                    "team", "Marketing"
                )
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "1", collection = RefV(id = "tokens")),ts: LongV(1631740130800000),instance: RefV(id = "1", collection = RefV(id = "users", collection = RefV(id = "collections"))),data: ObjectV(team: StringV(Marketing)))
result, err := client.Query(
	f.Update(
		f.Ref(f.Tokens(), "1"),
		f.Obj{
			"data": f.Obj{
				"team": "Marketing",
			},
		},
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:map[team:Marketing] instance:{1 0xc000200660 0xc000200660 <nil>} ref:{1 0xc0002004b0 0xc0002004b0 <nil>} ts:1631739856660000]
System.out.println(
    client.query(
        Update(
            Ref(Tokens(), "1"),
            Obj(
                "data", Obj(
                    "team", Value("Marketing")
                )
            )
        )
    ).get());
{ref: ref(id = "1", collection = ref(id = "tokens")), ts: 1631740302150000, instance: ref(id = "1", collection = ref(id = "users", collection = ref(id = "collections"))), data: {team: "Marketing"}}
client.query(
  q.Update(
    q.Ref(q.Tokens(), '1'),
    {
      data: {
        team: 'Marketing',
      },
    },
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  ref: Ref(Tokens(), "1"),
  ts: 1631739422930000,
  instance: Ref(Collection("users"), "1"),
  data: { team: 'Marketing' }
}
result = client.query(
  q.update(
    q.ref(q.tokens(), "1"),
    {
      "data": {
        "team": "Marketing"
      }
    }
  )
)
print(result)
{'ref': Ref(id=1, collection=Ref(id=tokens)), 'ts': 1631739701090000, 'instance': Ref(id=1, collection=Ref(id=users, collection=Ref(id=collections))), 'data': {'team': 'Marketing'}}
try {
  println(Await.result(
    client.query(
      Update(
        Ref(Tokens(), "1"),
        Obj(
          "data" -> Obj(
            "team" -> "Marketing"
          )
        )
      )
    ),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{ref: ref(id = "1", collection = ref(id = "tokens")), ts: 1631740710610000, instance: ref(id = "1", collection = ref(id = "users", collection = ref(id = "collections"))), data: {team: "Marketing"}}
Update(
  Ref(Tokens(), '1'),
  {
    data: {
      team: "Marketing"
    }
  }
)
{
  ref: Ref(Tokens(), "1"),
  ts: 1631738655590000,
  instance: Ref(Collection("users"), "1"),
  data: { team: 'Marketing' }
}

Discussion

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

More information

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!