drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.52k stars 487 forks source link

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

Open san4d opened 2 months ago

san4d commented 2 months ago

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: https://github.com/drizzle-team/drizzle-orm/pull/1079.

mwanago commented 1 month ago

That would be great