Order by in a query clause

Hey guys, I am trying to retrieve a list of exercises and it's data + grid position (join) from my database. I also want this array of result to be ordered by it's grid position. Here is what I came up with (the following code works just fine however notice the javascript .sort rather than using the drizzle orderBy).
const getExercises = () =>{
return (
await ctx.db.query.exercises.findMany({
with: {
data: { orderBy: (data, { asc }) => [asc(data.doneAt)] },
position: true,
},
})
).sort((a, b) => b.position.gridPosition - a.position.gridPosition);
//^ how could I move this logic to an orderBy clause ?
}
const getExercises = () =>{
return (
await ctx.db.query.exercises.findMany({
with: {
data: { orderBy: (data, { asc }) => [asc(data.doneAt)] },
position: true,
},
})
).sort((a, b) => b.position.gridPosition - a.position.gridPosition);
//^ how could I move this logic to an orderBy clause ?
}
So how could I order my final result by it's grid position ? I have tried the following but without any luck:
const getExercises = () =>{
return (
await ctx.db.query.exercises.findMany({
with: {
data: { orderBy: (data, { asc }) => [asc(data.doneAt)] },
position: true,
},
orderBy: () => sql`exercises.grid_position`,
})
);
}
const getExercises = () =>{
return (
await ctx.db.query.exercises.findMany({
with: {
data: { orderBy: (data, { asc }) => [asc(data.doneAt)] },
position: true,
},
orderBy: () => sql`exercises.grid_position`,
})
);
}
1 Reply
Mykhailo
Mykhailo7mo ago
Hey @Augustin Sorel. grid_position is column in exercises table?
Want results from more Discord servers?
Add your server