简体   繁体   English

Firebase 实时数据库 - 过滤查询不在客户端 Web/React

[英]Firebase realtime database - filtering query not on client side Web/React

Quite new to Firebase and I'm facing some issue on the logic on querying/filtering the needed requests. Firebase 很新,我在查询/过滤所需请求的逻辑上遇到了一些问题。

I have my users stored in the /users and they have a list of projects such as:我将我的用户存储在 /users 中,他们有一个项目列表,例如:

users : {
  userA : {
    projects: {
      projectId1: true,
      projectId2: true
    },
    ...
  }
...
}

And obviously I have the projects as such:显然我有这样的项目:

projects: {
  projectId1: {
    name: "bla"
  }
  ...
}

I want for a user to query all the projects that are in his projects list based on their Ids.我想让用户根据他们的 ID 查询他的项目列表中的所有项目。 Right now I only succeed to query every single projects of the database and their filter on the client side but obviously this has some serious security implication and loading time as well as I don't want anyone to query all the projects and get them.现在我只成功地查询了数据库的每个项目及其在客户端的过滤器,但显然这有一些严重的安全隐患和加载时间,而且我不希望任何人查询所有项目并获取它们。 I can add security rules but then I have access to nothing as I can't query /projects/ anymore but need to be specific.我可以添加安全规则,但之后我无法访问任何内容,因为我无法再查询 /projects/ 但需要具体说明。

I'm using https://github.com/CSFrequency/react-firebase-hooks/tree/master/database我正在使用https://github.com/CSFrequency/react-firebase-hooks/tree/master/database

and getting the data as such:并这样获取数据:

  const [projects, loading, error] = useListVals(firebase.db.ref("projects"), {
    keyField: "uid",
  });

And so would like to be able to add an array of projected in this request like where({ id is included in [projectsId]})因此希望能够在此请求中添加一个投影数组,例如 where({ id is included in [projectsId]})

You'll need to load each individual project for the user separately, pretty much like a client-side join operation.您需要分别为用户加载每个单独的项目,这与客户端加入操作非常相似。 This is not nearly as slow as you may think, as Firebase pipelines the operations over a single connection .这并不像您想象的那么慢,因为 Firebase 通过单个连接流水线操作

I don't see anything built into the library you use for such client-side joins, but in regular JavaScript it's something like this:我没有看到您用于此类客户端连接的库中内置的任何内容,但在常规 JavaScript 中,它是这样的:

let userRef = firebase.database().ref('users').child(firebase.auth().currentUser.uid);
userRef.once('value').then((projectKeys) => {
  let promises = [];
  projectSnapshot.forEach((projectKey) => {
    let key = projectKey.key;
    let projectRef = firebase.database().ref('projects').child(key);
    promises.push(projectRef.once('value');
  });
  Promise.all(promises).then((snapshots) => {
    console.log(snapshots.map(snapshot => snapshot.val()));
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM