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

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"))
)
{@set = {events: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))}}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "events": {
            "@ref": "classes/posts/233555580689580553"
          }
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "@set": {
      "events": { "@ref": "classes/posts/233555580689580553" }
    }
  }
}
result, err := client.Query(f.Events(f.RefClass(f.Collection("posts"), "233555580689580553")))

fmt.Println(result)
{map[events:{233546497131545088 0xc0001bc2e0 <nil>}]}
System.out.println(
    client.query(
        Events(Ref(Collection("posts"), "233555580689580553"))
    ).get()
);
{@set = {events: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))}}
client.query(
  q.Events(q.Ref(q.Collection('posts'), '233555580689580553'))
).then((ret) => console.log(ret))
SetRef({ events: Ref(Class("posts"), "233555580689580553") })
client.query(
  q.events(q.ref(q.collection("posts"), "233555580689580553"))
)
SetRef({'events': Ref(id=233555580689580553, class=Ref(id=posts, class=Ref(id=classes)))})
client.query(
    Events(Ref(Collection("posts"), "233555580689580553"))
)
{@set = {events: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))}}

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

client.Query(
  Paginate(Events(Ref(Collection("posts"), "233555580689580553")))
)
{
  data: [
    {
      ts: 1558994808800000,
      action: "create",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: {
        title: "What I had for breakfast..."
      }
    },
    {
      ts: 1558994916810000,
      action: "update",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: {
        tags: ["eggs", "toast"]
      }
    },
    {
      ts: 1558994945460000,
      action: "delete",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: NullV
    }
  ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "events": {
              "@ref": "classes/posts/233555580689580553"
            }
          }
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      {
        "ts": 1558994808800000,
        "action": "create",
        "resource": { "@ref": "classes/posts/233555580689580553" }
      },
      {
        "ts": 1558994916810000,
        "action": "create",
        "resource": { "@ref": "classes/posts/233555580689580553" }
      },
      {
        "ts": 1558994945460000,
        "action": "delete",
        "resource": { "@ref": "classes/posts/233555580689580553" }
      }
    ]
  }
}
result, err := client.Query(
  f.Paginate(f.Events(f.RefClass(f.Collection("posts"),
  "233555580689580553"))))

fmt.Println(result)
map[data:[map[action:create data:map[title:What I had for breakfast...] instance:{233555580689580553 0xc0001d22c0 <nil>} ts:1558994808800000] map[action:update data:map[tags:[eggs toast]] instance:{233555580689580553 0xc0001d2600 <nil>} ts:1558994916810000] map[action:delete data:{} instance:{233555580689580553 0xc0001d29a0 <nil>} ts:1558994945460000]]]
System.out.println(
    client.query(
        Paginate(Events(Ref(Collection("posts"), "233555580689580553")))
    ).get()
);
{
  data: [
    {
      ts: 1558994808800000,
      action: "create",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: {
        title: "What I had for breakfast..."
      }
    },
    {
      ts: 1558994916810000,
      action: "update",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: {
        tags: ["eggs", "toast"]
      }
    },
    {
      ts: 1558994945460000,
      action: "delete",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: NullV
    }
  ]
}
client.query(
  q.Paginate(q.Events(q.Ref(q.Collection('posts'), '233555580689580553')))
).then((ret) => console.log(ret))
{ data:
   [ { ts: 1558994808800000,
       action: 'create',
       instance: Ref(Class("posts"), "233555580689580553"),
       data: [Object] },
     { ts: 1558994916810000,
       action: 'update',
       instance: Ref(Class("posts"), "233555580689580553"),
       data: [Object] },
     { ts: 1558994945460000,
       action: 'delete',
       instance: Ref(Class("posts"), "233555580689580553"),
       data: null } ] }
client.query(
  q.paginate(q.events(q.ref(q.collection("posts"), "233555580689580553")))
)
{
  'data': [
    {
      'ts': 1558994808800000,
      'action': 'create',
      'instance': Ref(id=233555580689580553, class=Ref(id=posts, class=Ref(id=classes))),
      'data': {
        'title': 'What I had for breakfast...'
      }
    },
    {
      'ts': 1558994916810000,
      'action': 'update',
      'instance': Ref(id=233555580689580553, class=Ref(id=posts, class=Ref(id=classes))),
      'data': {
        'tags': ['eggs', 'toast']
      }
    },
    {
      'ts': 1558994945460000,
      'action': 'delete',
      'instance': Ref(id=233555580689580553, class=Ref(id=posts, class=Ref(id=classes))),
      'data': None
    }
  ]
}
client.query(
    Paginate(Events(Ref(Collection("posts"), "233555580689580553"))
)
{
  data: [
    {
      ts: 1558994808800000,
      action: "create",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: {
        title: "What I had for breakfast..."
      }
    },
    {
      ts: 1558994916810000,
      action: "update",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: {
        tags: ["eggs", "toast"]
      }
    },
    {
      ts: 1558994945460000,
      action: "delete",
      instance: ref(id = "233555580689580553",
      class = ref(id = "posts", class = ref(id = "classes"))),
      data: NullV
    }
  ]
}

The following query fetches the events while using Singleton:

client.Query(
  Paginate(
    Events(Singleton(Ref(Collection("posts"), "233555580689580553")))
  )
)
{
  data: [
    {
      ts: 1558994808800000,
      action: "add",
      instance: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))
    },
    {
      ts: 1558994945460000,
      action: "remove",
      instance: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))
    }
  ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": { "events": { "singleton": {
            "@ref": "classes/posts/233555580689580553"
          }}}
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      {
        "ts": 1558994808800000,
        "action": "create",
        "resource": { "@ref": "classes/posts/233555580689580553" }
      },
      {
        "ts": 1558994945460000,
        "action": "delete",
        "resource": { "@ref": "classes/posts/233555580689580553" }
      }
    ]
  }
}
result, err := client.Query(
  f.Paginate(
    f.Events(
      f.Singleton(
        f.RefClass(f.Collection("posts"), "233555580689580553")))))

fmt.Println(result)
map[data:[map[action:add instance:{233555580689580553 0xc0001e62c0 <nil>} ts:1558994808800000] map[action:remove instance:{233555580689580553 0xc0001e65a0 <nil>} ts:1558994945460000]]]
System.out.println(
    client.query(
        Paginate(
            Events(
                Singleton(Ref(Collection("posts"), "233555580689580553"))
            )
        )
    ).get()
);
{
  data: [
    {
      ts: 1558994808800000,
      action: "add",
      instance: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))
    },
    {
      ts: 1558994945460000,
      action: "remove",
      instance: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))
    }
  ]
}
client.query(
  q.Paginate(
    q.Events(
      q.Singleton(q.Ref(q.Collection('posts'), '233555580689580553'))
    )
  )
).then((ret) => console.log(ret))
{ data:
   [ { ts: 1558994808800000,
       action: 'add',
       instance: Ref(Class("posts"), "233555580689580553") },
     { ts: 1558994945460000,
       action: 'remove',
       instance: Ref(Class("posts"), "233555580689580553") } ] }
result = client.query(
  q.paginate(
    q.events(
      q.singleton(q.ref(q.collection("posts"), "233555580689580553"))
    )
  )
)
print(result)
{
  'data': [
    {
      'ts': 1558994808800000,
      'action': 'add',
      'instance': Ref(id=233555580689580553, class=Ref(id=posts, class=Ref(id=classes)))
    },
    {
      'ts': 1558994945460000,
      'action': 'remove',
      'instance': Ref(id=233555580689580553, class=Ref(id=posts, class=Ref(id=classes)))
    }
  ]
}
client.query(
  Paginate(
    Events(
      Singleton(Ref(Collection("posts"), "233555580689580553"))
    )
  )
)
{
  data: [
    {
      ts: 1558994808800000,
      action: "add",
      instance: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))
    },
    {
      ts: 1558994945460000,
      action: "remove",
      instance: ref(id = "233555580689580553", class = ref(id = "posts", class = ref(id = "classes")))
    }
  ]
}

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!