0
Delete data
One script reply has been approved by the moderators Verified
Created by adam186 701 days ago Viewed 9314 times
0
Submitted by adam186 Deno
Verified 701 days ago
1
import { refreshAndRetryIfExpired } from "https://deno.land/x/windmill_helpers@v1.1.1/mod.ts";
2

3
/**
4
 * @param filter Learn more at https://supabase.com/docs/reference/javascript/filter
5
 *
6
 * @param token Supabase `access_token` and `refresh_token`. `expires_at` (optional) is a UNIX
7
 * timestamp in seconds.
8
 *
9
 * @param count Count algorithm to use to count rows in the table or view.
10
 * `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the hood.
11
 * `"planned"`: Approximated but fast count algorithm. Uses the Postgres statistics under the hood.
12
 * `"estimated"`: Uses exact count for low numbers and planned count for high numbers.
13
 */
14
type Supabase = {
15
  url: string;
16
  key: string;
17
};
18
export async function main(
19
  auth: Supabase,
20
  table: string,
21
  filter: {
22
    column: string;
23
    operator: string;
24
    value: any;
25
  },
26
  return_deleted: boolean = false,
27
  token?: {
28
    access: string;
29
    refresh: string;
30
    expires_at?: number;
31
  },
32
  count?: "exact" | "planned" | "estimated",
33
) {
34
  return await refreshAndRetryIfExpired(auth, token, async (client) => {
35
    let query: any = client
36
      .from(table)
37
      .delete({ count })
38
      .filter(filter.column, filter.operator, filter.value);
39

40
    if (return_deleted) {
41
      query = query.select();
42
    }
43

44
    return query;
45
  });
46
}
47