question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: tagged template raw queries

See original GitHub issue

Hi, looking good so far, but this is a feature that I would like to see, something like this

await db.sql<User & { posts: Post[] }>`
SELECT
    u.*,
    (SELECT json_agg(post) FROM post WHERE user_id = user.id) as posts
FROM user
WHERE id = ${ userId }`

and userId would be escaped automatically. When doing raw queries with bindings as an array at the end it’s quite difficult to keep track on which ? is should correspond to which binding.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
koskimascommented, Feb 5, 2022

Yep, I’ll add a way to use identifiers.

I didn’t want to break anyone’s usecases, that’s why I suggested a separate method.

I don’t want to have two functions that do the same thing. Kysely doesn’t yet have that many users. Now’s the time to break the API if it improves it.

5reactions
koskimascommented, Feb 4, 2022

Everything seems cleaner and better with tagged templates 😄

Completely raw query

await db.raw('select * from person where id = ?', [id])
await db.sql`select * from person where id = ${id}`

Subquery

await db
  .selectFrom('person')
  .select(
    db.raw<number>(`(
      select count(pet.id) 
      from pet
      where species = ?
      and pet.owner_id = person.id
    )`, [species]).as('pet_count')
  )
await db
  .selectFrom('person')
  .select(
    db.sql<number>`(
      select count(pet.id) 
      from pet
      where species = ${species}
      and pet.owner_id = person.id
    )`.as('pet_count')
  )

I think we are going to replace raw with the sql tag you suggested in the next version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using raw queries with tagged template literals doesn't ...
Bug description It seems to me that the raw queries feature does not really work with tagged template literals using parameters.
Read more >
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
[RFC] Support for tagged template literals - ReScript Forum
Use raw to directly directly inline the tagged template. referenced post. %%raw("require('gql')` your query `").
Read more >
8. Template literals - Exploring JS
Tagged templates are function calls. In the previous example, the method String.raw is called to produce the result of the tagged template. 8.2...
Read more >
ES6 tagged templates practical usability - Stack Overflow
You can use tagged templates to build APIs that are more expressive than regular function calls. For example, I'm working on a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found