UNPKG

unfire

Version:

⚡️ Utilities for Firebase 🔥 built with Composition API, Typescript and ❤️

222 lines (220 loc) 7.89 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/firebase/firestore.ts var firestore_exports = {}; __export(firestore_exports, { Timestamp: () => import_unfire_init2.Timestamp, createCollection: () => createCollection, defaultWhereQueryBuilder: () => defaultWhereQueryBuilder, deleteDoc: () => deleteDoc, getCollectionDocRef: () => getCollectionDocRef, getCollectionDocsOnce: () => getCollectionDocsOnce, getDocOnce: () => getDocOnce, queryCollection: () => queryCollection, queryDoc: () => queryDoc, upsertDoc: () => upsertDoc, whereFilterOps: () => whereFilterOps }); module.exports = __toCommonJS(firestore_exports); var import_unfire_init = require("@unfire-init"); var import_unfire_firestore_schema = require("@unfire-firestore-schema"); var import_unfire_init2 = require("@unfire-init"); var defaultWhereQueryBuilder = (...query2) => query2; var queryBuilder = ({ whereQueries, orderByQuery, limitBy }) => { return [ ...whereQueries ? whereQueries({ where: defaultWhereQueryBuilder }).map((query2) => (0, import_unfire_init.where)(...query2)) : [], ...orderByQuery ? [(0, import_unfire_init.orderBy)(orderByQuery.fieldPath, orderByQuery.directionStr)] : [], ...limitBy ? [(0, import_unfire_init.limit)(limitBy)] : [] ]; }; var getDocData = (docRef) => { const data = docRef.data(); if (data) { Object.defineProperty(data, "id", { value: docRef.id.toString(), writable: false }); } return data; }; var createCollection = (collectionPath, firestoreInstance) => { console.log("in createCollection"); return (0, import_unfire_init.collection)(firestoreInstance != null ? firestoreInstance : (0, import_unfire_init.getFirestore)(), collectionPath); }; var getCollectionDocRef = (collectionPath, docId, firestoreInstance) => (0, import_unfire_init.doc)(createCollection(collectionPath, firestoreInstance), docId); var upsertDoc = async (options) => { var _a; const { data, collectionPath, docId, docRef, setOption, validation = true, customSchema, onSuccess, onError } = options; const ref = docRef != null ? docRef : docId ? getCollectionDocRef(collectionPath, docId) : (0, import_unfire_init.doc)(createCollection(collectionPath)); try { if (validation) { const schema = customSchema != null ? customSchema : await (0, import_unfire_firestore_schema.ImportFirestoreSchemaByPath)(collectionPath); const schemaParseResult = schema == null ? void 0 : schema.safeParse(data); (schemaParseResult == null ? void 0 : schemaParseResult.success) ? await (0, import_unfire_init.setDoc)(ref, schemaParseResult.data, setOption != null ? setOption : { merge: true }).then(() => { onSuccess == null ? void 0 : onSuccess(schemaParseResult.data); }) : onError == null ? void 0 : onError((_a = schemaParseResult == null ? void 0 : schemaParseResult.error) != null ? _a : new Error("No Schema Found")); } else { await (0, import_unfire_init.setDoc)(ref, data, setOption != null ? setOption : { merge: true }).then(() => { onSuccess == null ? void 0 : onSuccess(data); }); } } catch (error) { onError == null ? void 0 : onError(error); } return ref; }; var queryCollection = (options) => { const { collectionPath, onSnapshot, onError, queryOption, snapshotListenOptions = { includeMetadataChanges: false } } = options; try { const q = (0, import_unfire_init.query)(createCollection(collectionPath), ...queryOption ? queryBuilder(queryOption) : []); return { unsubscribe: (0, import_unfire_init.onSnapshot)(q, snapshotListenOptions, { next: (snapshot) => { onSnapshot({ snapshot, docsData: snapshot.docs.map(getDocData) }); }, error: onError }) }; } catch (error) { onError == null ? void 0 : onError(error); return { error }; } }; var getCollectionDocsOnce = async (options) => { const { collectionPath, queryOption, onError } = options; try { const snapshot = await (0, import_unfire_init.getDocs)((0, import_unfire_init.query)(createCollection(collectionPath), ...queryOption ? queryBuilder(queryOption) : [])); return { snapshot, docsData: snapshot.docs.map(getDocData) }; } catch (error) { onError == null ? void 0 : onError(error); return { error }; } }; var queryDoc = (options) => { const { collectionPath, docId, docRef, onSnapshot, onError, snapshotListenOptions = { includeMetadataChanges: false } } = options; try { if (docRef) { return { unsubscribe: (0, import_unfire_init.onSnapshot)(docRef, snapshotListenOptions, { next: (snapshot) => { onSnapshot({ snapshot, docData: getDocData(snapshot) }); }, error: onError }) }; } else if (collectionPath && docId) { return { unsubscribe: (0, import_unfire_init.onSnapshot)(getCollectionDocRef(collectionPath, docId), snapshotListenOptions, { next: (snapshot) => { onSnapshot({ snapshot, docData: getDocData(snapshot) }); }, error: onError }) }; } else { const error = new Error('must provide "collectionPath" and "docId" or "docRef"'); onError == null ? void 0 : onError(error); return { error }; } } catch (error) { onError == null ? void 0 : onError(error); return { error }; } }; var getDocOnce = async (options) => { const { collectionPath, docId, docRef, firestoreInstance, onError } = options; try { let documentSnapshot; let error; if (docRef) { documentSnapshot = await (0, import_unfire_init.getDoc)(docRef); } else if (collectionPath && docId) { documentSnapshot = await (0, import_unfire_init.getDoc)(getCollectionDocRef(collectionPath, docId, firestoreInstance)); } else { error = new Error('must provide "collectionPath" and "docId" or "docRef"'); onError == null ? void 0 : onError(error); } return { documentSnapshot, data: documentSnapshot ? getDocData(documentSnapshot) : void 0, error }; } catch (error) { onError == null ? void 0 : onError(error); return { error }; } }; var deleteDoc = (options) => { const { collectionPath, docId, docRef, onError } = options; try { if (docRef) { return (0, import_unfire_init.deleteDoc)(docRef); } else if (collectionPath && docId) { return (0, import_unfire_init.deleteDoc)(getCollectionDocRef(collectionPath, docId)); } else { throw new Error('must provide "collectionPath" and "docId" or "docRef"'); } } catch (error) { onError == null ? void 0 : onError(error); } }; var whereFilterOps = ["==", "!=", ">", ">=", "<", "<=", "array-contains", "array-contains-any", "in", "not-in"]; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Timestamp, createCollection, defaultWhereQueryBuilder, deleteDoc, getCollectionDocRef, getCollectionDocsOnce, getDocOnce, queryCollection, queryDoc, upsertDoc, whereFilterOps });