kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
9.81k stars 249 forks source link

Select columns as NULL or as placeholder #1020

Closed MaxosHadj closed 1 month ago

MaxosHadj commented 1 month ago

Trying to do the following:

SELECT value_a, value_b, null AS c FROM table

or

SELECT value_a, value_b, 'myValue' AS c FROM table

I tried to use raw SQL without success image

Any idea?

igalklebanov commented 1 month ago

Hey 👋

I tried to use raw SQL without success

You should provide the errors you're getting. Otherwise people can't help you.

sql template tag has the lit method for this use-case.

import { sql } from "kysely";

await db
  .selectFrom("table")
  .select([
    "value_a",
    "value_b",
    sql.lit(null).as("c"),
    sql.lit("myValue").as("d"),
  ])
  .execute();

https://kyse.link/qjh24

But be careful! to avoid SQL injection, you must validate strings passed to sql.lit.