Skip to content

Commit

Permalink
Refactor Supabase plugin to avoid upsert operations, separate create …
Browse files Browse the repository at this point in the history
…and update functions
  • Loading branch information
warrenbhw committed Jul 5, 2024
1 parent 7e7cfea commit 91dabb4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Binary file modified bun.lockb
Binary file not shown.
41 changes: 29 additions & 12 deletions src/sync-plugins/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,34 @@ export function syncedSupabase<
}
: undefined;

const upsert = async (input: SupabaseRowOf<Client, Collection>) => {
const res = await client.from(collection).upsert(input).select();
const { data, error } = res;
if (data) {
const created = data[0];
return created;
} else {
throw new Error(error?.message);
}
};
const create = !actions || actions.includes('create') ? upsert : undefined;
const update = !actions || actions.includes('update') ? upsert : undefined;
const create =
!actions || actions.includes('create')
? async (input: SupabaseRowOf<Client, Collection>) => {
const res = await client.from(collection).insert(input).select();
const { data, error } = res;
if (data) {
const created = data[0];
return created;
} else {
throw new Error(error?.message);
}
}
: undefined;

const update =
!actions || actions.includes('update')
? async (input: SupabaseRowOf<Client, Collection>) => {
const res = await client.from(collection).update(input).eq('id', input.id).select();
const { data, error } = res;
if (data) {
const created = data[0];
return created;
} else {
throw new Error(error?.message);
}
}
: undefined;

const deleteFn =
!fieldDeleted && (!actions || actions.includes('delete'))
? async (input: { id: SupabaseRowOf<Client, Collection>['id'] }) => {
Expand All @@ -184,6 +200,7 @@ export function syncedSupabase<
}
}
: undefined;

const subscribe = realtime
? ({ node, value$, update }: SyncedSubscribeParams<TRemote[]>) => {
const { filter, schema } = (isObject(realtime) ? realtime : {}) as { schema?: string; filter?: string };
Expand Down

0 comments on commit 91dabb4

Please sign in to comment.