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

Supabase Sync Plugin, RLS and Create Action don't listen to errors #430

Open
zenfin-tech opened this issue Jan 2, 2025 · 1 comment
Open

Comments

@zenfin-tech
Copy link

I created an org table with a RLS policy to enable only the org owner to insert it.

When I try to use the plugin to create a new org with this policy defaulting to false (just to try if RLS is working) the org gets created locally, although it fails to save on the backend, but I get no error back.

Would be great to listen for the result of the API, and in case we get an error, it rolls back the action and propagate the error back to the client.

import { createClient } from '@/lib/supabase/client'
import { observable } from '@legendapp/state'
import { configureSyncedSupabase, syncedSupabase } from '@legendapp/state/sync-plugins/supabase'
import { v4 as uuidv4 } from "uuid"

const generateId = () => uuidv4()

configureSyncedSupabase({
generateId,
changesSince: 'last-sync',
fieldCreatedAt: 'created_at',
fieldUpdatedAt: 'updated_at',
fieldDeleted: 'deleted'
})

const supabase = createClient()

export const orgs$ = observable(
syncedSupabase({
supabase,
collection: 'org',
select: (from) => from.select("*"),
actions: ['read', 'create'],
})
)

'use client';
import { Input, Button } from "@nextui-org/react";
import { Eye, IdCard, Users } from "lucide-react";
import { useFormik } from "formik";
import { usePreferredLanguage } from "@uidotdev/usehooks";
import { addOrg } from "@/lib/legend"
import * as Yup from "yup";

export function Form({ handleClose }) {

const form = useFormik({
initialValues: {
    nome: "",
    cnpj: "",
},
validationSchema: Yup.object().shape({
  nome: Yup.string().required('Campo obrigatório'),
  cnpj: Yup.string().required('Campo obrigatório'),
}),
onSubmit: async (values, { setSubmitting }) => {
    setSubmitting(true);
    addOrg(values)
    setSubmitting(false);
    handleClose()
},
validateOnBlur: true,
validateOnChange: true

})

return (


<Input
autoFocus
name="nome"
type="string"
isRequired
variant="underlined"
label="Nome"
placeholder="Nome"
labelPlacement="outside"
endContent={

}
value={form.values.nome}
onChange={form.handleChange('nome')}
onBlur={form.handleBlur('nome')}
errorMessage={form.errors.nome}
isInvalid={form.touched.nome && form.errors.hasOwnProperty('nome')}
/>
<Input
name="cnpj"
type="string"
isRequired
variant="underlined"
label="CNPJ"
placeholder="CNPJ"
labelPlacement="outside"
endContent={

}
value={form.values.cnpj}
onChange={form.handleChange('cnpj')}
onBlur={form.handleBlur('cnpj')}
errorMessage={form.errors.cnpj}
isInvalid={form.touched.cnpj && form.errors.hasOwnProperty('cnpj')}
/>

<Button fullWidth color="primary" onPress={form.handleSubmit} isLoading={form.isSubmitting} isDisabled={!form.isValid || form.isSubmitting}>
Salvar



);
}

@zenfin-tech
Copy link
Author

the problem seems to be on the default create action, if I force it to:

create: (input) => supabase.from('org').insert(input),

becasue the default behaviour adds a select after the insert and because I have a trigger that runs after the row is inserted and the RLS Policy depends on this trigger outcome, the select fails, but ir rolls back the entire commit, making the insert fail as well.

But I am not sure if this work around can mess other inner logics from legend state... Anyone can help confirming that? Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant