I have an array of strings and want to check which of all those documents exist in my Firestore in two ways:
I am using Node Admin SDK:
import Server from '../../../../firebase/firebase_server_exports';
export const databaseEntryExists = async (paths: string[]): Promise<string> => {
const promises = paths.map(
(element) =>
new Promise((resolve) => {
Server.db
.doc(element)
.get()
.then((doc) => resolve(doc.exists ? element : ''));
})
);
const results = await Promise.all(promises);
return results.includes(true) ? paths[results.indexOf(true)] : '';
};
Though this method works nicely, if I have 100 paths, I will be making 100 API calls which doesn't sound a good deal to me. Can someone suggest any Fierstore utility function or something like batch document fetching in order to reduce this to one API call?
In the Node.js SDK, you can create DocumentReference objects for all IDs and then use getAll() to get them all in one go. See the documentation for getAll() for an example.
This call does not exist in the client-side SDKs though, so the closest you can get there is to combine up to 10 IDs in a single call by querying on FieldPath.documentId() and using the in operator. There is nothing to request