The easiest way to integrate Supabase with Elysia.
- Authentication Check: Verify user authentication using bearer tokens (default for
supabse-jsinvoke) - User Data Access: Easily retrieve data for the authenticated user
- Client Creation: Get Supabase client instance for the authenticated user
- Flexible Configuration: Use environment variables or custom settings
- Edge Function Compatible: Seamless integration with Supabase Edge Functions
import { Elysia } from "elysia";
import { supabase } from "@mastermakrela/elysia-supabase";
const app = new Elysia()
.use(supabase())
.get("/", ({ supabase, user }) => `Hi ${user.email}`)
.get("/data", async ({ supabase, error }) => {
const resp = await supabase.from("table").select("*");
if (resp.error) {
return error(500, resp.error.message);
}
return resp.data;
})
.listen(3000);bunx jsr add @mastermakrela/elysia-supabaseimport { supabase } from "jsr:@mastermakrela/elysia-supabase";The supabase function accepts the same arguments as the createClient function from @supabase/supabase-js.
import { Elysia } from "elysia";
import { supabase } from "@mastermakrela/elysia-supabase";
const app = new Elysia()
.use(
supabase("https://my-supabase-url.supabase.co", "anon-key", {
global: {
fetch: fetch,
},
})
)
.get("/", ({ supabase, user }) => `Hi ${user.email}`)
.listen(3000);If the URL and/or anon key are not provided, the plugin will use the environment variables:
SUPABASE_URLSUPABASE_ANON_KEY
In the Edge Function environment, SUPABASE_URL and SUPABASE_ANON_KEY are pre-set. Use a Deno server and mount Elysia for compatibility:
import { Elysia } from "npm:elysia";
import { cors } from "npm:@elysiajs/cors";
import { supabase } from "jsr:@mastermakrela/elysia-supabase";
const app = new Elysia()
.use(cors())
.use(supabase())
.get("/hello", ({ supabase, user }) => `Hi ${user.email}`);
Deno.serve(app.fetch);Note: The cors plugin is added to allow browser invocation of the Edge Function.
Then in the client we can call it using:
const { data, error } = await supabase.functions.invoke("hello");url: Supabase project URL (optional ifSUPABASE_URLis set)key: Supabase project API key (optional ifSUPABASE_ANON_KEYis set)options: Additional Supabase client options
Returns an Elysia plugin that adds supabase and user to the Elysia context.