ibrahimyaacob
ibrahimyaacob2mo ago

[ents] filter by index with a field and order by "updatedAt" instead of "_creation_time"

Im using ents to fetch the list of item by its indexed field, and i can only perform order based on created time. i need the updated time!
13 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ibrahimyaacob
ibrahimyaacobOP2mo ago
here's a snippet
No description
ibrahimyaacob
ibrahimyaacobOP2mo ago
when i add a field, updatedAt, like this, function deployed
ibrahimyaacob
ibrahimyaacobOP2mo ago
No description
ibrahimyaacob
ibrahimyaacobOP2mo ago
i get this error runtime error
ibrahimyaacob
ibrahimyaacobOP2mo ago
No description
ibrahimyaacob
ibrahimyaacobOP2mo ago
*ignore the function path, the error is when i add the updatedAt field on the order
ampp
ampp2mo ago
You make a new index with organizationId, and updatedAt so then its .table('mainList', 'organizationId_updatedAt', (q) => q.eq(... Order only accepts asc or desc so its based on the last item in the index. And having a additional .eq/.gt for updatedAt is optional
ibrahimyaacob
ibrahimyaacobOP2mo ago
hi @ampp can you further explain? I think youre solution might work but I have no idea how it would look like and how do i get the sorted mainList based on updatedAt.
ampp
ampp2mo ago
mainlist: defineEnt({})
.field('organizationId', v.string())
.field('updatedAt', v.number())
.index('organizationId_updatedAt', ['organizationId', 'updatedAt']),

const query = await ctx.table('mainList', 'organizationId_updatedAt', (q) => q.eq('organizationId', organizationId)).order('desc')
mainlist: defineEnt({})
.field('organizationId', v.string())
.field('updatedAt', v.number())
.index('organizationId_updatedAt', ['organizationId', 'updatedAt']),

const query = await ctx.table('mainList', 'organizationId_updatedAt', (q) => q.eq('organizationId', organizationId)).order('desc')
ibrahimyaacob
ibrahimyaacobOP2mo ago
hey @ampp this works!, thanks so much, yet i have no idea how indexing works like this. Would you mind sharing some document/article explaining this.
ampp
ampp2mo ago
Databases are Spreadsheets
I want to share my mental model of databases: - Databases are just big spreadsheets - An index is just a view of the spreadsheet sorted by one or mor...
Indexes | Convex Developer Hub
Indexes are a data structure that allow you to speed up your
Michal Srb
Michal Srb5w ago
Btw I fixed the types in 0.13.0, so the code that threw runtime exception no longer type checks.

Did you find this page helpful?