Firebase with Angular:从查询中排除字段

时间:2018-08-31 17:05:43

标签: javascript angular typescript firebase

我想知道我的结构是否可用于Firebase还是需要更改。这是/ rooms包含一个Room数组的一个示例:

export class Room {
    id: number;
    password: string;
    state: number;
    messages: Message[] = [];
}

和消息:

export class Message{
    author: string;
    message: string;
    playerExcluded: string;

}

现在,我必须在没有密码的情况下加载房间(我想这可能吗?)以及其中message.playerExcluded!= firebase.auth()。currentUser.uid的地方(看起来更难)。

这意味着每个用户将拥有除被排除的消息以外的所有消息。 一种解决方案可能是加载没有Room中的对象的字段(例如id,state),然后进行另一次加载Message的查询,但这似乎不是最佳选择。

在一个查询中可能还是太复杂?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

根据Firebase文档,您应该flatten your data,并且由于您已经在执行此操作,因此您的结构应该可以。这也意味着您将需要执行两次查询才能获取消息。


从Firebase文档中:

  

如果将数据拆分为单独的路径(也称为非规范化),则可以根据需要在单独的调用中有效地下载数据。考虑一下这种扁平化的结构:

{
  // Chats contains only meta info about each conversation
  // stored under the chats's unique ID
  "chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
      "timestamp": 1459361875666
    },
    "two": { ... },
    "three": { ... }
  },

  // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

  // Messages are separate from data we may want to iterate quickly
  // but still easily paginated and queried, and organized by chat
  // conversation ID
  "messages": {
    "one": {
      "m1": {
        "name": "eclarke",
        "message": "The relay seems to be malfunctioning.",
        "timestamp": 1459361875337
      },
      "m2": { ... },
      "m3": { ... }
    },
    "two": { ... },
    "three": { ... }
  }
}

也来自文档:

  

现在可以通过每次对话仅下载几个字节来遍历会议室列表,快速获取元数据以在UI中列出或显示会议室。可以分别提取消息并在到达时显示消息,从而使UI保持快速响应。

相关问题