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

Created by adam186 133 days ago Viewed 76 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.85.0/mod.ts";
import { refreshAndRetryIfExpired } from "https://deno.land/x/windmill_helpers@v1.1.1/mod.ts";

/**
 * @param filter Learn more at https://supabase.com/docs/reference/javascript/filter
 * 
 * @param token Supabase `access_token` and `refresh_token`. `expires_at` (optional) is a UNIX
 * timestamp in seconds.
 * 
 * @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,
  filter: {
    column: string;
    operator: string;
    value: any;
  },
  return_deleted: boolean = false,
  token?: {
    access: string;
    refresh: string;
    expires_at?: number;
  },
  count?: "exact" | "planned" | "estimated",
) {
  return await refreshAndRetryIfExpired(auth, token, async (client) => {
    let query: any = client.from(table)
      .delete({ count })
      .filter(filter.column, filter.operator, filter.value);

    if (return_deleted) {
      query = query.select()
    }

    return query;
  })
}

Submitted by adam186 133 days ago

Edited 57 days ago

No comments yet

Login to be able to comment