postguard icon indicating copy to clipboard operation
postguard copied to clipboard

Support for column aliases (`as`)

Open andrewconner opened this issue 5 years ago • 1 comments

Thanks for writing this library, it's exactly what I was looking for.

Is there a way to support column aliases? Here's what I get right now:

✖ Query validation failed in server/models/SomeModel.ts:39:13:

  No table in the query's scope has a column "user_token".
  Tables in scope: "some_table"

  37 | export async function getUserToken(requestId: string): Promise<string | null> {
  38 |   const { rows } = await database.query<{user_token: string}>(sql`
> 39 |     SELECT details -> 'user_token' AS user_token FROM some_table WHERE
     |            ^
  40 |       request_id = ${requestId} AND
  41 |       details -> 'user_token' IS NOT NULL
  42 |     ORDER BY timestamp DESC;

The workaround that seems to be fine is by using sql.raw:

    SELECT ${sql.raw('details -> \'user_token\' AS user_token')} FROM some_table WHERE 
      request_id = ${requestId} AND 
      details -> 'user_token' IS NOT NULL
    ORDER BY timestamp DESC;

Is this the best way to handle it? Thanks!

andrewconner avatar Oct 16 '19 19:10 andrewconner

Hi Andrew! Yeah, stick to that work-around for now if it works.

I cannot promise when exactly I will be able to look into this, but it should definitely be possible.

Thanks for reporting, btw.

andywer avatar Oct 17 '19 02:10 andywer