Skip to content

mastermakrela/elysia-supabase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elysia Supabase Plugin

The easiest way to integrate Supabase with Elysia.

Features

  • Authentication Check: Verify user authentication using bearer tokens (default for supabse-js invoke)
  • 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

Quick Start

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);

Installation

Using JSR (recommended)

bunx jsr add @mastermakrela/elysia-supabase

In Deno

import { supabase } from "jsr:@mastermakrela/elysia-supabase";

Configuration

The supabase function accepts the same arguments as the createClient function from @supabase/supabase-js.

Custom Configuration Example

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);

Environment Variables

If the URL and/or anon key are not provided, the plugin will use the environment variables:

  • SUPABASE_URL
  • SUPABASE_ANON_KEY

Usage in Supabase Edge Functions

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");

API Reference

supabase(url?: string, key?: string, options?: SupabaseClientOptions)

  • url: Supabase project URL (optional if SUPABASE_URL is set)
  • key: Supabase project API key (optional if SUPABASE_ANON_KEY is set)
  • options: Additional Supabase client options

Returns an Elysia plugin that adds supabase and user to the Elysia context.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors