Key metadata

Reading or writing key definitions requires an admin key.

Problem

You need to store additional metadata within a key document.

Solution

Set the data field in a key:

try
{
    Value result = await adminClient.Query(
        Update(
            Ref(Keys(), "1"),
            Obj(
                "data", Obj(
                    "team", "Engineering"
                )
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "1", collection = RefV(id = "keys")),ts: LongV(1631728225420000),role: StringV(admin),hashed_secret: StringV($2a$05$W5HR2L6lJQ/fLllyqpvVw.knusY0BqHVYztLznZsBF8lvXqGNCE/S),data: ObjectV(team: StringV(Engineering)))
result, err := adminClient.Query(
	f.Update(
		f.Ref(f.Keys(), "1"),
		f.Obj{
			"data": f.Obj{
				"team": "Engineering",
			},
		},
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:map[team:Engineering] hashed_secret:$2a$05$6APJkVtuIG9nrNzsfnLt4e.D.wVqCLK6sQU2GkqOWov4dSrScgaXK ref:{1 0xc000103aa0 0xc000103aa0 <nil>} role:admin ts:1631728147980000]
System.out.println(
    adminClient.query(
        Update(
            Ref(Keys(), "1"),
            Obj(
                "data", Obj(
                    "team", Value("Engineering")
                )
            )
        )
    ).get());
{ref: ref(id = "1", collection = ref(id = "keys")), ts: 1631728316710000, role: "admin", hashed_secret: "$2a$05$DDEdo7/UaZmkwaz/enpFDOMljr39oHsZEIE4mZFH7cPQVOHFGGTnW", data: {team: "Engineering"}}
adminClient.query(
  q.Update(
    q.Ref(q.Keys(), '1'),
    {
      data: {
        team: 'Engineering',
      }
    },
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  ref: Ref(Keys(), "1"),
  ts: 1631728043380000,
  role: 'admin',
  hashed_secret: '$2a$05$kdsBSqgV/EoShNny0aoCMuccN94tHSqJOuTTbJ58SRIrHixS/MUhm',
  data: { team: 'Engineering' }
}
result = adminClient.query(
  q.update(
    q.ref(q.keys(), "1"),
    {
      "data": {
        "team": "Engineering"
      }
    }
  )
)
print(result)
{'ref': Ref(id=1, collection=Ref(id=keys)), 'ts': 1631728104560000, 'role': 'admin', 'hashed_secret': '$2a$05$Lo9hpVwalRvyIFbhIZbgy.Y/PEoDjAM/5RzQiVmJoJuiVic5kr/IK', 'data': {'team': 'Engineering'}}
try {
  println(Await.result(
    adminClient.query(
      Update(
        Ref(Keys(), "1"),
        Obj(
          "data" -> Obj(
            "team" -> "Engineering"
          )
        )
      )
    ),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{role: "admin", data: {team: "Engineering"}, ts: 1631728496900000, ref: ref(id = "1", collection = ref(id = "keys")), hashed_secret: "$2a$05$7g3vA01YPLLXF6jZt8oRXebd8BOokx6lMcXwz7kyc6vzKCvCvmqq6"}
Update(
  Ref(Keys(), '1'),
  {
    data: {
      team: "Engineering"
    }
  }
)
{
  ref: Ref(Keys(), "1"),
  ts: 1631727967280000,
  role: 'admin',
  hashed_secret: '$2a$05$qbGb.Xk0N.7Dnu9LP6MF7up7bgAlR1RiJyyZ4zaRXT521OWwL0Sam',
  data: { team: 'Engineering' }
}

Discussion

You can add the metadata while creating or updating a key 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!