orderBy with variable column

Hey all!
I'd like to explore the possibility to order a query by one of it's columns, but the column itself is inferred from a variable.
What i've tried:
// "categories" has 2 column, id and name

const sortField = 'name';

await db.query.categories.findMany({ orderBy: asc(sql`${sortField}`) });


but no sort is applied.

Even using

const sortField = 'name' as keyof typeof categories;

does not change the final result.
Any idea?

Thanks in advance!
Solution
const sortField = 'name';

await db.query.categories.findMany({ orderBy: asc(sql.identifier(sortField)) });
Was this page helpful?