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

Unable to insert a subscription into stripe wrapper #288

Closed
mfissehaye opened this issue Jun 18, 2024 · 2 comments · Fixed by #300
Closed

Unable to insert a subscription into stripe wrapper #288

mfissehaye opened this issue Jun 18, 2024 · 2 comments · Fixed by #300
Labels
bug Something isn't working

Comments

@mfissehaye
Copy link

mfissehaye commented Jun 18, 2024

Bug report

  • [*] I confirm this is a bug with Supabase, not with my own application.
  • [*] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I am trying to insert into the stripe.subscriptions table by doing this

const { data, error } = await supabase
      .schema("stripe")
      .from("subscriptions")
      .insert({
        customer,
        attrs: JSON.stringify({ items: [{ price: priceId }] }),
      });

I also tried directly inserting the data on the sql console

INSERT INTO stripe.subscriptions (customer, attrs)
VALUES (
    'customer_id',
    jsonb_build_object(
        'items', jsonb_build_array(
            jsonb_build_object('price', 'price_id')
        )
    )
);

However I am getting the following error.

{
  "code": "HV000",
  "details": "Wrappers",
  "hint": null,
  "message": "RETURNING is not supported"
}

Please provide more examples on how to insert data into the stripe foreign tables

System information

  • OS: macOS
  • Version of supabase-js: 2.43.4
  • Version of Node.js: v20.3.0
@mfissehaye mfissehaye added the bug Something isn't working label Jun 18, 2024
@burmecia
Copy link
Member

burmecia commented Jun 27, 2024

There are two things:

  1. insert data into foreign table

    The foreign table cannot convert JSON column for API post use, so we need to create the column name exactly same as the API required. For example, to insert a subscription follow the Stripe docs:

-- create the subscription table for data insert
create foreign table stripe.subscriptions (
  id text,
  customer text,
  "items[0][price]" text  -- column name will be used in API Post request
)
  server stripe_server
  options (
    object 'subscriptions',
    rowid_column 'id'
  );

And then we can insert a subscription:

insert into stripe.subscriptions (customer, "items[0][price]")
values ('cus_Na6dX7aXxi11N4', 'price_1MowQULkdIwHu7ixraBm864M');

Note that table can only be used for data insertion, querying on it will need to create another 'normal' foreign table like this.

  1. using API to access foreign table

    It is normally not suggested using API to access foreign table (docs), but if you really want to it is better to create a security definer function to encapsulate this operation like below example:

create function public.insert_stripe_subscription(customer text, price_id text)
   returns void
   language plpgsql
   security definer set search_path = public, extensions, pg_temp
   as $$
begin
     insert into stripe.subscriptions (customer, "items[0][price]")
     values (customer, price_id);
end;
$$;

And then call it from the client:

const { data, error } = await supabase
  .rpc('insert_stripe_subscription', { customer: 'cus_QMxTWax3UG0mYl', price_id: 'price_1Ojb7WJDPojXS6LNxZi0Z4eA' })
if (error) console.error(error)
else console.log(data)

@vickkhera
Copy link

vickkhera commented Jul 17, 2024

FWIW, this works as well:

insert into stripe.subscriptions (customer,attrs) values ('cus_QUgT45Svmxz0Mt','{"items[0][price]":"price_1PQvBRAKKSGCtBGcJAf3nMHP"}');

Having the ability to RETURNING id would be immensely helpful as well, not just for this table but customers as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants