Update data

Script supabase Verified

by adam186 ยท 1/20/2023

The script

Submitted by hugo989 Bun
Verified 6 days ago
1
import { createClient, SupabaseClient } from "@supabase/[email protected]"
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

49
async function refreshAndRetryIfExpired(
50
  auth: { url: string; key: string },
51
  token: { access: string; refresh: string; expires_at?: number } | undefined,
52
  fn: (client: SupabaseClient) => Promise<{ data: any; error?: any }>,
53
): Promise<{ data: any; error?: any; token?: { access: string; refresh: string; expires_at?: number } }> {
54
  const makeClient = async (autoRefreshToken: boolean) => {
55
    const client = createClient(auth.url, auth.key, {
56
      auth: { autoRefreshToken, persistSession: false },
57
    })
58
    if (token) {
59
      await client.auth.setSession({ access_token: token.access, refresh_token: token.refresh })
60
    }
61
    return client
62
  }
63
  try {
64
    let result = await fn(await makeClient(false))
65
    if (result?.error?.code === "PGRST301" && token) {
66
      const client = await makeClient(true)
67
      result = await fn(client)
68
      const { data } = await client.auth.getSession()
69
      if (data?.session) {
70
        token = {
71
          access: data.session.access_token,
72
          refresh: data.session.refresh_token,
73
          expires_at: data.session.expires_at,
74
        }
75
      }
76
    }
77
    return { ...result, token }
78
  } catch (error) {
79
    return { data: null, error }
80
  }
81
}
82

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { refreshAndRetryIfExpired } from "https://deno.land/x/[email protected]/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