Seeking a solution to create reusable functions that can access Firestore Document/Collection references in both web and admin (node.js) environments.
For instance:
getUserDocumentReference(company: string, user: string) {
return firebase.collection("companies")
.doc(company)
.collection("users")
.doc(user);
}
This approach aims to minimize errors and ensure consistency across different platforms.
Challenge: While admin relies on firestore from firebase-admin
, the web platform imports it from firebase
.
Attempts have been made to create classes/functions where firestore
reference is passed in, but dealing with return types like below becomes cumbersome:
const ref = (
getUserDocumentReference("a", "1") as
firebase.firestore.DocumentReference
)
.withConverter(converter)
Is there a more efficient/cleaner method to achieve this without starting from scratch (maybe passing an array or reconstructing paths in a sophisticated manner)?
Approach currently employed:
class FirestoreReferences {
constructor(firestore: firebase.firestore.Firestore
| admin.firestore.Firestore) {
this.firestore = firestore;
}
getUserDocumentReference(company: string, user: string): FirebaseFirestore.DocumentReference | firebase.firestore.DocumentReference {
return this.firestore.collection(...).doc(...);
}
}