Closed sfxworks closed 7 years ago
Querying data is a bit tricky, I will explain the full scenario:
In my database I have a node named cms_news
, inside this node I have hundreds of entries, each one looks something like this:
title = "My Title"
description = "Some description"
timestamp = 100045454545
ispublished = true
Before querying this data we must add an index on the database rules. Go to the rules tab and copy this code:
"cms_news": {
".read": true,
".write": true,
".indexOn": [
"ispublished"
]
}
It is very important that the index key is the same as the key you want to search/filter, in this case it is ispublished
.
Now we want to query all the news entries that are published.
In SQL we would do something like:
SELECT * FROM cms_news WHERE ispublished = true
In Firebase we need to load this url:
https://YOUR-PROJECT-ID.firebaseio.com/cms_news.json?orderBy="ispublished"&equalTo=true
It is very important that ispublished
be surrounded by double quotes.
Your data would not be ordered, you will have to do a simple myArray.sort()
in AS3.
What about a scenario where you would want to look for the names of news articles? Typing in a word and getting .title's that match a string.
Unfortunately Firebase doesn't offer this functionality: http://stackoverflow.com/questions/38618953/how-to-do-a-simple-search-in-string-in-firebase-database
You can use this code that was suggested in one of the answers for something similar to a SQL LIKE:
//We have several entries with the title Promotion XYZ, we are going to find the ones that have the word Promo inside them.
var searchTerm:String = "Promo" //Capitalization does matter
var parameter1:String = "%22title%22";
var parameter2:String = "%22" + searchTerm + "%22";
var parameter3:String = "%22" + searchTerm + "\uf8ff%22";
var request:URLRequest = new URLRequest('https://<YOUR-PROJECT-ID>.firebaseio.com/cms_news.json?orderBy='+parameter1+'&startAt='+parameter2+'&endAt='+parameter3);
Edit: Added and tested the example code.
What if I use current values? Such as publishDate={todaysTimeStamp}
Can I get only the results of today's values for example?
Yes, that's possible. You will do something like the following:
//First get the timestamp of the current day, not the current time. You can use a calendar component.
var myDate:Number = myCalendar.selectedDate.getTime(); //This should get the timestamp at 00:00:01
var myDateAtMidnight:Number = myDate + 86400000; //We add the milliseconds to the date to cover the full range.
//Then you request this URL
'https://<YOUR-FIREBASE-PROJECT>.firebaseio.com/cms_news.json?orderBy="publishedDate"&startAt='+myDate+'&endAt='+myDateAtMidnight
and I would just do an indexBy timestamp for the rules, right?
That's correct, for every field you want to be able to search by you add it to the .indexOn
array:
"cms_news": {
".read": true,
".write": true,
".indexOn": [
"ispublished",
"title",
"timestamp",
"something"
]
}
Would this work for numbers too? Example: if I have a number of latitude and longitudes and I want to get something around a certain point?
Example: [35.2457298,-80.7758522] for the point Range 50 meters
To do this kind of search Firebase offers a library called GeoFire:
https://github.com/firebase/geofire-js
You will have to study how it makes the request and port the relevant part to AS3.
An alternative solution could be to create a proxy with this library:
Here's a guide to retrieve the parameters from an URL:
http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters
Once you have those parameters you can call your GeoFire query.
I've never played with a javascript api before. I usually only use vanilla. How do I handle geofire exactly?
Take it's library (.js files) and put all of it in one html file? And then load it using a url request... but then how would I call a function and all from it?
My experience with JS is limited as well, this is what I found:
Let's say I have a large number of users or items I want to go over. I obviously don't want to download the entire list and parse it (I could but that's bad practice for large scale lists).
How would I do that? I'm trying to translate this to the best of my ability. https://firebase.google.com/docs/reference/rest/database/#section-query-parameters