Fetch the next record after a specific ID that also satisfies a filter


#1

I have a specific record ID and a FetchSpec, and I’m interested in finding the next record after that ID that also satisfies that FetchSpec (e.g. filter and order). How would I do this in the Type System?

The only solution I can think of is to fetch in batches (limits and offsets, in addition to the filter and order) until my record appears in a batch, then return the next record.


#2
Type.fetch({filter: Filter.gt("id", "THE_LAST_ID").and("YOUR_CURRENT_FILTER"), limit: 1})


#3

This only works if the FetchSpec is ordering by ID, which is not always the case.


#4

i don’t believe thats true. The “order” in a fetch spec specifies the order of the returned data, not the order of the data in the db.

For example,

Type.fetch({filter: Filter.gt("someField", "someValue"), order: "someOtherField"})

works as expected.

In any case. Then add “order: ‘id’” to your spec?


#5

I see now, I mistook the gt("id", "THE_LAST_ID") as an assumption that the ordering was being done by IDs.


#6

But, if this fetch is limited to 2000, and my “id of interest” is not returned, then it doesn’t work.


#7

Maybe rephrasing the problem will help… there are 100,000 records and I “pick” record 5300.

According to some filter and order (that I know satisfies 5300) the items returned are:
[4343,6534,123,65465,…,5300,400,…,2132,5454,1231]
But without setting a limit:-1, I have no guarantee that 5300 or 400 is returned according to the filter and order.

I need something that will tell me 400.