Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add supabase-storage driver #403

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/2.drivers/supabase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
icon: ri:supabase-line
---

# Supabase Storage

> Store data in Supabase Storage.

::read-more{to="https://supabase.com/docs/guides/storage"}
Learn more about Supabase Storage.
::

::warning
Supabase Storage driver is in beta.
::

To use it, you will need to install `@supabase/supabase-js` in your project

```js
import { createStorage } from "unstorage";
import supabaseStorageDriver from "unstorage/drivers/supabase-storage";

const storage = createStorage({
driver: supabaseStorageDriver({
url: "<your Supabase project URL>",
key: "<your Supabase project API key>",
bucket: "<your Supabase project storage bucket name>",
}),
});
```

**Options:**

- `base`: [optional] Prefix to use for all keys. Can be used for namespacing.
- `url`: The unique Supabase URL which is supplied when you create a new project in your project dashboard.
- `key`: The unique Supabase Key which is supplied when you create a new project in your project dashboard.
- `bucket`: The Supabase storage bucket name.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"@types/jsdom": "^21.1.6",
"@types/mri": "^1.1.5",
"@types/node": "^20.11.5",
"@supabase/storage-js": "2.5.5",
"@supabase/supabase-js": "^2.39.7",
"@upstash/redis": "^1.28.1",
"@vercel/kv": "^0.2.4",
"@vitejs/plugin-vue": "^5.0.3",
Expand Down Expand Up @@ -106,6 +108,7 @@
"@capacitor/preferences": "^5.0.6",
"@netlify/blobs": "^6.4.2",
"@planetscale/database": "^1.13.0",
"@supabase/supabase-js": "^2.39.7",
"@upstash/redis": "^1.28.1",
"@vercel/kv": "^0.2.4",
"idb-keyval": "^6.2.1"
Expand Down Expand Up @@ -138,6 +141,9 @@
"@planetscale/database": {
"optional": true
},
"@supabase/supabase-js": {
"optional": true
},
"@upstash/redis": {
"optional": true
},
Expand Down
73 changes: 73 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 135 additions & 0 deletions src/drivers/supabase-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { SupabaseClient, createClient } from "@supabase/supabase-js";
import { createError, defineDriver, joinKeys, normalizeKey } from "./utils";

export interface SupabaseOptions {
base?: string;
url?: string;
key?: string;
bucket?: string;
}

const DRIVER_NAME = "supabase-storage";

export default defineDriver((opts: SupabaseOptions) => {
if (!opts.url) {
throw createError(DRIVER_NAME, "url");
}
if (!opts.key) {
throw createError(DRIVER_NAME, "key");
}
if (!opts.bucket) {
throw createError(DRIVER_NAME, "bucket");
}

const r = (key: string = "") => {
return (opts.base ? joinKeys(opts.base, key) : normalizeKey(key)).replace(
/:/g,
"/"
);
};

let client: SupabaseClient;

const getClient = () => {
if (!client) {
client = createClient(opts.url!, opts.key!);
}
return client;
};

const getKeys = async (prefix: string): Promise<string[]> => {
const { data, error } = await getClient()
.storage.from(opts.bucket!)
.list(prefix);

if (error) throw error;
if (!data) return [];

const keys: string[] = [];
for (const { name, id } of data) {
const key = `${prefix !== "" ? prefix + "/" : ""}${name}`;
// If it's a folder, get the keys inside. The Supabase docs do not mention how to differentiate between a file and a folder, but it is observed that a folder has an id with a value of null.
if (!id) {
keys.push(...(await getKeys(key)));
} else {
keys.push(key);
}
}
return keys;
};

const getMeta = async (key: string) => {
const segments = r(key).split("/");
const prefix = segments.slice(0, -1).join("/");
const name = segments.at(-1);
const { data, error } = await getClient()
.storage.from(opts.bucket!)
.list(prefix, {
search: name,
});

if (error) throw error;
if (data.length === 0 || !data[0].id) return null;
return {
...data[0],
};
};

return {
name: DRIVER_NAME,
options: opts,
async hasItem(key) {
return getMeta(key).then(Boolean);
},
async getItem(key) {
const { data, error } = await getClient()
.storage.from(opts.bucket!)
.download(r(key));

if (error) return null;
return await data.text();
},
async getItemRaw(key) {
const { data, error } = await getClient()
.storage.from(opts.bucket!)
.download(r(key));

if (error) return null;
return data.arrayBuffer();
},
async setItem(key, value) {
const { error } = await getClient()
.storage.from(opts.bucket!)
.upload(r(key), value, {
upsert: true,
});

if (error) throw error;
},
async setItemRaw(key, value) {
const { error } = await getClient()
.storage.from(opts.bucket!)
.upload(r(key), value, {
upsert: true,
});

if (error) throw error;
},
async removeItem(key) {
const { error } = await getClient()
.storage.from(opts.bucket!)
.remove([r(key)]);

if (error) throw error;
},
getMeta,
async getKeys(base) {
const keys = await getKeys(r(base));
return opts.base ? keys.map((key) => key.slice(opts.base!.length)) : keys;
},
async clear(base) {
const keys = await getKeys(r(base));
await getClient().storage.from(opts.bucket!).remove(keys);
},
};
});
Loading