porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.39k stars 267 forks source link

👋 Is it possible to provide more information in the error output? #914

Open HHogg opened 2 months ago

HHogg commented 2 months ago

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?

const selectKeys = ['name', ...];
const department = 'food';
const category = undefined;
const field = 'price';
const direction = 'asc';

const productsQuery = sql`
  select ${sql(selectKeys)}
  from products
  where department = ${department}
  ${category ? sql`and ${category} = any(categories)` : sql``}
  ${sql`order by ${sql(field)} ${direction}`}
  limit ${limit}`;

// PostgresError: syntax error at or near "$2"

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.

HHogg commented 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?

karlhorky commented 2 months ago

Would be great to get more information in the error output 👍

I think the current open issue requesting this is:

josh-i386g commented 2 months ago

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).

image

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.

karlhorky commented 1 month ago

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.