For those who are using SvelteKit, had problems wi...
# javascript
f
For those who are using SvelteKit, had problems with RLS on supabase calls in an endpoint? I had to move all supabase calls away from endpoints
s
Do you have an example of such code? I am using SvelteKit with Supabase, but I'm not doing much in the endpoints besides running code that requires the
service_role
on the server.
f
Let me get one example
Copy code
# lib/db.js
import { createClient } from '@supabase/supabase-js'

const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_ANON_KEY,
)

...

export const insertRecipe = async (recipe) => {
  console.log(recipe);

  const { data, error } = await supabase
    .from('recipes')
    .insert([{ 
      name: recipe.name, 
      description: recipe.description, 
      user_id: recipe.user_id 
    }], { returning: "minimal" });

  if (error) {
    console.log(error);
    return false;
  } else {
    return data;
  }
};

...

export default supabase

# routes/api/recipes/create.js
import {
  insertRecipe,
  insertIngrdient,
  insertStep
} from "$lib/db";

export const post = async ({
  body
}) => {
  const formData = JSON.parse(body);

  const data = await insertRecipe({
    name: formData.name,
    description: formData.description,
    user_id: formData.user_id
  });

  if (data) {
    const recipe = data[0];

    formData.ingredients.forEach(async (ing) => {
      await insertIngrdient({
        ...ing,
        recipeId: recipe.id
      });
    });

    formData.steps.forEach(async (step) => {
      await insertStep({
        ...step,
        recipeId: recipe.id
      });
    });

    return {
      body: {
        result: 'ok',
      }
    };
  } else {
    return {
      body: {
        result: 'error',
      }
    };
  }
};
This was one of the problematic enpoints that the RLS wasn't working
Once I moved the logic and calls to the page/components, it started working haha
Sorry about my bad JS, I'm a Ruby/Elixir backend dev, so my JS skills are not that impressive haha
s
What was the RLS issue you were having? was it that no data was being returned or it was returning everything?
f
Returning no data at all
s
I mainly use SvelteKit so I will test this out in one of my projects over the weekend and see how I can best help
I've set this thread to stay open for 3 days so I don't forget about it
f
Nice, thanks again 🙂
Ill do more tests and update this thread if I find more info about this problem
s
Ok cool