Faunadb how do I paginate an index sorted by Timestamp

720 views Asked by At

I created an index on faunadb to sort by the time stamp in order to get the most recent items first, and I am trying to retrieve 100 items at a time. My problem is that when I enter the "after" parameter from the result, I receive the same results as the initial query.

This is the index I created:

CreateIndex({
  name: "all_school_queries",
  source: Collection('<school_queries_reversed>'),
  values:   values: [
     {
       field: ["ts", {reverse: true}]
     },
     {
       field: ["ref"]
     }
  ]
})

This is how I am querying the database:

    Map(
      Paginate(Match(Index("school_query_reverse")), {
        after: [ Ref(Collection("collection_name") ,'collection ref from first query')],
      }),
    
      Lambda(
        ['ts',"ref"], 
        Get(Var("ref"))
      ) 
    )

and this is the first result:

{
  before: [Ref(Collection("collection_name"), "275484304279077376")],
  after: [
    1598907150720000,
    Ref(Collection("school_queries"), "12345"),
    Ref(Collection("school_queries"), "12345")
  ],
}

I have used both the timestamp for the after, 1598907150720000 and the ref, 12345. I tried the console first to make sure I could get the right response, but upon entering the either result from the after, I get the same result.

2

There are 2 answers

0
Brecht De Rooms On

I'll try to answer your question (I'm the dev adv at FaunaDB). I have to say that I'm quite confused by your question due to syntax that doesn't seem to make sense to me, so I apologize if it's not the answer you are looking for.

Things that I'm confused by.

  • The index syntax is wrong, did you copy this somewhere or did you rewrite this manually? If you copied it somewhere then we might display it wrongly so do let me know if that's the case. The index name does not match the name you are using so I assume this is a typo.
  • <school_queries_reversed>, reversed in the collection name doesn't seem to make sense to me since reverse is defined on the index, not on the collection.

Doesn't matter though, I tried to reproduce your issue, since I don't have an idea how the data looks I kept it simple. enter image description here

The index I used looks as follows:

CreateIndex({
   name: "all_school_queries",
    source: Collection('school_queries'),
    values: [
     {
       field: ["ts"], 
       reverse: true
     },
     {
       field: ["ref"]
     }
  ]
})

If I then query this index as follows:

Map(
  Paginate(Match(Index("all_school_queries")), {size: 1}),   
  Lambda(
     ['ts',"ref"], 
     Get(Var("ref"))
  )
)

I do get the last element I added first (reverse index)

{
  after: [
    1599220462170000,
    Ref(Collection("school_queries"), "275735235372515847"),
    Ref(Collection("school_queries"), "275735235372515847")
  ],
  data: [
    {
      ref: Ref(Collection("school_queries"), "275735244842205703"),
      ts: 1599220471200000,
      data: {
        query: "bli"
      }
    }
  ]
}

and when I use the returned after cursor to get the next page (I have specified pages of only one element here):

Map(
      Paginate(Match(Index("all_school_queries")), {size: 1, after: [
        1599220462170000,
        Ref(Collection("school_queries"), "275735235372515847"),
        Ref(Collection("school_queries"), "275735235372515847")
      ]}),
    
      Lambda(
        ['ts',"ref"], 
        Get(Var("ref"))
      )
)

I do get (as expected) the other element.


{
  before: [
    1599220462170000,
    Ref(Collection("school_queries"), "275735235372515847"),
    Ref(Collection("school_queries"), "275735235372515847")
  ],
  data: [
    {
      ref: Ref(Collection("school_queries"), "275735235372515847"),
      ts: 1599220462170000,
      data: {
        query: "bla"
      }
    }
  ]
}

Is that not working for you?

0
Tw1t611 On

I had the same problem and created a forum post on how to deal with it.

https://forums.fauna.com/t/filter-by-timestamp-with-gql-resolver/3302

I guess most people are missing

  • paginated: true in the gql schema or
  • Map(Var("page"), Lambda(["ts", "ref"], Get(Var("ref")))) to pass ["ts", "ref"] to the lambda after pagination