polymerfire icon indicating copy to clipboard operation
polymerfire copied to clipboard

Firebase-Query Should Always Return An Array

Open phidias51 opened this issue 7 years ago • 2 comments

I have a database structure that looks like this:

documents/[[projectId]]/[[documentId]]/

  • title
  • description
  • tags -1: "preclinical" -2: "mouse data"

When I query using the following path: /documents/someProjectId/someDocumentId/tags I get an array of key/value objects. [{key: 1, value: "preclinical"},{key: 2, value: "mouse data"}].

What I would like is that regardless of the path that I use to query the data, I should always get an array of values back. If the tags node doesn't exist, I should get an empty array. If it has a single node in it, I should get an array with that node value.

If I want to get key/value pairs, then there should be a way of getting the result as a list of key/value pairs as shown above. Perhaps with some query attribute that says give me key/pair values.

The main thing is that I should always get the same result back, in a fashion that makes it easily usable in a dom-repeat template, or an iron-list.

phidias51 avatar Jun 20 '17 22:06 phidias51

I don't think I get it. When do you ever not get an array with firebase-query? Or do you mean we should get nested arrays rather than arrays with objects? Because then I (and everybody else, I promise) disagree. We want a list for the top level then below that we want to be able to access keys based on their names, so we definitively want arrays containing key:value objects.

It's easy enough to do [[__toArrayFn(item.some_list)]] anyway?

I get an array of key/value objects. [{key: 1, value: "preclinical"},{key: 2, value: "mouse data"}].

Do you? Shouldnt it be [{"1": "preclinical"}, {"2": "mouse data"}]? Which is exactly how it should be

tograd avatar Jun 21 '17 02:06 tograd

If I use firebase-document to get the document node and all of its subnodes, I get the following error when I attempt to iterate over the tag nodes with a dom-repeat template:

[dom-repeat::dom-repeat]: expected array for `items`, found {0: "pre-clinical", -Kn6gHwAkFpJk2o4h3j_: "mouse"}

Which means that for every document I need to run two queries to get the data that I need. I also can't directly bind document.tags to a list or a dom-repeat element.

The second problem is when you have an empty array of tags. Since firebase doesn't store empty nodes, you end up with an incomplete document object. If you attempt to bind tags to something, an exception is usually thrown saying that the attribute is undefined. If it simply returned an empty array that would be fine, I could use tags.push("something") to push new tags to the array. But because it's storing it as an object, I can't do that. I have to get a reference to the query that provided the data and push to that query. The query is often in a top-level component, and simply bound at that level to a subcomponent. Which means that events to mutate the tags have to bubble up to the top level.

phidias51 avatar Jun 21 '17 18:06 phidias51