UNPKG

@dill-pixel/storage-adapter-supabase

Version:

Supabase Storage Adapter

96 lines (95 loc) 3.49 kB
import { createClient as n } from "@supabase/supabase-js"; import { StorageAdapter as i, Logger as a } from "dill-pixel"; const c = "5.0.3", l = "2.50.0", u = "1.19.4"; class h extends i { /** * Returns the Supabase client. * @returns {SupabaseClient<Database>} The Supabase client. */ get client() { return this._supabase; } hello() { const s = `%c Dill Pixel Supabase Adapter v${c} | %cSupabase v${l} | %cSupabase Postgres v${u}`; console.log( s, "background: rgba(31, 41, 55, 1);color: #74b64c", "background: rgba(31, 41, 55, 1);color: #e91e63", "background: rgba(31, 41, 55, 1);color: #e91e63", "background: rgba(31, 41, 55, 1);color: #74b64c", "background: rgba(31, 41, 55, 1);color: #74b64c" ), this._options.debug && a.log(this._options); } /** * Initializes the adapter. * @param {IApplication} _app The application that the adapter belongs to. * @param {ISupabaseAdapterOptions} options The options to initialize the adapter with. * @returns {void} */ initialize(s, e = {}) { a.log("SupabaseAdapter initialized"); const t = { supabaseUrl: s.env.VITE_SUPABASE_URL || s.env.SUPABASE_URL, anonKey: s.env.VITE_SUPABASE_ANON_KEY || s.env.SUPABASE_ANON_KEY }; if (this._options = { ...t, ...e }, this.hello(), !this._options.supabaseUrl) throw new Error("Supabase URL is not set"); if (!this._options.anonKey) throw new Error("Supabase anon key is not set"); this._supabase = n(this._options.supabaseUrl, this._options.anonKey); } /** * Saves data to a specified table in the Supabase database. * @param {string} tableId The table to save the data to. * @param {any} data The data to save. * @param {SaveMethod} method The method to use for saving the data. * @returns {Promise<any>} The saved data. * * @example * await this.app.supabase.save('scores', { username: 'relish', score: 50 }) */ async save(s, e, t = "upsert") { Array.isArray(e) || (e = [e]); const o = this.client.from(s); switch (t) { case "insert": return await o.insert(e).select(); case "update": return await o.update(e).select(); case "upsert": return await o.upsert(e).select(); } } /** * Loads data from a specified table in the Supabase database. * @param {string} tableId The table from which to load the data. * @param {string[]} selectors The columns to select. Default is '*'. * @returns {PostgrestFilterBuilder<any, any, any>} PostgrestFilterBuilder // TODO * * @example * await this.app.supabase.load('scores', ['score', 'username']).order('score', { ascending: false }).limit(5) */ async load(s, e) { const t = this.client.from(s).select(e == null ? void 0 : e.join(",")), { data: o, error: r } = await t; if (r) throw r; return o; } /** * Deletes data from a specified table in the Supabase database. * @param {string} tableId The table from which to load the data. * @param {DeleteData} data The data to delete. * @returns {Promise<any>} The deleted data. * * @example * await this.app.supabase.delete('scores', { username: 'relish', score: 50 }) */ async delete(s, e) { const t = Object.keys(e)[0], o = e[t]; return await this.client.from(s).delete().eq(t, o).select(); } } export { h as SupabaseAdapter, h as default }; //# sourceMappingURL=dill-pixel-storage-adapter-supabase.mjs.map