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

Expression builder v2 #565

Merged
merged 7 commits into from
Jul 7, 2023
Merged

Expression builder v2 #565

merged 7 commits into from
Jul 7, 2023

Conversation

koskimas
Copy link
Member

@koskimas koskimas commented Jul 5, 2023

This PR deprecates the cmpr and bxp functions and makes the ExpressionBuilder callable. eb can now be used to create all sorts of binary expressions. I didn't change the site examples yet, or otherwise they would be broken until we release this.

This PR also removes the deprecated orWhere, whereExits etc. methods. It might be a bit early to do that, but didn't find a way to make the expression builder callable while they existed.

The second commit introduces an alternative way to create and and or expressions. The old way is still there and won't be removed or deprecated. Some people prefer that one and it's (at least currently) much easier to create conditional or-expressions using the or([expr1, expr2]) function.

These changes now make simple or statements fluent and arguably more readable:

// The "old" way
db.selectFrom('person')
  .selectAll()
  .where(eb => eb.or([
    eb.cmpr('first_name', '=', 'Jennifer'),
    eb.cmpr('last_name', '=', 'Aniston')
  ])

VS

// The new way
db.selectFrom('person')
  .selectAll()
  .where(eb => 
    eb('first_name', '=', 'Jennifer').or('last_name', '=', 'Aniston')
  )

You can still destructure eb. eb contains a property eb that refers to itself:

db.selectFrom('person')
  .selectAll()
  .where(({ eb, or }) => or([
    eb('first_name', '=', 'Jennifer'),
    eb('last_name', '=', 'Aniston')
  ])

closes #494

@koskimas koskimas requested a review from igalklebanov July 5, 2023 09:36
@vercel
Copy link

vercel bot commented Jul 5, 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 7, 2023 0:40am

@igalklebanov igalklebanov added enhancement New feature or request api Related to library's API labels Jul 5, 2023
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.

Phenomenal stuff! 🙇

This is definitely a step in the right direction, removing the noise of cmpr and bxp, and adding the on-the-fly or/and chaining.

And all the deprecated code being deleted, music to my ears!! 😃

A small concern might be the addition of DB and TB to ExpressionWrapper's interface, and how it will affect consumers who make their own helpers. Is it still as flexible as before? I guess we'll find out and adjust if there are issues.

I'll do another pass tomorrow morning, it's late and I fear I missed some stuff.

test/node/src/join.test.ts Outdated Show resolved Hide resolved
src/schema/create-table-builder.ts Show resolved Hide resolved
src/query-builder/deprecated-where-expression-builder.ts Outdated Show resolved Hide resolved
src/expression/expression-wrapper.ts Outdated Show resolved Hide resolved
src/expression/expression-wrapper.ts Outdated Show resolved Hide resolved
src/expression/expression-builder.ts Outdated Show resolved Hide resolved
@koskimas
Copy link
Member Author

koskimas commented Jul 6, 2023

A small concern might be the addition of DB and TB to ExpressionWrapper's interface, and how it will affect consumers who make their own helpers. Is it still as flexible as before? I guess we'll find out and adjust if there are issues.

I don't think ExpressionWrapper is that useful in customer code. Expression and AliasedExpression are still simple.

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.

LET'S GO! 🚀

koskimas and others added 7 commits July 7, 2023 15:40
This commit also removes the deprecated filter methods. It wasn't possible to keep them
and have the combined expression builder + query builder monster be callable.
@koskimas koskimas force-pushed the expression-builder-v2 branch from 7eb9e03 to b83266e Compare July 7, 2023 12:40
@koskimas koskimas merged commit b23844b into master Jul 7, 2023
@koskimas koskimas deleted the expression-builder-v2 branch July 7, 2023 12:50
Gaspero pushed a commit to Gaspero/kysely that referenced this pull request Oct 2, 2023
* Make expression builder callable.

This commit also removes the deprecated filter methods. It wasn't possible to keep them
and have the combined expression builder + query builder monster be callable.

* Add chainable ands and ors to expression builder

* Update src/expression/expression-builder.ts

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

* Update src/expression/expression-wrapper.ts

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

* Update src/expression/expression-wrapper.ts

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

* fix confusing join expression test

* consistent language

---------

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 breaking change Includes breaking changes enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expression Builder API feedback
2 participants