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 not updated if there are no observers #465

Open
juanmigdr opened this issue Feb 14, 2025 · 1 comment
Open

Supabase not updated if there are no observers #465

juanmigdr opened this issue Feb 14, 2025 · 1 comment

Comments

@juanmigdr
Copy link

juanmigdr commented Feb 14, 2025

I am using LegendState V3 with the Supabase plugin and I was having an issue where I was doing a .set() and the data was only being stored locally and not in the DB. After a lot of troubleshooting I found out that if I do not have an observer listening to the changes, these are not updated in Supabase. Here is my configuration:

export const generateId = () => uuidv4();

export const customSynced = configureSynced(syncedSupabase, {
  persist: {
    plugin: observablePersistAsyncStorage({
      AsyncStorage,
    }),
  },
  generateId,
  supabase,
  changesSince: "last-sync",
  fieldCreatedAt: "created_at",
  fieldUpdatedAt: "updated_at",
  fieldDeleted: 'deleted'
});

Here is the table I am updating:

import { batch, observable } from "@legendapp/state";
import { customSynced, supabase, generateId } from "../utils/SupaLegend";
import { exams$ } from "./examsState";
import { use$ } from "@legendapp/state/react";
import { observe } from "@legendapp/state";

export const chapters$ = observable(
  customSynced({
    supabase,
    initial: {},
    collection: "chaps",
    select: (from) =>
      from
        .select("id,blocks,selected_block_idx,created_at,updated_at,deleted")
        .order("created_at", { ascending: false }),
    actions: ["read", "create", "update", "delete"],
    realtime: true,    persist: {
      name: "chapters",
      retrySync: true,
    },
    retry: {
      infinite: true,
    },
    onError(error, params) {
      console.log(error);
    },
    onSaved(params) {
      console.log("SV");
    },
    syncMode: "auto",
  })
);

export const addChapter = (examId: string) => {
  const chapterId = generateId();
  chapters$[chapterId].assign({
      id: chapterId,
      blocks: [],
      selected_block_idx: 0,
  });
};

Since I have nothing observing these, the only way to fix the issue was to add the following at the bottom:

observe(() => {
  chapters$.get();
  console.log("Chapters updated:");
});

Is this intended? If so:

  • Why?
  • Where is it documented?
@jmeistrich
Copy link
Contributor

Yes, it's intended, though I very much understand how it's confusing. It's documented here but we may need to repeat that in all the plugin pages...

Image

The reason is that it's very useful to be able to define how an observable syncs itself when accessed, without actually syncing it. In Bravely for example, we set up sync for hundreds of different observables, and then individual pages access some subset of them. We don't want to immediately call hundreds of server functions, but to update them on demand while the user navigates around between pages.

However! Doing a set should trigger a sync followed by committing the save to the server. We have some tests for that:

test('set not called until loaded first', async () => {

But is that not working in supabase for some reason? Can you make sure you're on the latest beta version and confirm in the network logs that calling set doesn't trigger the supabase sync or the update?

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

2 participants