Skip to content

Commit

Permalink
refactor: update createSupaworkerClient function
Browse files Browse the repository at this point in the history
  • Loading branch information
davidglivar committed May 1, 2024
1 parent 1da71ce commit ad31559
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,32 @@ export interface SupaworkerClientOptions {
supabase_options?: SupabaseClientOptions<'supaworker'>;
}

export function createSupaworkerClient<Payload>({
supabase_url,
supabase_service_role_key,
supabase_options,
}: SupaworkerClientOptions): {
/**
* Create a Supaworker client and enqueuing function.
* @param options Client options for the Supaworker client.
* @returns A supabase client and a function to enqueue jobs.
*/
export function createSupaworkerClient<Payload>(options: SupaworkerClientOptions): {
client: SupabaseClient<Database, 'supaworker'>;
enqueue: (job: SupaworkerJob<Payload>) => Promise<unknown>;
} {
const { supabase_url, supabase_service_role_key, supabase_options } = options;
const client = createClient<Database>(supabase_url, supabase_service_role_key, {
...supabase_options,
db: {
...supabase_options?.db,
schema: 'supaworker',
},
});
const enqueue = async (job: SupaworkerJob<Payload>) => {
return await client.from('jobs').insert({
...job,
options: job.options as Json,
payload: job.payload as Json,
});
};
return {
client,
enqueue: async (job: SupaworkerJob<Payload>) => {
return await client.from('jobs').insert({
...job,
options: job.options as Json,
payload: job.payload as Json,
});
},
enqueue,
};
}

0 comments on commit ad31559

Please sign in to comment.