0
Update data
One script reply has been approved by the moderators Verified
Created by adam186 702 days ago Viewed 22400 times
0
Submitted by adam186 Deno
Verified 702 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
  values: any,
22
  filter: {
23
    column: string;
24
    operator: string;
25
    value: any;
26
  },
27
  return_updated: boolean = false,
28
  token?: {
29
    access: string;
30
    refresh: string;
31
    expires_at?: number;
32
  },
33
  count?: "exact" | "planned" | "estimated",
34
) {
35
  return await refreshAndRetryIfExpired(auth, token, async (client) => {
36
    let query: any = client
37
      .from(table)
38
      .update(values, { count })
39
      .filter(filter.column, filter.operator, filter.value);
40

41
    if (return_updated) {
42
      query = query.select();
43
    }
44

45
    return query;
46
  });
47
}
48