FireSQL transforms your SQL query into one or more queries to Firestore. Once all the necessary data has been retrieved, it does some internal processing in order to give you exactly what you asked for.
For example, take the following SQL:
SELECT *
FROM cities
WHERE country = 'USA' AND population > 50000
This would get transformed into this single Firestore query:
db.collection('cities')
.where('country', '==', 'USA')
.where('population', '>', 50000);
That's pretty straightforward. But what about this one?
SELECT *
FROM cities
WHERE country = 'USA' OR population > 50000
There's no direct way to perform an OR
query on Firestore so FireSQL splits that into 2 separate queries:
db.collection('cities').where('country', '==', 'USA');
db.collection('cities').where('population', '>', 50000);
The results are then merged and any possible duplicates are eliminated.
The same principle applies to any other query. Sometimes your SQL will result in a single Firestore query and some other times it might result in several.
For example, take a seemingly simple SQL statement like the following:
SELECT *
FROM cities
WHERE country != 'Japan' AND region IN ('north', 'east', 'west') AND (capital = true OR population > 100000)
This will need to launch a total of 12 concurrent queries to Firestore!
const cities = db.collection('cities');
cities.where('country', '<', 'Japan').where('region', '==', 'north').where('capital', '==', true);
cities.where('country', '<', 'Japan').where('region', '==', 'north').where('population', '>', 100000);
cities.where('country', '<', 'Japan').where('region', '==', 'east').where('capital', '==', true);
cities.where('country', '<', 'Japan').where('region', '==', 'east').where('population', '>', 100000);
cities.where('country', '<', 'Japan').where('region', '==', 'west').where('capital', '==', true);
cities.where('country', '<', 'Japan').where('region', '==', 'west').where('population', '>', 100000);
cities.where('country', '>', 'Japan').where('region', '==', 'north').where('capital', '==', true);
cities.where('country', '>', 'Japan').where('region', '==', 'north').where('population', '>', 100000);
cities.where('country', '>', 'Japan').where('region', '==', 'east').where('capital', '==', true);
cities.where('country', '>', 'Japan').where('region', '==', 'east').where('population', '>', 100000);
cities.where('country', '>', 'Japan').where('region', '==', 'west').where('capital', '==', true);
cities.where('country', '>', 'Japan').where('region', '==', 'west').where('population', '>', 100000);
As you can see, SQL offers a very concise and powerful way to express your query. But as they say, with great power comes great responsibility. Always be mindful of the underlying data model when using FireSQL.