Upsert data ( supabase)
One script reply has been approved by the moderators Verified

Created by adam186 60 days ago Viewed 57 times 0 Points

No comments yet

Login to be able to comment
Points: 0
deno
One script reply has been approved by the moderators
Ap­pro­ved
import { Resource } from "https://deno.land/x/windmill@v1.70.1/mod.ts";
import { removeObjectEmptyFields } from "https://deno.land/x/windmill_helpers@v1.0.1/mod.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";

/**
 * @param onConflict Comma-separated UNIQUE column(s) to specify how duplicate 
 * rows are determined. Two rows are duplicates if all the onConflict columns are equal.
 * 
 * @param ignoreDuplicates If true, duplicate rows are ignored. If false, duplicate 
 * rows are merged with existing rows.
 * 
 * @param count Count algorithm to use to count rows in the table or view.
 * `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the hood.
 * `"planned"`: Approximated but fast count algorithm. Uses the Postgres statistics under the hood.
 * `"estimated"`: Uses exact count for low numbers and planned count for high numbers.
 */
export async function main(
  auth: Resource<"supabase">,
  table: string,
  values: any,
  onConflict?: string,
  ignoreDuplicates: boolean = true,
  returnUpdated: boolean = false,
  count?: "exact" | "planned" | "estimated",
) {
  const client = createClient(auth.url, auth.key);

  let query: any = client.from(table).upsert(
    values,
    removeObjectEmptyFields({ onConflict, ignoreDuplicates, count })
  );
  if (returnUpdated) {
    query = query.select()
  }

  return await query;
}

Submitted by adam186 60 days ago

Edited 13 days ago

No comments yet

Login to be able to comment