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

aliasing joined tables in .select('*') #484

Open
c00 opened this issue Oct 7, 2023 · 1 comment
Open

aliasing joined tables in .select('*') #484

c00 opened this issue Oct 7, 2023 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@c00
Copy link

c00 commented Oct 7, 2023

Improve documentation

There's a super nifty feature: renaming columns / tables in a select(), to influence the shape of the resulting object. However, it is not really documented anywhere.

Link

This is the only place I've found where renaming is shown, but it's labeled "Querying the same foreign table multiple times".

Describe the problem

A vital bit of information is missing. tl;dr: I can do client.from('books').select('*, author:authors(*)'). This is super helpful, but is not documented anywhere.

Example tables:

books:
  id: number
  title: text
  isbn: text
  author_id: number # FK > author.id

authors:
  id: number
  name: text

Now, I want to fetch book id 1, with the author.

const result = client.from('books').select('*, authors(*)').eq('id', 1).single();
const book = result.data;
console.log(`The book ${book.title} is written by ${book.authors.name}.'`);

The fact that the property book.authors is named as a plural, but the value is a singular author, is not great. This is a pretty common scenario when joining tables. Luckily we can change that like so:

const result = client.from('books').select('*, author:authors(*)').eq('id', 1).single();
                                               //^ Here we add an alias for the table
const book = result.data;
console.log(`The book ${book.title} is written by ${book.author.name}.'`);

This is much more reasonable. However, documentation of this is missing.

Describe the improvement

On this page, in the section of fetch examples, add 2 examples. One called "rename columns" and one called "rename joined tables". The first example would be to showcase renaming normal columns such as .select('name:title'). The second example would be specifically about the use-case described above.

Additional context

In my code, getting more than one table's data at the same time is very common. And renaming columns like this is an essential tool for making the resulting objects easy to use. I would love it to be a lot clearer to new people reading the docs, that this is possible. It would've saved me a lot of refactoring at least :)

@c00 c00 added the documentation Improvements or additions to documentation label Oct 7, 2023
@rookledookle
Copy link

I would add that we need documentation for stating the alias name when you also specify the foreign key - can't find docs on this

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

No branches or pull requests

2 participants