UNPKG

@dill-pixel/storage-adapter-firebase

Version:

Firebase Storage Adapter

234 lines (233 loc) 7.98 kB
import { StorageAdapter as u, Logger as b, isDev as f } from "dill-pixel"; import { initializeApp as w } from "firebase/app"; import { getFirestore as g, doc as a, addDoc as I, collection as s, setDoc as A, getDoc as d, query as E, where as S, getDocs as c, deleteDoc as h } from "firebase/firestore"; const _ = "6.2.3", y = "10.14.1"; class v extends u { /** * Returns the Firebase app. * @returns {FirebaseApp} The Firebase app. */ get firebaseApp() { return this._firebaseApp; } /** * Returns the Firestore database. * @returns {Firestore} The Firestore database. */ get db() { return this._db; } hello() { const i = `%c Dill Pixel Firebase Storage Adapter v${_} | %cFirebase v${y}`; console.log( i, "background: rgba(31, 41, 55, 1);color: #74b64c", "background: rgba(31, 41, 55, 1);color: #e91e63", "background: rgba(31, 41, 55, 1);color: #74b64c" ), this._options.debug && b.log(this._options); } /** * Initializes the adapter. * @param {IApplication} _app The application that the adapter belongs to. * @param {FirebaseOptions} options The options to initialize the adapter with. * @returns {void} */ initialize(i, t) { const r = { debug: f, apiKey: t.env.VITE_FIREBASE_API_KEY || t.env.FIREBASE_API_KEY, authDomain: t.env.VITE_FIREBASE_AUTH_DOMAIN || t.env.FIREBASE_AUTH_DOMAIN, projectId: t.env.VITE_FIREBASE_PROJECT_ID || t.env.FIREBASE_PROJECT_ID, storageBucket: t.env.VITE_FIREBASE_STORAGE_BUCKET || t.env.FIREBASE_STORAGE_BUCKET, messagingSenderId: t.env.VITE_FIREBASE_MESSAGING_SENDER_ID || t.env.FIREBASE_MESSAGING_SENDER_ID, appId: t.env.VITE_FIREBASE_APP_ID || t.env.FIREBASE_APP_ID }; this._options = { ...r, ...i }, this._firebaseApp = w(this._options), this._db = g(this._firebaseApp), this.hello(); } /** * Save or update a document in a collection. * @param {string} collectionName The name of the collection. * @param {DocumentData} data The data to save or update. * @param {string} id The ID of the document to save or update, if applicable. * @returns {Promise<DocumentResult>} The saved or updated document. * * @example * await this.app.firebase.save('users', { username: 'relish', score: 50 }, 'custom-id'); */ async save(i, t, r) { if (!this.db) throw new Error("Firestore has not been initialized. Call initialize() first."); let e; try { r ? e = a(this.db, i, r) : e = await I(s(this.db, i), t), await A(e, t, { merge: !0 }); const o = await d(e); return { id: o.id, ...o.data() }; } catch (o) { throw new Error(`Error saving document: ${o}`); } } /** * Get a single document by its ID. * @param {string} collectionName The name of the collection. * @param {string} id The ID of the document to get. * @returns {Promise<DocumentResult | null>} The document, or null if not found. * * @example * await this.app.firebase.getDocumentById('users', 'custom-id'); */ async getDocumentById(i, t) { if (!this.db) throw new Error("Firestore has not been initialized. Call initialize() first."); const r = a(this.db, i, t); try { const e = await d(r); if (!e.exists()) return null; const o = { id: e.id, ...e.data() }; return o || null; } catch (e) { throw new Error(`Error getting document: ${e}`); } } /** * Get a single document by a field value. * @param {string} collectionName The name of the collection. * @param {string} field The field to query. * @param {unknown} value The value to query. * @returns {Promise<DocumentResult | null>} The document, or null if not found. * * @example * await this.app.firebase.getDocumentByField('users', 'username', 'relish'); */ async getDocumentWhere(i, t, r) { if (!this.db) throw new Error("Firestore has not been initialized. Call initialize() first."); try { const e = s(this.db, i), o = E(e, S(t, "==", r)), l = await c(o); if (l.empty) return null; { const n = l.docs[0]; return { id: n.id, ...n.data() }; } } catch (e) { throw new Error(`Error getting document: ${e}`); } } /** * Get all documents in a collection. * @param {string} collectionName The name of the collection. * @returns {Promise<DocumentResult[]>} An array of documents. * * @example * await this.app.firebase.getCollection('users'); */ async getCollection(i) { if (!this.db) throw new Error("Firestore has not been initialized. Call initialize() first."); try { const t = s(this.db, i), r = await c(t), e = []; return r.forEach((o) => { e.push({ id: o.id, ...o.data() }); }), e; } catch (t) { throw new Error(`Error getting collection: ${t}`); } } /** * Delete a document by its ID. * @param {string} collectionName The name of the collection. * @param {string} id The ID of the document to delete. * @returns {Promise<DocumentResult | null>} The deleted document, or null if not found. * * @example * await this.app.firebase.deleteDocumentById('users', 'custom-id'); */ async deleteDocumentById(i, t) { if (!this.db) throw new Error("Firestore has not been initialized. Call initialize() first."); try { const r = a(this.db, i, t), e = await d(r); return e.exists() ? (await h(r), { id: e.id, ...e.data() }) : null; } catch (r) { throw new Error(`Error deleting document: ${r}`); } } /** * Delete a document by a field value. * @param {string} collectionName The name of the collection. * @param {string} field The field to query. * @param {unknown} value The value to query. * @returns {Promise<DocumentResult | null>} The deleted document, or null if not found. * * @example * await this.app.firebase.deleteDocumentByField('users', 'username', 'relish'); */ async deleteDocumentWhere(i, t, r) { if (!this.db) throw new Error("Firestore has not been initialized. Call initialize() first."); try { const e = await this.getDocumentWhere(i, t, r); if (!e) return null; const o = a(this.db, i, e.id); return await h(o), e; } catch (e) { throw new Error(`Error deleting document: ${e}`); } } /** * Delete all documents in a collection. * @param {string} collectionName The name of the collection. * @returns {Promise<void>} A promise that resolves when the operation is complete. * * @example * await this.app.firebase.deleteCollection('users'); */ async deleteCollection(i) { if (!this.db) throw new Error("Firestore has not been initialized. Call initialize() first."); try { const t = s(this.db, i), r = await c(t), e = []; r.forEach((o) => { e.push(h(o.ref)); }), await Promise.all(e); } catch (t) { throw new Error(`Error deleting collection: ${t}`); } } /** * Query a collection. * @param {string} collectionName The name of the collection. * @param {QueryConstraint[]} queries The query constraints to apply. * @returns {Promise<DocumentResult[]>} An array of documents. * * @example * await this.app.firebase.queryCollection('users', where('score', '>', 0), limit(10)); */ async queryCollection(i, ...t) { if (!this.db) throw new Error("Firestore has not been initialized. Call initialize() first."); const r = []; try { const e = s(this.db, i), o = E(e, ...t); return (await c(o)).forEach((n) => { r.push({ id: n.id, ...n.data() }); }), r; } catch (e) { throw new Error(`Error querying collection: ${e}`); } } } export { v as FirebaseAdapter, v as default }; //# sourceMappingURL=dill-pixel-storage-adapter-firebase.mjs.map