renathy
renathy

Reputation: 5355

knex.raw with existing columns array using .first statement

Here is existing code:

       knex("products")
           .first("id", "name", "ingredients")
       ...

So, currently it just uses array of column names.

Now I want to add calculated column here. It would consists of "constant" + product.id. For product with id 1 it would be "api/v1/img/1". For product with id 222 it would be "api/v1/img/222". Alias of it should be "image".

I have to use knex.raw somehow. Do not understand how and what is the correct syntax to use it with .first().

Upvotes: 0

Views: 755

Answers (1)

Mikael Lepistö
Mikael Lepistö

Reputation: 19728

I'm sorry, I'm unable to understand the question. What kind of result are you trying to achieve? maybe something like this?

knex("products")
  .select('*', knex.raw(`'api/v1/img' || ?? as computed`, ['products.id']))
  .first()

Like this: https://runkit.com/embed/9okme0czge8z

Upvotes: 1

Related Questions