-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
45 lines (37 loc) · 1.05 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import {
createClient,
SupabaseClient,
} from "https://esm.sh/@supabase/supabase-js@2";
// Environment variables
const SUPABASE_URL: string = Deno.env.get("SUPABASE_URL")!;
const SUPABASE_ANON_KEY: string = Deno.env.get("SUPABASE_ANON_KEY")!;
// Supabase client
const supabase: SupabaseClient = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
// Type for student data
type Student = {
id: string;
first_name: string;
last_name: string;
email: string;
age: number;
};
// Request handler
const handler = async (): Promise<Response> => {
const { data, error } = await supabase
.from<"students", Student[]>("students")
.select()
.limit(10);
if (error) {
// eslint-disable-next-line
console.error("Error fetching data:", error);
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
return new Response(JSON.stringify(data), {
headers: { "Content-Type": "application/json" },
});
};
// Start the server
Deno.serve(handler);