Skip to content

Expression Builder API feedback #494

@ethanresnick

Description

@ethanresnick

Thanks again for all the great work you're doing here! I just started using the new(ish) ExpressionBuilder API, and wanted to give a bit of feedback, which I hope will be helpful.

In general, I think the new design makes a lot of sense, and the new names apply better in the various contexts where expressions can be used than the old ones did. My only suggestion has to do with the abbreviated method names cmpr and bxp. Two things struck me about these:

  1. Using abbreviations felt a bit out of place with the rest of the kysely API, which generally seems favors explicitness and readability over maximum concision (e.g., selectFrom rather than from, or something even shorter). To me, the three letters saved by using cmpr as opposed to compare doesn't seem worth it, given that cmpr is not an especially familiar abbreviation.

  2. However, if the expression builder API is going to use abbreviations — and I can see a case for that in this API — then I think it might be worth adding abbreviated methods for specific, common binary comparisons, like eq, neq, gt, gte, etc. Having methods like that is, to me, both more intuitive (because eq is a much more common/familiar abbreviation than cmpr) and more concise (since the operator can be omitted).

So, I guess the above suggest three possible APIs:

  1.  qb.where(({ compare, and }) => and([
       compare('first_name', '=', 'Jennifer'),
       compare('last_name', '!=', 'Tremblay'),
       compare('updated_at', '>', lastMonth)
     ])
  2.  qb.where(({ cmpr, and }) => and([
       cmpr('first_name', '=', 'Jennifer'),
       cmpr('last_name', '!=', 'Tremblay'),
       cmpr('updated_at', '>', lastMonth)
     ])
  3.  qb.where(({ eq, neq, gt, and }) => and([
       eq('first_name', 'Jennifer'),
       neq('last_name', 'Tremblay'),
       gt('updated_at', lastMonth)
     ])

Personally, I think I prefer (1), for maximum clarity without too much verbosity. But, if maximal concision is the goal, then (3) makes more sense to me than two.

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiRelated to library's APIdiscussionLet's talk about design, goals and other stuff related to Kysely's development

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions