Open HHogg opened 2 months ago
With the help of ChatGPT (😬) I can now see it's the ${direction}
part that was the issue.
const productsQuery = sql`
select ${sql(selectKeys)}
from products
where department = ${department}
${category ? sql`and ${category} = any(categories)` : sql``}
order by ${sql(field)} ${direction === 'asc' ? sql`asc` : sql`desc`}
limit ${limit}`;
... is what was needed.
However I guess my question is still relevant, in that is it possible to provide more hints in the error message?
Would be great to get more information in the error output 👍
I think the current open issue requesting this is:
Hmmm I think yes sir maybe the error messages could be improved.
But another approach could be avoiding those errors by using a query builder.
I used to write queries this manual way too until I used Kysely (it's a query builder, not an ORM, I hate ORMs).
const query = db
.withSchema(schemaName)
.selectFrom(tableName)
.select(selectKeys)
.where('department', '=', department);
if (categories instanceof Array && categories.length > 0) {
query = query.where('categories', in, categories);
}
if (typeof sortDirection === 'string') {
query = query.orderBy(sortField, sortDirection);
} else {
query = query.orderBy(sortDefaultField, sortDefaultDirection);
}
if (typeof limit === 'number') {
query = query.limit(limit);
}
const compiled = query.compile();
console.log({ compiled });
const items = await query.execute();
console.log({ items });
Above example may or may not be the exact code you need but maybe just tweak it better.
Hm, @josh-i386g not so sure this is the best recommendation for this project.
I think the ability of writing raw SQL in Postgres.js and getting verbose, helpful error messages should be the default - not recommending that users download, install and integrate a separate query builder.
Hi 👋
I'm struggling to debug this query (even after reading the documentation... several times). I know it's highly likely I'm doing something wrong, but I'm wondering if it's possible to add any additional information to the error that is being thrown to help track down the issue? Like outputting the final SQL query with some formatting to highlight which part is failing?
Side note: I really like the API of the library, I think you did a great job keeping it as close to writing standard sql while also being super powerful.