Skip to content
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

Support materialized CTEs using CTE builder #578

Merged
merged 4 commits into from
Jul 12, 2023
Merged

Support materialized CTEs using CTE builder #578

merged 4 commits into from
Jul 12, 2023

Conversation

koskimas
Copy link
Member

@koskimas koskimas commented Jul 11, 2023

The first argument of with can now be a callback that's given a CTE builder. The CTE builder can be used to create the common table expression statement.

db.with(
    (cte) => cte('jennifers').materialized(),
    (db) =>
      db
        .selectFrom('person')
        .where('first_name', '=', 'Jennifer')
        .select(['first_name', 'last_name as ln', 'gender'])
  )
  .selectFrom('jennifers')
  .select(['first_name', 'ln'])
  .execute()

The syntax feels natural and this way as materialized is "in the correct place" between the name and the query. But we're introducing a new way to do things here and I'm not sure if this a good precedent for future development, or just something completely disjoint from all other builders.

Putting the CTE builder last, after the query, also feels bad since it's in the wrong place. But that would be more in line with the other builders. 🤷

Closes #246

@vercel
Copy link

vercel bot commented Jul 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
kysely ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2023 5:57am

@koskimas koskimas marked this pull request as draft July 11, 2023 09:16
@koskimas
Copy link
Member Author

@igalklebanov I'll rename asMaterialized to materialized. as is not a part of materialized keyword.

@igalklebanov igalklebanov added enhancement New feature or request postgres Related to PostgreSQL api Related to library's API labels Jul 11, 2023
src/query-creator.ts Outdated Show resolved Hide resolved
Copy link
Member

@igalklebanov igalklebanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

left some nitpicky minor comments/suggestions..

@koskimas koskimas marked this pull request as ready for review July 12, 2023 05:58
@koskimas koskimas merged commit 6f565cb into master Jul 12, 2023
4 checks passed
@koskimas koskimas deleted the cte-builder branch July 12, 2023 06:01
Gaspero pushed a commit to Gaspero/kysely that referenced this pull request Oct 2, 2023
* Support materialized CTEs using CTE builder

* Remove 'as' from materialized methods in CTEBuilder

* add documentation for CTE builder

* Update src/query-compiler/default-query-compiler.ts

Co-authored-by: Igal Klebanov <[email protected]>

---------

Co-authored-by: Igal Klebanov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API enhancement New feature or request postgres Related to PostgreSQL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support [cte] as materialized (...)
2 participants