tinyplex / tinybase

The reactive data store for local‑first apps.
https://tinybase.org
MIT License
3.39k stars 67 forks source link

Chained/nested joins #79

Closed segments-tobias closed 10 months ago

segments-tobias commented 10 months ago

I want to create a query over multiple tables, let's say orders, customers, and countries, to retrieve a list of orders along with customer information and the country information of each customer. The orders table has a foreign key customer_id referencing the customers table, and the customers table has a foreign key country_id referencing the countries table.

In SQL, you can achieve this using nested LEFT JOINs like this:

SELECT
    o.order_id,
    c.customer_name,
    co.country_name
FROM
    orders o
LEFT JOIN
    customers c ON o.customer_id = c.customer_id
LEFT JOIN
    countries co ON c.country_id = co.country_id;

In Tinybase, it seems like I cannot replicate the second join condition. If I do join("countries", "country_id"), I don't get any results.

Perhaps the join condition could allow for something like join("countries", "customers.country_id")?

Any thoughts?

segments-tobias commented 10 months ago

Nevermind, this is already possible by doing join("countries", "customers", "country_id"). Thanks for the great library :)

jamesgpearce commented 10 months ago

Sorry it wasn't obvious, but glad you got unblocked!