Skip to content

[FEATURE]: Support Raw SQL in CTE Query Builder #2168

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
san4d opened this issue Apr 16, 2024 · 5 comments
Closed

[FEATURE]: Support Raw SQL in CTE Query Builder #2168

san4d opened this issue Apr 16, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@san4d
Copy link

san4d commented Apr 16, 2024

Describe what you want

Summary

Developers should be able to define CTEs using the sql escape hatch.

Current State

CTE definitions use a query builder that can be of type TypedQueryBuilder<TSelection, unknown> or (qb: QueryBuilder) => TypedQueryBuilder<TSelection, unknown>). Developers need to implement a custom query builder if they use the first type option or are limited to selects with they use the second option.

Candidate API

Something like this would allow access to the underlying sql:

db.$with('my-cte)
    .as(sql<MyCustomType[]>`
        select * from sometable
    `)

Additional Context

I'm reaching for this because I have a CTE that makes use of a lateral join, which is not currently supported in the select API. I'm watching this MR, which appears to add this support: #1079.

@san4d san4d added the enhancement New feature or request label Apr 16, 2024
@mwanago
Copy link

mwanago commented May 26, 2024

That would be great

@AlexDaniel
Copy link

This can be very helpful for introducing drizzle into existing codebases – put an existing query into a CTE and then combine it with drizzle queries. Right now you can more or less achieve this with subqueries, but CTEs would be more convenient.

@johanneskares
Copy link

how can you achieve that with subqueries?

@johanneskares
Copy link

ok answer found. I needed the ( and )

const bla = await drizzle.select({ id: sql<string>`id` }).from(sql`(SELECT * FROM public.user)`);

@AndriiSherman
Copy link
Member

Available in [email protected]

Release Notes

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

No branches or pull requests

5 participants