Database Style Queries with Firebase

Is there a quick way to make database-style queries with Firebase?

For example:

Given a link to firebase users with user_id , name and age fields, what would be the best way to make a request like this:

 SELECT name FROM users WHERE `user_id`=147; 

and

 SELECT COUNT(*) FROM users WHERE age=21; 
+46
javascript firebase
Jul 20 2018-12-22T00:
source share
3 answers

In general, no. Firebase is essentially a “real-time database,” constantly updating you as data changes, making it more difficult to make general-purpose queries. Currently, there are several (supposedly limited) query primitives that are provided. See the Requests / Constraints page in the documents.

You can often get around these limitations with various approaches:

  • Using location names and priorities is wise. If you structure your data as / users / [userid] / name, you can complete your first “request” by simply loading / users / 147 / name. If you know what you want to request by age, you can use age as a priority for user nodes, and then run "usersRef.startAt (21) .endAt (21) .on (" child_added ", ...)" to get All users over the age of 21. You still have to count them manually.
  • Fulfill client-side requests. If the whole data set is small, you can get the entire data set and then filter / process it manually on the client.
  • Start a separate server . It can connect to Firebase, synchronize data, and then respond to "requests" for clients. It can still communicate with clients through Firebase, and Firebase can still be the main data warehouse, but your separate server can do the job of quickly executing requests.

We intend to improve this over time, as we understand this weak point compared to the flexible query provided by traditional relational database systems.

+69
Jul 20 '12 at 23:21
source share
— -

Mr. Lehenbauer, of course, is the master of all Firebase stuff, so listen to him .;) However, this particular topic is something that I have been working on for a couple of weeks.

Here are a few of my thoughts to improve the responses to “Launch a separate server” and “Client request”:

ElasticSearch (a node.js script)

When using the node.js script on the server, you can integrate ElasticSearch and provide a solid data search within an hour. Here's a blog post and lib that makes it even easier: https://www.firebase.com/blog/2014-01-02-queries-part-two.html

cached / general requests

They can be processed by the server / cron process, which reads the table and duplicates the data. For example, suppose I want to show "unavailable / available" for a user login during registration, but store user records by a different unique identifier for some complicated reason.

My cron / server can read all the records from the user table and then insert them into another table, which is stored by email address, with the value of the user record identifier (or any other data that I might want to know).

This duplicate data approach is a manual caching method and is a common practice in non-SQL environments; We trade storage space (which is considered cheap and affordable) to expedite and simplify processes.

customized queries (using the queue)

User requests can be sent via XHR (ajax) directly to the server, which can do the hard work and return the best results. Alternatively, you can use Firebase to connect to the server server using the queue.

The client puts the request request as JSON in a special Firebase table called queue and waits for a response.

The server listens on queue.on('child_added', ...) and returns data back using `queue_record.child ('response', ... data here ...)

This has some nice advantages. For example, any number of servers can listen and serve responses, making load balancing a breeze. The code for this is very simplified to configure in another thread here in SO.

Hope this will be helpful!

+23
Jul 23 2018-12-12T00:
source share

I created my own CMS for firebase, so by creating a firebase data table, I filtered it with this

 var child = ref.child(); var compare; switch(filter){ case "First_Name": compare = child.First_Name; break; case "Last_Name": compare = child.Last_Name; break; case "Phone_Number": compare = child.Phone_Number; break; case "Department_Number": compare = child.Department_Number; break; case "Position": compare = child.Position; break; case "Status": compare = child.Status; break; case "Tier": compare = child.Tier; break; } if(compare.match("^" + string)){ //display items 
+2
Jul 12 '14 at 19:35
source share



All Articles