REST API for Google Firestore Tutorial

Thanks a lot for your prompt reply. Unfortunately I couldnt find a way to upload any image from my phone to any database via api with appgyver… Only https://docs.appgyver.com/tutorials/uploading-files method is working but this very very slow or not working at all on live app. If you can suggest any other method, I really appreciate…

I have not used it, but from the Google photos API reference, I see that an AppGyver integrated data resource could be configured to POST a media item (photo, video) to a user photos library (remember there is no storage limit in Google Photos if the resolution adheres to Google’s cap)

I wrote a better looking and, hopefully, better explained Firestore REST API direct integration tutorial.
If there is any question or error :scream:, or something is not clear, please drop a note here.

2 Likes

@Eduardo_Jaramillo Thanks for putting this guide together! QQ for you…

The create record works fine when I test the schema in the Data Resource API configurator and in the web preview app. In both instances the new test record shows up in my firestore DB.

But when I test the create record logic node in the mobile preview app it doesn’t work properly. Looking at the debugger the http request goes through fine but it doesn’t actually create a record in firestore (currently I have security rules off so I don’t believe that’s the issue).

Any idea why it works on web but not on mobile?

I have tried the same logic on both, web and mobile (android) preview app and it does create the new record, so I am not sure what’s happening. If you send me a snapshot of your create record logic node and data resource (post) setup I will gladly take a look at it.

1 Like

I’m not either. I’m using the iOS preview app so perhaps that has something to do with it. :man_shrugging:

Thanks for giving it a look over @Eduardo_Jaramillo !

Here’s the logic node flow & node itself

:

I’ve also tried the above create record after executing dismiss initial view node in a different place in the nav stack and also removed the check device token/notification nodes.

Here’s the binding for the create record node:


Here’s the POST setup:

Here’s the Schema:


Here’s the successful test record:


William,
I don’t find anything wrong with the setup.
The only thing that comes to my mind that might be a problem is the Firestore authorization, if you have application restrictions (found on the API Keys in the Google Cloud console):

because Firestore OAuth 2.0 tokens are different for android apps, web and ios apps.

1 Like

Thanks Eduardo! I double checked and I have no restrictions, though, I don’t think I have the API key anywhere in AppGyver. The google API was automatically created through Firebase where I setup separate apps for iOS, Android, & Web.

I’m not sure what the issue is. I think I’m just going to use the Real Time Database which writes just fine on the iOS preview app for the time being.

EDIT: Resolved but saved answer for posterity – see below

Hi @Eduardo_Jaramillo,

I’ve followed your instructions exactly (at least, I think so!) but every time I run a Test on the GET COLLECTION, I see the following error:

"Error: TypeError: Failed to fetch. Does the server allow CORS?status: undefined
Resource settings for Get collection (GET)

Resource URL: https://firestore.googleapis.com/v1
Relative path: ​ /projects/[redacted]/databases/(default)/documents/./test

What am I doing wrong? I have my Rules set to test mode (“allow read, write: if true;”) so I don’t think I should need to set up authorization?

UPDATE: ANSWERED

After spending a ridiculous amount of time troubleshooting, here’s the answer: For whatever reason it’s very important to Appgyver that the forward slash between the Base URL and the Relative URL be put in the Resource URL field of the Base tab (i.e., “https://firestore.googleapis.com/v1/”). This is shown incorrectly in the tutorial (see screenshot under Data Resource Setup: Base). So moving that one slash from the beginning of the Relative URL (in GET tab) to the end of the Resource URL (in Base tab) solved my problem.

I recommend replacing that screenshot in the tutorial – or better yet updating this part of the Appgyver interface to concatenate better and allow both placements of the forward slash as long as they’re consistent!

Sorry for the late answer, but was away.
Appgyver does not require the slash at the end of the url on the base tab.
The after mentioned error is the result of a wrong full URL (base+relative path), as having both an slash at the end of the base and another one at the beginning of the relative path, so there will be two successive slashes on the full path.

@Eduardo_Jaramillo I think I see what you mean – AppGyver assumes a forward slash at the end of Base URL, whether you put it there or not? Still, the point stands that you can’t start your Relative URL with a slash or else it will throw an error (even if you did not end your Base URL with a slash), so I still think the tutorial needs to be updated to reflect that.

I do have another question that’s hopefully less nitpicky! I’m having some trouble understanding schemas. In my Firestore database I have documents with several different schemas (e.g. one document with two boolean fields and a number field, another document with 5 text fields). How does this map onto Appgyver data sources? Do I need to create a schema that includes every field that every one of my documents might contain? Do I need to set separate data sources for each separate type of document? Or is there a better way to do this?

Thanks for your help,
~Ethan

It works either way:
image image

Or
image image

I have both configurations in the same app and both work. In the first data resource configuration the slash is at the end of the Resource URL, and in the second is in the Relative path. AppGyver makes no assumption about the slash placement; what is important is that there is only one slash, because de full path string is the concatenation of the Resource URL and the Relative path.

As far as I know, your schema in AppGyver has to include all the fields on the database, or the documents that don’t adhere to the schema will not be retrieved. In your example, the schema has to have the two boolean, the number and the five text fields.

1 Like

Hello @Eduardo_Jaramillo/All,
Saw your comment about you being able to implement Firebase auth using OAuth2. Can you please assist by showing how you did using AppGyver Data Configurator? I mean how to get the authorization code and then using it to get the access Token. Also, what AppGyver URI to use for the redirect call back from the authorization server.
Worth mentioning that, for test purposes, i got Postman to work nicely with the Firebase OAuth2.

Thanks

You are a lifesaver @Eduardo_Jaramillo.

I have had many sleepless nights on this matter.

I will implement this right away.

I have a question though. This question is from the perspective of someone used to SQL database.

I have multiple tables (Apologies for my SQL grammar) for my database. These include; User, Sales, Products, Customers, etc. These tables have a many-to-many relationship and I want to recreate them in Firestore’s database.

I would love to know how I can start to think about them.

Here are my questions:

  1. Should I create multiple collections where each collection represent each table? or should I make each table a Sub-collection of the User table?

  2. How do I go about relating them to each other?

  3. When I connect them to my Appgyver data Configurator, should I make them different resources or a single resource?

Thank you

Hi Startup,

The paradigm of SQL and noSQL databases is so different that would suggest another approach if you are to implement your application in Firestore.
The design of a noSQL database does not start from the data, but from the info you want to retrieve from it. In other words, first you think of what the users need to retrive and how is it to be presented, and then you think about the database structure.
Suggested basic reading: https://www.mongodb.com/nosql-explained/data-modeling#:~:text=NoSQL%20databases%20are%20designed%20to,the%20beginning%20of%20a%20schema.
How to Design Schema for Your NoSQL Database? - DATAVERSITY

Best regards,

Eduardo

Thank you very much, Eduardo.

Hello @Eduardo_Jaramillo ,

I have a collection of 400 documents in my Firestore Database and I followed the documentation tutorial for setting up my AppGyver Data Configurator, but I’m just getting the 20 first documents of my collection.

Maybe it’s related to that, but, first, I had an error when running my test, so I modified my rules to that:
rules_version = ‘2’;
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow write: if request.auth != null; //need to be authenticated to write (or use “if false”)
allow read: if true;
}
}
}

I’m also having that problem: if there are 50 records in some path, GET COLLECTION from the DATA CONFIGURATOR only retrieves up to exactly 20 records! Why is that and how do we fix it??