0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FirebaseでcollectionGroupを使う際の注意

Last updated at Posted at 2020-04-13

いや、Firebase便利ですね。
さて、SubCollectionで結構悩んだので備忘録です

最初に

  • typescript使ってます。
  • Firebaseのインストールとかはここでは触れません

データ構造

channels
|
+- events(Sub Collection)
   +- event1
   +- event2
   +- event3
   ...続く

channelはこのような構造を持ちます

{
  id: string, // doc.idと同じ
  status: "active" | "inactive",
  name: string
}

eventはこのような感じにしました。

{
  id: string, // doc.idと同じ
  status: "open" | "close" | "draft",
  name: string
}

...まぁほぼ同じですが

TOPページにイベント一覧を表示させたいですね。
ここで様々なチャンネルが有るわけですが、こういう時にCollection Groupが便利です。


firebase
  .firestore()
  .collectionGroup("events")
  .where("status" "==", "open")
  .limit(20)
  .get()

Rulesはこんな感じです

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{path=**}/events/{event} {
      allow list: if resource.data.status == "open";
    }
    match /channels/{channel} {
      allow list, get: if resource.data.status == "active";
      match /events/{event} {
        allow list, get:
          if resource.data.status == "open"
          && get(/databases/{database}/documents/channels/{channel}).data.status == "active"
      }
    }
  }
}

望む機能

channels [x]inactiveの場合
|
+- events(SubCollection)
   +- event1 [x]表示できない 
   +- event2 [x]表示できない
   +- event3 [x]表示できない
   ...続く

さて、ここで一つ問題があります。
statusが"inactive"の場合でもchannelのeventドキュメントがcollectionGroup経由で取得できてしまいます。

良い方法ご存じの方教えて下さい(ToT/

0
0
2

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?