drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Support Postgres CTE Materialization Options
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Describe what you want
Currently, Drizzle ORM supports common table expression (CTE) / WITH queries for the Postgres dialect. It would be useful for Drizzle to allow users to configure [materialization options](https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-CTE-MATERIALIZATION) for when the query is executed.
Currently, CTEs can be used with something like:
```ts
const theCte = db.$with('the_cte').as(db.select().from(users));
const query = db.with(theCte).select().from(theCte);
```
The above will generate SQL like the following:
```sql
with "the_cte" as (
select
"id",
"first_name",
"last_name"
from
"users"
)
select
"id",
"first_name",
"last_name"
from
"the_cte"
```
Supporting a new configuration parameter on the `.with()` method might allow passing materialization options. For example:
```ts
db.with(theCte, {
mode: CteMaterializationMode.NOT_MATERIALIZED
}).select().from(theCte);
```
Specifying the materialization mode as shown above would presumably generate some SQL like:
```sql
with "the_cte" as NOT MATERIALIZED (
select
"id",
"first_name",
"last_name"
from
"users"
)
select
"id",
"first_name",
"last_name"
from
"the_cte"
```
The following materialization modes would ideally be supported:
- `CteMaterializationMode.DEFAULT` (current serialization behavior)
- `CteMaterializationMode.MATERIALIZED`
- `CteMaterializationMode.NOT_MATERIALIZED`
Contributor guide
Assessment
This issue has not been assessed yet.