如何在Firebase中按关键字过滤?

时间:2018-07-20 11:09:06

标签: firebase firebase-realtime-database

我有这样的Firebase数据库结构

{
  "5ZhJG1NACDdG9WoNZWrBoYGkIpD3" : {
    "Company" : {
      "5ZhJG1NACDdG9WoNZWrBoYGkIpD3" : {
        "authTime" : 1532061957,
        "companyName" : "Scopic Software",
        "contactName" : "Hoang Scopic",
        "email" : "hoang.trinh@scopicsoftware.com",
        "firebaseID" : "5ZhJG1NACDdG9WoNZWrBoYGkIpD3",
        "isFirstLogin" : false,
        "phoneNumber" : "1234567890",
        "schoolName" : "MIT",
        "teachers" : {
          "aOpjnzHpDiZ7uwQQqJoinGvM9ZD3" : "0"
        }
      }
    }
  },
  "AhZc9B02goOtZ6qBNhz9W0K6Esg2" : {
    "Subscription" : {
      "-LHlQ4OhijzzFY5HZOT4" : {
        "firebaseID" : "-LHlQ4OhijzzFY5HZOT4",
        "period" : {
          "endAt" : "1533194625",
          "startAt" : "1531985025"
        },
        "status" : "trial"
      }
    },
    "Teacher" : {
      "AhZc9B02goOtZ6qBNhz9W0K6Esg2" : {
        "authTime" : 1532061932,
        "email" : "hoang.trinhj@gmail.com",
        "firebaseID" : "AhZc9B02goOtZ6qBNhz9W0K6Esg2",
        "isFirstLogin" : false,
        "name" : "Hoang Trinh",
        "schoolName" : "HUST",
        "subscriptions" : {
          "-LHlQ4OhijzzFY5HZOT4" : "0"
        }
      }
    }
  },
  "aOpjnzHpDiZ7uwQQqJoinGvM9ZD3" : {
    "Subscription" : {
      "-LHlWnpNZazBC5lpXLi0" : {
        "firebaseID" : "-LHlWnpNZazBC5lpXLi0",
        "period" : {
          "endAt" : "1533196388",
          "startAt" : "1531986788"
        },
        "status" : "trial"
      }
    },
    "Teacher" : {
      "aOpjnzHpDiZ7uwQQqJoinGvM9ZD3" : {
        "Company" : {
          "5ZhJG1NACDdG9WoNZWrBoYGkIpD3" : "0"
        },
        "authTime" : 1532060884,
        "email" : "piavghoang@gmail.com",
        "firebaseID" : "aOpjnzHpDiZ7uwQQqJoinGvM9ZD3",
        "isFirstLogin" : false,
        "subscriptions" : {
          "-LHlWnpNZazBC5lpXLi0" : "0"
        }
      }
    }
  },
  "ooR32SjABdOYEkWX6dzy4fE5Kym1" : {
    "Admin" : {
      "email" : "admin@test.com",
      "firstName" : "Hoang",
      "lastName" : "Trinh",
      "password" : "123456xx"
    },
    "isAdmin" : true
  }
}

这是现有系统中的数据,因此我无法更改其结构。

现在,我需要编写一个API来列出通过“ email”属性(嵌套在键中)过滤的这些记录。

如何搜索此电子邮件?

我想到了一种解决方案。只是取回整个json并以代码处理数据(不使用Firebase数据库查询功能)。

但是我不能这样做,因为在过滤之后,我还需要进行分页。

为了进行分页,我需要使用“ sortByChild”或“ limitToLast”之类的查询函数,因此必须将这些查询函数与过滤一起使用。

谢谢。

编辑: 我当前的分页实现:

getUsers: async (page, perPage, searchTerm) => {
    const db = fbAdmin.db();
    let dbRef = await db.ref();

    if (searchTerm) {
      // TODO: Add filter logic here
    }

    let totalItems = await dbRef.once('value');
    let totalItemsNumber = await Object.keys(totalItems.toJSON()).length;

    // Calculate for pagination
    let lastItemId;

    if ((page - 1) * perPage + 1 > totalItemsNumber) {
      lastItemId = null;
    } else {
      let lastItems = await dbRef.orderByKey().limitToLast((page - 1) * perPage + 1).once('value');
      lastItemId = Object.keys(lastItems.toJSON())[0];
    }

    // Do the pagination
    let snap;
    let data;

    if (lastItemId) {
      snap = await dbRef.orderByKey().endAt(lastItemId).limitToLast(perPage).once('value');
      data = snap.toJSON();
    } else {
      data = {};
    }

    let users = Object.keys(data).map(key => {
      if (data[key][KEY]) { // Check if it's an admin

        return {
          role: TYPE_ADMIN,
          email: data[key][KEY].email,
          name: data[key][KEY].firstName + " " + data[key][KEY].lastName
        }

      } else if (data[key][Company.KEY]) { // Check if it's a company member

        return {
          role: TYPE_COMPANY_MEMBER,
          email: data[key][Company.KEY][key].email,
          name: data[key][Company.KEY][key].name,
          schoolName: data[key][Company.KEY][key].schoolName
        }

      } else if (data[key][Teacher.KEY]) { // Check if it's a teacher

        if (data[key][Teacher.KEY][key][Company.KEY]) { // Check if it's a company teacher

          return {
            role: TYPE_COMPANY_TEACHER,
            email: data[key][Teacher.KEY][key].email,
            name: data[key][Teacher.KEY][key].name,
            schoolName: data[key][Teacher.KEY][key].schoolName,
            subscription: data[key][Subscription.KEY]
          }

        } else { // Check if it's an individual teacher

          return {
            role: TYPE_INDIVIDUAL_TEACHER,
            email: data[key][Teacher.KEY][key].email,
            name: data[key][Teacher.KEY][key].name,
            schoolName: data[key][Teacher.KEY][key].schoolName,
            subscription: data[key][Subscription.KEY]
          }

        }

      } else {
        return null;
      }
    });

    return users || null;
  },

0 个答案:

没有答案