Skip to content

Dynamic order by #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mortifia opened this issue Apr 16, 2021 · 4 comments
Closed

Dynamic order by #174

mortifia opened this issue Apr 16, 2021 · 4 comments

Comments

@mortifia
Copy link

mortifia commented Apr 16, 2021

hello, I am currently using a list protected by an enum to retrieve the order wanted by the request

I wonder if there was a way to use this list or if I need to create each case

example of list for order by

const orderList = ['is_business desc', 'owner_business desc', 'owner_user desc']

example of use case

await this.db`
        select t.*, max(m.created_on) as created_on
          from chat.tickets t
          join chat.messages m using (ticket_uuid)
          join chat.ticket_user tu using (ticket_uuid)
          where tu.user_uuid = ${this.context.user_uuid}
          group by t.ticket_uuid
          ${this.db.orderBy(orderList)}`)
@porsager
Copy link
Owner

porsager commented Apr 16, 2021

For now you have to make do with multiple query versions or sql.unsafe().

Once we get dynamic keyword / fragments support you will be able to do it like this:

// Not working yet
sql`
  select * from wat
  orer by ${ sql(column) } ${ desc ? sql`desc` : sql`asc` }
`

@mortifia
Copy link
Author

mortifia commented Apr 17, 2021

thanks for your fast reply :)

@joshxyzhimself
Copy link

joshxyzhimself commented Aug 14, 2021

this one is supported already right?

i was looking for a way to use variables in columns. dynamic column names.

the following surprisingly works, lol.

await sql`
  CREATE TABLE IF NOT EXISTS "users" (
    "id" serial PRIMARY KEY,
    "name" text NOT NULL,
    "telegram_id" text,
    "whatsapp_id" text,
    "wechat_id" text,
    UNIQUE("name")
  );
`;
const platform = 'telegram';
const platform_user_id = '123123123';
const users = await sql`
  SELECT * FROM "users"
  WHERE ${sql(platform.concat('_id'))} = ${platform_user_id}
  LIMIT 1;
`;

@porsager
Copy link
Owner

Oh yeah, that should be fine :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants