Events

Events( input )
Events( input )
Events( input )
Events( input )
events( input )
Events( input )

Description

The Events function returns an array of events for the provided reference. These events represent the history of the reference, and demonstrate when the reference was created, modified, or removed.

The kinds of events vary based on the kind of reference provided:

Singular references
  • create

  • delete

  • update

Set references
  • add

  • remove

Since documents are stored immutably within Fauna, events are stored as snapshots of the associated document. Directly manipulating document history, using the Insert and Remove functions, does not currently affect subsequent event snapshots.

Parameter

Argument Type Definition and Requirements

input

A Set of references (or SetRef), or a singular reference.

Returns

A set containing the creation, modification, or deletion events related to the provided input. Each event in the set contains the following fields:

Field Name Field Type Definition and Requirements

action

The action applied to the instance during the event.

data

Optional: only provided for documents containing user-supplied data. The data involved in the event.

instance

The reference identifies the document associated with the event.

ts

The timestamp, with microsecond resolution, associated with the event.

Examples

The following query fetches the events for a regular document:

client.Query(
    Events(Ref(Collection("posts"), "233555580689580553"))
);
SetRefV(System.Collections.Generic.Dictionary`2[System.String,FaunaDB.Types.Value])
result, err := client.Query(
	f.Events(f.Ref(f.Collection("posts"), "233555580689580553")))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
{map[events:{233555580689580553 0xc000192180 0xc000192180 <nil>}]}
System.out.println(
    client.query(
        Events(Ref(Collection("posts"), "233555580689580553"))
    ).get()
);
{@set = {events: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections")))}}
client.query(
  q.Events(q.Ref(q.Collection('posts'), '233555580689580553'))
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
Events(Ref(Collection("posts"), "233555580689580553"))
result = client.query(
  q.events(q.ref(q.collection("posts"), "233555580689580553"))
)
print(result)
SetRef({'events': Ref(id=233555580689580553, collection=Ref(id=posts, collection=Ref(id=collections)))})
client.query(
  Events(Ref(Collection("posts"), "233555580689580553"))
)
{@set = {events: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections")))}}

And again, using Paginate to see the details of each event:

client.Query(
    Paginate(Events(Ref(Collection("posts"), "233555580689580553")))
);
ObjectV(data: Arr(ObjectV(ts: LongV(1603756294080000),action: StringV(create),document: RefV(id = "233555580689580553", collection = RefV(id = "posts", collection = RefV(id = "collections"))),data: ObjectV(title: StringV(What I had for breakfast ..))), ObjectV(ts: LongV(1603756294110000),action: StringV(update),document: RefV(id = "233555580689580553", collection = RefV(id = "posts", collection = RefV(id = "collections"))),data: ObjectV(tags: Arr(StringV(eggs), StringV(toast)))), ObjectV(ts: LongV(1603756294140000),action: StringV(delete),document: RefV(id = "233555580689580553", collection = RefV(id = "posts", collection = RefV(id = "collections"))),data: NullV)))
result, err := client.Query(
	f.Paginate(f.Events(f.Ref(f.Collection("posts"), "233555580689580553"))))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:[map[action:create data:map[title:What I had for breakfast ..] document:{233555580689580553 0xc00009a240 0xc00009a240 <nil>} ts:1603747163140000] map[action:update data:map[tags:[eggs toast]] document:{233555580689580553 0xc00009a450 0xc00009a450 <nil>} ts:1603747163170000] map[action:delete data:{} document:{233555580689580553 0xc00019a0f0 0xc00019a0f0 <nil>} ts:1603747163190000]]]
System.out.println(
    client.query(
        Paginate(Events(Ref(Collection("posts"), "233555580689580553")))
    ).get()
);
{data: [{ts: 1593464840120000, action: "create", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections"))), data: {title: "What I had for breakfast .."}}, {ts: 1593464840150000, action: "update", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections"))), data: {tags: ["eggs", "toast"]}}, {ts: 1593464840170000, action: "delete", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections"))), data: null}]}
client.query(
  q.Paginate(q.Events(q.Ref(q.Collection('posts'), '233555580689580553')))
)
.then((ret) => console.log(util.inspect(ret, { depth: null })))
.catch((err) => console.error('Error: %s', err))
{
  data: [
    {
      ts: 1592269279120000,
      action: 'create',
      document: Ref(Collection("posts"), "233555580689580553"),
      data: { title: 'What I had for breakfast ..' }
    },
    {
      ts: 1592269279150000,
      action: 'update',
      document: Ref(Collection("posts"), "233555580689580553"),
      data: { tags: [ 'eggs', 'toast' ] }
    },
    {
      ts: 1592269279170000,
      action: 'delete',
      document: Ref(Collection("posts"), "233555580689580553"),
      data: null
    }
  ]
}
result = client.query(
  q.paginate(q.events(q.ref(q.collection("posts"), "233555580689580553")))
)
print(result)
{'data': [{'ts': 1592939385260000, 'action': 'create', 'document': Ref(id=233555580689580553, collection=Ref(id=posts, collection=Ref(id=collections))), 'data': {'title': 'What I had for breakfast ..'}}, {'ts': 1592939385290000, 'action': 'update', 'document': Ref(id=233555580689580553, collection=Ref(id=posts, collection=Ref(id=collections))), 'data': {'tags': ['eggs', 'toast']}}, {'ts': 1592939385310000, 'action': 'delete', 'document': Ref(id=233555580689580553, collection=Ref(id=posts, collection=Ref(id=collections))), 'data': None}]}
client.query(
  Paginate(Events(Ref(Collection("posts"), "233555580689580553")))
)
{data: [{ts: 1594487123210000, action: "create", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections"))), data: {title: "What I had for breakfast .."}}, {ts: 1594487123240000, action: "update", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections"))), data: {tags: ["eggs", "toast"]}}, {ts: 1594487123260000, action: "delete", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections"))), data: NullV}]}

The following query fetches the events while using Singleton:

client.Query(
    Paginate(
        Events(Singleton(Ref(Collection("posts"), "233555580689580553")))
    )
);
ObjectV(data: Arr(ObjectV(ts: LongV(1603756294080000),action: StringV(add),document: RefV(id = "233555580689580553", collection = RefV(id = "posts", collection = RefV(id = "collections")))), ObjectV(ts: LongV(1603756294140000),action: StringV(remove),document: RefV(id = "233555580689580553", collection = RefV(id = "posts", collection = RefV(id = "collections"))))))
result, err := client.Query(
	f.Paginate(
		f.Events(
			f.Singleton(
				f.Ref(f.Collection("posts"), "233555580689580553")))))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:[map[action:add document:{233555580689580553 0xc0000915c0 0xc0000915c0 <nil>} ts:1603747163140000] map[action:remove document:{233555580689580553 0xc0000917a0 0xc0000917a0 <nil>} ts:1603747163190000]]]
System.out.println(
    client.query(
        Paginate(
            Events(
                Singleton(Ref(Collection("posts"), "233555580689580553"))
            )
        )
    ).get()
);
{data: [{ts: 1593464840120000, action: "add", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections")))}, {ts: 1593464840170000, action: "remove", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections")))}]}
client.query(
  q.Paginate(
    q.Events(
      q.Singleton(q.Ref(q.Collection('posts'), '233555580689580553'))
    )
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  data: [
    {
      ts: 1592269043990000,
      action: 'add',
      document: Ref(Collection("posts"), "233555580689580553")
    },
    {
      ts: 1592269044060000,
      action: 'remove',
      document: Ref(Collection("posts"), "233555580689580553")
    }
  ]
}
result = client.query(
  q.paginate(
    q.events(
      q.singleton(q.ref(q.collection("posts"), "233555580689580553"))
    )
  )
)
print(result)
{'data': [{'ts': 1592939385260000, 'action': 'add', 'document': Ref(id=233555580689580553, collection=Ref(id=posts, collection=Ref(id=collections)))}, {'ts': 1592939385310000, 'action': 'remove', 'document': Ref(id=233555580689580553, collection=Ref(id=posts, collection=Ref(id=collections)))}]}
client.query(
  Paginate(
    Events(
      Singleton(Ref(Collection("posts"), "233555580689580553"))
    )
  )
)
{data: [{ts: 1594487123210000, action: "add", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections")))}, {ts: 1594487123260000, action: "remove", document: ref(id = "233555580689580553", collection = ref(id = "posts", collection = ref(id = "collections")))}]}

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!