Thanks to visit codestin.com
Credit goes to hub.windmill.dev

Insert data
Codestin Search App Verified
Created by adam186 1082 days ago Picked 10 times
Submitted by adam186 Deno
Verified 244 days ago
1
import { refreshAndRetryIfExpired } from "https://deno.land/x/[email protected]/mod.ts";
2

3
/**
4
 * @param token Supabase `access_token` and `refresh_token`. `expires_at` (optional) is a UNIX
5
 * timestamp in seconds.
6
 *
7
 * @param count Count algorithm to use to count rows in the table or view.
8
 * `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the hood.
9
 * `"planned"`: Approximated but fast count algorithm. Uses the Postgres statistics under the hood.
10
 * `"estimated"`: Uses exact count for low numbers and planned count for high numbers.
11
 */
12
type Supabase = {
13
  url: string;
14
  key: string;
15
};
16
export async function main(
17
  auth: Supabase,
18
  table: string,
19
  values: Record<string, any> | Record<string, any>[],
20
  returnInserted: boolean = false,
21
  token?: {
22
    access: string;
23
    refresh: string;
24
    expires_at?: number;
25
  },
26
  count?: "exact" | "planned" | "estimated",
27
) {
28
  return await refreshAndRetryIfExpired(auth, token, async (client) => {
29
    let query: any = client.from(table).insert(values, { count });
30
    if (returnInserted) {
31
      query = query.select();
32
    }
33

34
    return query;
35
  });
36
}
37