LoginSignup
7
9

More than 3 years have passed since last update.

firestoreでよく使う「型」「メソッド」「返り値」まとめ(CollectionReference, QuerySnapshot...など)

Last updated at Posted at 2020-11-13

きっかけ

firestoreを使って開発していますが、

  • CollectionReferenceとかQuerySnapshotとか種類多すぎてわからん…
  • get()とかdata()は何に呼び出されて、何返すんだっけ?

と思うことがよくあり、その都度API documentを参照しています。
この時間がもったいないなと思ったので、すぐにわかるようまとめておきます。
同じ思いをしている誰かのためになればと思います🙏

前提

Nuxt.jsでfireAuthから呼び出す前提とします。

javascript
await this.$fireStore.collection(`/users/${userId}/items`).get()

この記事で出てくる型の種類

  1. Firestore
  2. CollectionReference
  3. DocumentReference
  4. DocumentSnapshot
  5. Query
  6. QuerySnapshot
  7. QueryDocumenSnapshot

型ごとによく使うメソッドと返り値

  • 1. Firestore
    • doc()
      • DocumentReferenceをかえす
      • ex) this.$fireStore.doc('LA')
    • collection()
      • CollectionReferenceをかえす
      • ex) this.$fireStore.collection('cities')

  • 2. CollectionReference
    • id
      • Stringをかえす(コレクションのID)
      • ex) this.$fireStore.collection('cities').id
    • doc()
      • DocumentReferenceをかえす
      • ex) this.$fireStore.collection('cities').doc('LA')
    • get()
      • Promise<QuerySnapshot>をかえす
      • ex) await this.$fireStore.collection('cities').get()
    • where()
      • Queryをかえす
      • ex) this.$fireStore.collection('cities').where(townId, 'in', townIds)
    • orderBy()
      • Queryをかえす
      • ex) this.$fireStore.collection('cities').orderBy('createdAt', 'desc')

  • 3. DocumentReference
    • id
      • Stringをかえす(ドキュメントのID)
      • ex) this.$fireStore.collection('cities').doc('LA').id
    • get()
      • Promise<DocumentSnapshot>をかえす
      • ex) await this.$fireStore.doc('LA').get()
    • set()
      • Promise<void>をかえす
      • ex) await this.$fireStore.doc('LA').set({ ... })
    • delete()
      • Promise<void>をかえす
      • ex) await this.$fireStore.doc('LA').delete()
    • collection()
      • CollectionReferenceをかえす
      • ex) this.$fireStore.doc('LA').collection('towns')

  • 4. DocumentSnapshot
    • ref
      • DocumentReferenceをかえす
      • ex) this.$fireStore.collection('cities').where(townId, 'in', townIds).ref
    • get()
      • anyをかえす
      • ex) this.$fireStore.collection('cities').where(townId, 'in', townIds).get('townName')
    • data()
      • T or undefinedをかえす(Tはオブジェクト、自分で型を指定する)
      • ドキュメント内のすべてのフィールドを返す
      • ex) this.$fireStore.collection('cities').where(townId, 'in', townIds).data()

  • 5. Query
    • get()
      • Promise<DocumentSnapshot>をかえす
      • ex) await this.$fireStore.collection('cities').where(townId, 'in', townIds).get()

  • 6. QuerySnapshot
    • docs
      • Array<QueryDocumentSnapshot>をかえす
      • ex) this.$fireStore.collection('cities').get().docs
    • forEach()
      • voidをかえす(処理を実行するだけ)
      • ex) this.$fireStore.collection('cities').get().docs.forEach(doc => {...})

  • 7. QueryDocumentSnapshot
    • id
      • Stringをかえす(そのドキュメントのID)
      • ex) this.$fireStore.collection('cities').get().docs.map(doc => { doc.id })
    • ref
      • DocumentReferenceをかえす
      • ex) this.$fireStore.collection('cities').get().docs.map(doc => { doc.ref })
    • get()
      • anyをかえす
      • ex) this.$fireStore.collection('cities').get().docs.map(doc => { doc.get('townId') })
    • data()
      • T or undefinedをかえす(Tはオブジェクト、自分で型を指定する)
      • ドキュメント内のすべてのフィールドを返す
      • ex) this.$fireStore.collection('cities').get().docs.map(doc => { doc.data() })

https://firebase.google.com/docs/reference/js/firebase.firestore.QueryDocumentSnapshot

ねがい

これでだいぶ楽になるといいな…🙏

7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9