andywer / andywer/postguard

Support for column aliases (`as`)

Open
#30 1 comment 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
179
Forks
7
PR merge metrics
No merged PRs in 30d

Description

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 {
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`:

```sql
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!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.