Gautier πŸ’™
Gautier πŸ’™

@mcflyDev

15 Tweets 270 reads Jan 06, 2022
I've worked with firebase and firestore for 3 years now.
Here's some things you should know to design your database πŸ‘‡πŸ§΅
#firebase #nosql #flutterdev #javascript
Firestore is a NOSQL database.
🧐Let’s see what is a NOSQL database.
This means that when in a standard RDBMS (sql database) you prefer to never repeat data, NOSQL is the opposite.
In NOSQL repeating data is not a bad practice.
Here is an example of some use case of sql vs nosql.
For some applications combining sql and nosql can be pretty great.
NOSQL = store large data and retrieve it fast
SQL = organize data but please take care of your table size and optimize for performance.
The CAP theorem
There is a rule in database.
You can’t have consistency / availability / partition tolerance.
You have to choose at max 2.
I would put Firestore as Availability / Partition.
Now we understand that firestore is about :
- high availability
- partition tolerance
- bad consistency
β™ŸοΈAll is about queries.
While you design your RDBMS with table and relations, try to design your firestore database with the queries you’ll need.
As I said It’s ok to duplicate data.
Let’s take an example.
You have a collection containing users informations.
You create a collection storing users's messages like a social feed.
This social post will contain fields:
- user id , user name
- message...
We won’t make a GET to get user name for each messages.
Counter example
You have a collection of buying events.
As you may want to have correct information, it’s ok to do another query to get user informations (for example as the user can update his own infos).
All is about balance between consistency and performance.
Firestore is document database
This means that firestore provides you the ability to create subdocument and create a real hierarchy.
Collection group query
As this is great you can also create queries known as collection group. So you can retrieve author with myUserId from any collection named reviews. Yes even if there is another collection named reviews anywhere else in your database.
Security rules
❌Don’t add user role into a collection
πŸ‘Œ Use the authentication claims.
+ You can access claims directly in security rules
+ user cannot access and update it
Complex string search
That’s not really where nosql shines... You won’t have that amazing functions that you have in sql (phonetic search, contains...).
πŸ‘‰Don’t have much data in your collection β‡’ retrieve it all and filter it in code.
πŸ‘‰Too much data β‡’ algolia or elastic
Here it is
Would have so much more to say about firebase or nosql but this will be for another thread.
Did you learn something here?
Share your experience using firebase and nosql πŸ‘‡
@tomlarkworthy You can have a document related to another.
Someone can update or delete one while many are fetching them.
What is the result of this?
Sql has complex locks that ensure that anyone touching their table will wait the end of operation and many other scenario.
@tomlarkworthy It can feel consistent on a small app because you don't have that many simultaneous write/reads.
But no. NoSQL is not consistent like a sql database by design.

Loading suggestions...