Wow paginated query is amazing!
Wanted to know if you can help, with in the paginated query I want to sort and filter if possible.
This is what I have right now, only the sorting. Not sure if it's right. Wanted to also do filtering,
export const getPortfolios = query({
args: {
paginationOpts: paginationOptsValidator,
sortType: v.string(),
filterType: v.string(),
},
handler: async (ctx, args) => {
const { paginationOpts, sortType, filterType } = args;
// TODO:Filtering
// Sorting
if (sortType === 'recentlyAdded') {
return await ctx.db
.query('portfolios')
.filter((q) => q.eq(q.field('tags'), [filterType])) // Doesn't work
.order('desc')
.paginate(paginationOpts);
} else if (sortType === 'mostPopular') {
return await ctx.db
.query('portfolios')
.filter((q) => q.eq(q.field('tags'), [filterType])) // Doesn't work
.withIndex('by_favoritesCount')
.order('desc')
.paginate(paginationOpts);
} else if (sortType === 'alphabetical') {
return await ctx.db
.query('portfolios')
.filter((q) => q.eq(q.field('tags'), [filterType])) // Doesn't work
.withIndex('by_name')
.order('asc')
.paginate(paginationOpts);
} else {
return await ctx.db.query('portfolios').paginate(paginationOpts);
}
},
});
here is my schema for portfolios
portfolios: defineTable({
name: v.string(),
link: v.string(),
tags: v.optional(v.array(v.string())),
image: v.id('_storage'),
favoritesCount: v.optional(v.number()),
})
.index('by_favoritesCount', ['favoritesCount'])
.index('by_name', ['name'])
.index('by_tags', ['tags']),
...```