1 | import { Resource } from "https://deno.land/x/windmill@v1.85.0/mod.ts"; |
2 | import { createClient } from "https://esm.sh/@supabase/supabase-js@2"; |
3 |
|
4 |
|
5 | * @param access_token It is only needed if you have an affecting RLS policy enabled |
6 | * on the table that you want to access. |
7 | * Learn more about RLS here: https://supabase.com/docs/guides/auth/row-level-security |
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 | * @param head When set to `true`, `data` will not be returned. |
15 | * Useful if you only need the count. |
16 | * |
17 | * @param filter Learn more at https://supabase.com/docs/reference/javascript/filter |
18 | * |
19 | * @param order Learn more at https://supabase.com/docs/reference/javascript/order |
20 | * |
21 | * @param limit Learn more at https://supabase.com/docs/reference/javascript/limit |
22 | */ |
23 | export async function main( |
24 | auth: Resource<"supabase">, |
25 | table: string, |
26 | columns?: string, |
27 | access_token?: string, |
28 | count?: "exact" | "planned" | "estimated", |
29 | head?: boolean, |
30 | filter?: { |
31 | column: string; |
32 | operator: string; |
33 | value: any; |
34 | }, |
35 | order?: { |
36 | column: string; |
37 | foreignTable: string; |
38 | ascending?: boolean; |
39 | nullsFirst?: boolean; |
40 | }, |
41 | limit?: { |
42 | count: number; |
43 | foreignTable?: string; |
44 | }, |
45 | ) { |
46 | const headers = access_token ? { |
47 | global: { headers: { Authorization: `bearer ${access_token}` } }, |
48 | } : undefined |
49 | const client = createClient(auth.url, auth.key, headers); |
50 | const options = (head || count) ? { head, count } : undefined; |
51 | let query = client.from(table).select(columns || undefined, options); |
52 | if (filter?.column) { |
53 | query = query.filter(filter.column, filter.operator, filter.value); |
54 | } |
55 | if (order?.column) { |
56 | const { column, ...options } = order; |
57 | query = query.order(column, options); |
58 | } |
59 | if (limit?.count) { |
60 | const { count, foreignTable } = limit; |
61 | query = query.limit(count, foreignTable ? { foreignTable } : undefined); |
62 | } |
63 |
|
64 | return await query; |
65 | } |
66 |
|