In this post I'm documenting a few examples using the Firestore REST API. Most of the examples on Googles Firestore database will guide you to installing the sdk via npm, that is what I also did in my latest project, it works great. Firestore\Firebase is fantastic, especially for quick prototyping.
When I finally had my project ready to be deployed to the web I realized that my bundle size was huge, like > 2MB. Firestore was the biggest contributor to this size, so I started to look for ways to reduce the bundle size.
Unfortunately I was already using it in the best way possible. Something like this.
I then decided to try the REST API instead of the sdk. The only problem I had was that the documentation on using the REST API was hard to find. ( This might have change by the time you read this )
So here goes a few examples.
This is one of the simplest examples, it's a normal Get request to your collection.
This is assuming you have no authorization set on read operations. If you do you will have to add an authorization header with a Bearer token.
Here we are just adding the document id to the end of the URL, to return this one document only.
In this example we are sending a post request with the structuredQuery object and this will run a query on Firestore and return only the data we are interested in.
Here you just need to pay attention to the format of the object you are sending to Firestore.
This was the toughest part to figure out, after a lot of googling someone guided me in the right direction and I finally got it right. Apart from passing the object in the same way we did for the Post request you also specify the field to update in the URL query params.
This is a easy one, pass in your project id, collection name and doc id.
I hope this is helpful for someone else who also wants to use these APIs. Using REST instead of the sdk worked out well for me, I had a huge reduction in my bundle size.
Thanks for reading!