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

Composite primary key problem #398

Open
A-Ghattas opened this issue Nov 18, 2024 · 0 comments
Open

Composite primary key problem #398

A-Ghattas opened this issue Nov 18, 2024 · 0 comments

Comments

@A-Ghattas
Copy link

Hi,
I am working in a project with supabase and using legend-state for "offline first" purposes. Everything is great apart from a small problem I run into. A little disclaimer tho, I just started using the library so please don't hesitate to correct me if you feel I understood something wrong.

This far this is my global config:

export const globalSynced = configureSynced(syncedSupabase, {
  // Use React Native Async Storage
  as: "Map",
  persist: {
    plugin: observablePersistAsyncStorage({
      AsyncStorage,
    }),
  },
  generateId, // This is hooked to uuidv4() but as I understand, it's only used for insert 
  supabase,
  changesSince: "last-sync",
  fieldCreatedAt: "created_at",
  fieldUpdatedAt: "updated_at",
  // Optionally enable soft deletes
  fieldDeleted: "deleted",
});

and then I access my tables like this:

export const trainings$ = observable(
  globalSynced({
    collection: "training_units", //This is a joint table with a composite primary key and no id field
    fieldId: "What should this be", // This accept one field only so it doesn't work with composite keys
    generateId: () => "No Idea what to put here" // This doesn't give access to the row so I can't work something up from the 2 columns I use as primary key
    select: (from) => from.select(trainingUnitsFields),
  })
);

Now for testing purposes I have 2 rows in the training_units table but when I access it with
const trainingUnits = trainingUnits$.get();
it returns a map with only one element with an undefined key. Since the 2 rows end up with an undefined id they overwrite each others in the map.

I have some proposition for the solution but I have no idea if it's feasible or not:

  • Give access to the row in the "generateId" param.
  • Add an array as an option for the "as" param, but I suppose this isn't really an option since there is a mention about it in the docs.
  • Add support to composite primary keys.

Please let me know if this is something that can be resolved in my side, if it needs to be implemented to the library to be possible, or if it's just impossible for the foreseeable future.
Thank you in advance for your replies.

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