Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Firestore dependencies and types to node.js

I'm recently updating my cloud functions to TypeScript after the talks on the FirebaseSummit of this year.

All my code looks quite cool, but I'm having some problems trying to recover the types of Firestore API, such QuerySnapshot , DocumentReference...

async function getEventsMadeByUser(userId: string): Promise<FirebaseFirestore.DocumentSnapshot[]> {
    const eventsMadeByUserQuery = await instance.collection('PrivateUserData')
        .doc(userId).collection(EVENTS)
        .where('interactionUser', '==', userId).get();
    return eventsMadeByUserQuery.docs;
}

Right now my IDE (WebStorm) is not getting the types for FirebaseFirestore. This is how my package.json looks:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "dependencies": {
    "firebase-admin": "^5.4.3",
    "firebase-functions": "^0.7.2"
  },
  "devDependencies": {
    "@types/express": "^4.0.37",
    "typescript": "^2.3.2"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc --watch",
    "deploy": "tsc && firebase deploy --only functions"
  },
  "main": "build/index.js",
  "private": true
}

I have already tried to add @firebase-firestore and nothing, it's not working. What is the right dependency to achieve this?

like image 835
Francisco Durdin Garcia Avatar asked Apr 21 '26 23:04

Francisco Durdin Garcia


1 Answers

I finally figured it out! The firebase-admin module is just a simple wrapper that initializes Firestore for you. To get the real Firestore, add "@google-cloud/firestore": "^0.9.0" to your package.json. You can admire those beautiful types here: https://github.com/googleapis/nodejs-firestore/blob/master/types/firestore.d.ts.

like image 80
SUPERCILEX Avatar answered Apr 25 '26 23:04

SUPERCILEX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!