erquhart
erquhart14mo ago

Multi-value indexes

Especially for paginated queries, supporting multiple values for the same field in an index would be make certain queries a lot less complicated to write. The pattern of paging through n pages from the client to gather a complete page does work, but it's fiddly to implement and, on a lesser note, typically multiplies function calls. Example, I want to paginate through high priority records in a table. Priority is a number, and high priority is anything above 10. I either have to do excess pagination calls on the frontend to build up a page, or add a "high priority" field to the table that provides a boolean for whether the priority field qualifies as "high". I don't honestly know what the API for that would look like. Maybe each field name string in the index definition can be optionally replaced with a config object.
.index('highPriority', [{ field: 'priority', values: [10, 20] }])
.index('highPriority', [{ field: 'priority', values: [10, 20] }])
5 Replies
lee
lee14mo ago
Does this work?
db.query("docs").withIndex("by_priority", q=>q.gt("priority", 10)).paginate(opts)
db.query("docs").withIndex("by_priority", q=>q.gt("priority", 10)).paginate(opts)
A general multi-value index filter could be possible, but it may be tricky to come up with an intuitive syntax. We haven't seen a lot of examples that require it, so having more details of your use-case might help
erquhart
erquhartOP14mo ago
Totally forgot we can include a range in the index - this does work for my use case 👍
ian
ian14mo ago
And if you want them with highest priority first, .order("desc")
lee
lee14mo ago
If you want to order by creationTime though, you're out of luck without doing the complicated thing
erquhart
erquhartOP14mo ago
Not a blocker for now 👍

Did you find this page helpful?