Joining Flattened Data

后端 未结 1 1707
小蘑菇
小蘑菇 2020-11-30 10:06

i\'d like to join the data on init from my customers table into the projects list.

Model is like this:

  • projects

    • key
    • na
相关标签:
1条回答
  • 2020-11-30 10:46

    Not exactly sure how your dataset looks like, so I'm just going to write a basic example. Assuming a structure something like this:

    - projects
      - key
        - name: string
        - customers
          - customerKey: boolean
    
    - customers
      - key
        - name: string
    

    Example data

    - projects
      - projectId1
        - name: "Cool project!",
        - customers
          - customerId1: true,
          - customerId2: true
      - projectId2
        - name: "Another cool project!",
        - customers
          - customerId2: true,
          - customerId3: true
    
    - customers
      - customerId1
        - name: "John Smith"
      - customerId2
        - name: "John Doe"
      - customerId3
        - name: "John John"
    

    So we're storing the customers' key in every projects' customers property.

    Let's say we want to list every projects, but we also want to get the customers' real name as well, not just their id. Since firebase doesn't have joins we'll have to do this manually. Here's one way to do it:

    this.projects = this.af.database.list(`projects`)
      .map(projects => {
        return projects.map(project => {
          project.customers.map(customer => {
            this.af.database.list(`customers`)
              .subscribe(c => {
                customer = c;
              });
          });
          return project;
        });
      });
    

    The inner .subscribe could be changed to a simple .map if you want to get the data asynchronously (in this case use the async pipe in the template`).

    0 讨论(0)
提交回复
热议问题