Migrating v4 UDF using Events to v10

Hi, I’ve got a working system which I need to migrate to use v10 UDF before the EOL in June.
I have the following UDF, which returns the number of times a document has been changed (requiring history_days to not be set):

q.Query(
        q.Lambda(
            ["id"],
            q.Count(
                q.Events(
                    q.Select("ref",
                        q.Get(q.Ref(q.Collection("Foo"), q.Var("id"))
                        )
                    )
                )
            )
        )
    )

Is there any way to do this in the new v10 syntax.
In terms of documentation I can only find the At to be able to query at a specific time, and EventSources which look to be built for a very different use case. Is there any way to just get a list of all the history objects?
Thanks,

Hi @JamesD and welcome! :wave:

To work with the list of events in your collections using FQL v10, you will need to use the Event Feeds feature. In your client, paginate your events and keep track of the number.

Using the JS driver, for example:

    const source_query = fql`
      let doc = MyCollection.byId("420816285734010946")
      Set.single(doc).eventSource()
    `

    const tenMinutesAgoInMicroseconds =
      (Date.now() - 10 * 60 * 1000) * 1000
    const feed = client.feed(source_query, {
      start_ts: tenMinutesAgoInMicroseconds,
    })

    const events = await Array.fromAsync(feed.flatten())
    const count = events.length

    console.log({ count })

Here are some additional resources:

Thank you ptpaterson,
I may be misunderstanding you, but I don’t think this is what I am looking for. I just want one fql query which tells me how may times the document has been edited. I shouldn’t have to pull all the events and then sum over them in javascript right? Surely I can calculate that in the database and just get a number back. I would be very surprised if this functionality is no longer available in the new version.
Thank you.

Hi @JamesD. I’ve worked with our engineers and product team to make sure we’ve gotten you the correct solution.

It is not possible to enumerate and count events within an FQL v10 query (i.e. using the query API). You will need to use the feed API to do so. The example I provided demonstrates how to use the JS driver to connect to the feed API.

I just want one fql query which tells me how may times the document has been edited. I shouldn’t have to pull all the events and then sum over them

I definitely appreciate that this is a lot of boilerplate to fold over events and obtain the count. Please note that even with the v4 query API, Fauna has to pull all of the events and tally the count linearly when executing Count(Events(...)). We built the feed API to serve as the backbone for a wider range of change data capture (CDC) features we are working on and that are not possible with the v4 APIs. Clearly, it is not optimized for a simple count of events, but it is the method available.