unfire
Version:
⚡️ Utilities for Firebase 🔥 built with Composition API, Typescript and ❤️
201 lines (200 loc) • 6.13 kB
JavaScript
// src/firebase/firestore.ts
import {
getFirestore,
doc,
collection,
setDoc,
getDoc as _getDoc,
getDocs as _getDocs,
deleteDoc as _deleteDoc,
onSnapshot as _onSnapshot,
query,
where,
limit,
orderBy
} from "@unfire-init";
import { ImportFirestoreSchemaByPath } from "@unfire-firestore-schema";
import { Timestamp } from "@unfire-init";
var defaultWhereQueryBuilder = (...query2) => query2;
var queryBuilder = ({
whereQueries,
orderByQuery,
limitBy
}) => {
return [
...whereQueries ? whereQueries({ where: defaultWhereQueryBuilder }).map((query2) => where(...query2)) : [],
...orderByQuery ? [orderBy(orderByQuery.fieldPath, orderByQuery.directionStr)] : [],
...limitBy ? [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 collection(firestoreInstance != null ? firestoreInstance : getFirestore(), collectionPath);
};
var getCollectionDocRef = (collectionPath, docId, firestoreInstance) => 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) : doc(createCollection(collectionPath));
try {
if (validation) {
const schema = customSchema != null ? customSchema : await ImportFirestoreSchemaByPath(collectionPath);
const schemaParseResult = schema == null ? void 0 : schema.safeParse(data);
(schemaParseResult == null ? void 0 : schemaParseResult.success) ? await 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 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 = query(createCollection(collectionPath), ...queryOption ? queryBuilder(queryOption) : []);
return {
unsubscribe: _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 _getDocs(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: _onSnapshot(docRef, snapshotListenOptions, {
next: (snapshot) => {
onSnapshot({ snapshot, docData: getDocData(snapshot) });
},
error: onError
})
};
} else if (collectionPath && docId) {
return {
unsubscribe: _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 _getDoc(docRef);
} else if (collectionPath && docId) {
documentSnapshot = await _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 _deleteDoc(docRef);
} else if (collectionPath && docId) {
return _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"];
export {
Timestamp,
createCollection,
defaultWhereQueryBuilder,
deleteDoc,
getCollectionDocRef,
getCollectionDocsOnce,
getDocOnce,
queryCollection,
queryDoc,
upsertDoc,
whereFilterOps
};