简体   繁体   中英

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?

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 .

I figured out that you can get the types directly from firebase-admin without adding additional packages:

import { firestore } from 'firebase-admin'

const iAmConsumingDB = (db: firestore.Firestore) => {
  // do something
}

const iAmConsumingADocumentReference = (reference: firestore.DocumentReference) => {
  // do something
}
import { DocumentData } from '@firebase/firestore-types'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM