vercel / nextjs-subscription-payments

Clone, deploy, and fully customize a SaaS subscription application with Next.js.
https://subscription-payments.vercel.app/
MIT License
6.08k stars 1.23k forks source link

What does the following code do? #160

Closed TheBuilderJR closed 1 year ago

TheBuilderJR commented 1 year ago
  const getSubscription = () =>
    supabase
      .from('subscriptions')
      .select('*, prices(*, products(*))')
      .in('status', ['trialing', 'active'])
      .single();

It's super unclear what the .select(', prices(, products(*))') means.

There is no price field on subscriptions, so what magic is going on? Google doesn't help me. Can we please add a comment?

TimDev9492 commented 1 year ago

I think, this syntax is used to join the tables prices and products with the "parent" table respectively. For example, on every row of the subscriptions table, there is a price_id (foreign) key that's used to join that table with the prices table (primary key).

To further support my hypothesis, this is from the schema.sql script:

create table subscriptions (
  -- ...
  -- ID of the price that created this subscription.
  price_id text references prices,
  -- ...
);
thorwebdev commented 1 year ago

Yes, correct, this is to pull in the content from foreign tables. You can find docs for it here: https://supabase.com/docs/reference/javascript/select