Configuring DB connectors - Firestore

Studio enables you to use the database connectors to connect to your databases like Elasticsearch, MongoDB, CouchDB, DynamoDB, Cloud DataStore, Firestore, and so on.

Firestore is a highly durable database that can scale automatically to handle your application’s load.

Configuring Firestore connector

To add a DB connector, under Studio > Connectors, click (+) Connector and under Databases select the Firestore connector.

You need to first configure the database. Enter the Connector name which would be used when you need the connector in your apps and while adding queries.

To build on top of your Firestore data, you can preferably make use of the Firebase console. Under Firebase console, click on your Project settings, and choose the “Service accounts” tab. Select the “Firebase Admin SDK” icon, and click “Generate new private key”. A JSON file is then downloaded, from where you can copy the Private key and paste it into the Firestore Connector.

Optionally you can also create an account key from your Google Cloud Platform console. Select a project, and click Create Service Account and assign the “role” as Firebase Admin. Create a JSON Key type and download the file. Keep it handy for use for connector configuration.

Once these configurations are added, click Test Connection. If the authentication is successful you would get the response accordingly. Click Save to Finish configuration.

Adding queries to the Database connector

Now that you have configured the connector you will find it under the Connectors list. Now to fetch data or undertake any other action, click the Add query option available once your connection is ready. Firestore contains Collections and you can then have various actions to be performed on these Collections. The Collections contain the Documents which in turn contain the fields to store the data. You can use different types of actions to Query database, Insert document, Update Document, Get Document by ID, Delete Document, Get Collections, and so on.

So click Add Query and create a query. Enter a Service name and select your query Action and click Run or press Ctrl + enter to run the query. The rows returned from the database are shown in the Response section.

You can also add dynamic values using the Variables. To add the variable, click Variables > +Add. Add the FieldName and select the Field type. Ensure that you select the field type that matches the column type of the collection so that the query works correctly. Add a Test Value to check the results before saving. You can also add a Default value or make it Mandatory. Add the Help text that would be shown with the field. Once you have entered the details, click Add Variable. The variable would be listed under Variables. You can make use of the variable in the query where you created the variable.

To use a variable inside a query, you simply need to put it into double Curly brackets. The Test value that you entered would be considered for fetching data. When using the connector queries the dynamic variables would have to be linked to the respective control for further run-time functioning.

You can now view the queries that you saved for use in your apps later under your specific connector under Custom Database connectors.

Using Firestore connector

Query Firestore

Now let us take a simple example to fetch the Data from a Collection. A sample is shown in the illustration below.

Select the action as Query Firestore, provide the Collection name, and you can also add the Filter, Sort, and Order data and set the Limit. Add the query as seen above and Run it to verify the output. Once done Save the query.

Add a tablegrid control. Go to Bind Data> Connectors and configure the query.

Select the keys to display the data from the respective collection

So now when you take a preview of the respective form you would be able to see the data for the specific Collection.

Add Document

You can add documents to the collection with the respective fields. In this case, choose the action as Insert document. You can provide a Document ID if you want to use a specific one else you can keep it blank. The Document ID will be auto-generated. The data should be provided in a valid JSON format.

In this example, you would be accepting the data from a form and adding it on the Click of your Action button. Hence you will configure the Connector from the Action Flow > Server Side actions and add the respective controls as Keywords.

So whenever you run your form and click to submit data, the action flow would be triggered and the Connector will enable you to save data to your Firestore entity. You can use a popup or toast action to display the ID created or view it in a tablegrid like the one we created in the previous example.

Now when you run the form you can add the Keyname and the data for first name and last name and submit the data. If you take a preview you can note that the data has been saved.

Update Document

You can also update and delete documents from the Collection. You can add a scenario like selecting the record from the tablegrid and then adding an action to update or delete. In this scenario, you need to use the Update Document. Add a query to update the document. It needs the Document ID as the parameter and the values to be updated can be provided as dynamic variables.

The query would be as seen in the example below.

Now whenever you select the row of your tablegrid control and click the Update action button, the record is to be updated. So define the action flow for the Update_click event.
cllick event

Configure the action flow. Under Server Side actions select the Firestore connector and click Continue. From the list of queries select the Update query, select the account and then configure the connector. Pass the values as Keywords from the form to configure the connector’s fields.

Now, whenever you run the form you can select a row and click the update action.

You can see that the selected record is updated.