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

Improve relations recipe #1081

Merged
merged 1 commit into from
Jul 16, 2024
Merged

Improve relations recipe #1081

merged 1 commit into from
Jul 16, 2024

Conversation

koskimas
Copy link
Member

@koskimas koskimas commented Jul 16, 2024

I don't know why it took so long to figure out that this is how we should tell people to write their relation helper functions:

function pets(foreignKey: Expression<string>) {
  return jsonArrayFrom(
    db.selectFrom('pet')
      .select(['pet.id', 'pet.name'])
      .where('pet.owner_id', '=', foreignKey)
      .orderBy('pet.name')
  )
}

Instead of the old suggestion:

function pets(eb: ExpressionBuilder<DB, 'person'>) {
  return jsonArrayFrom(
    eb.selectFrom('pet')
      .select(['pet.id', 'pet.name'])
      .whereRef('pet.owner_id', '=','person.id')
      .orderBy('pet.name')
  )
}

The former only takes in the dependencies (the foreign key) as an argument and provides just as much type-safety as the old way.

The old way had a number of issues: It specified the exact ExpressionBuilder type, which doesn't work if the parent query has any joins or uses an alias for the person table.

Another benefit is that we can work with the db type instead of the more complex eb type. Especially if the relations are deeply nested, the deepest eb types become really slow, as they inherit their DB types from the parent ExpressionBuilders and etc.

Also, this is in line with how we instruct people to write any helper functions:

function doStuff(expr: Expression<number>) { 
  return sql<T>`something that uses ${expr}`
}

Another benefit: You can't accidentally select stuff from the parent scope.

Copy link

vercel bot commented Jul 16, 2024

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 16, 2024 2:14pm

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! 🚀

@igalklebanov
Copy link
Member

Hey 👋

Shouldn't this land on master instead of the version branch?

@igalklebanov igalklebanov added the documentation Improvements or additions to documentation label Jul 16, 2024
@koskimas
Copy link
Member Author

Shouldn't this land on master instead of the version branch?

True. I'll rebase.

@koskimas koskimas merged commit 1374f73 into master Jul 16, 2024
12 checks passed
@koskimas koskimas deleted the improve-relations-recipe branch July 16, 2024 14:30
thecodrr pushed a commit to thecodrr/kysely that referenced this pull request Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants