Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
Delphi

How To Use The Firebase API To Add, Read, and Delete Data In A Realtime Document-Oriented Database

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database Softacom Logo

Currently, the use of a remote database for storing various types of data (such as product and user information for an online store, patient details in a clinic, etc.) is highly relevant. Also, more and more developers want to create cross platform apps to access the data on device targets such as Windows, macOS, iOS, and Android.

For this purpose, both traditional relational databases and document-oriented databases can be employed. Document-oriented databases are more flexible as they provide users with a REST API for interaction.

Therefore, for data manipulation, it is necessary to use such requests as PUT, POST, GET, DELETE. They allow users to write, receive, and delete data. In contrast to relational databases, there is no need for additional drivers here. This significantly simplifies the development of software that should utilize a remote database. Data in such a database is represented in the JSON format.

In this article, we will demonstrate how to write, read, and delete data in a document-oriented Firebase Realtime Database using the Firebase API with an RAD Studio with Delphi FMX cross-platform application.

What is the Firebase Database API?

To work with the Firebase Realtime Database, we will use Firebase API.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

How to create a Firebase project?

To leverage the capabilities of the document-oriented Firebase Realtime database, it is necessary to create a Firebase project.

The process of creating a new Firebase project is explained in an article dedicated to user registration and authentication using the Firebase API.

In the existing project MyNewSoftacomProj, let’s select the Realtime Database tab.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Now let’s click on Create Database

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Then, we will need to choose the location for our database. Let’s leave as it is and we will have the United States. Now, let’s click on Next.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Now you need to choose either the locked mode (Start in locked mode) or the test mode (Start in test mode). In the test mode, the database will be available for 30 days. To ensure better security, it is recommended to choose the locked mode (in this mode, a secret key will be used to access the data).

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

How to find the URL for your Firebase database?

The Firebase Realtime Database has been created. You need to use a URL to access the database.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

To access the database in the secure mode, it is necessary to use a secret key. We should copy and save it for future access to the database from our Embarcadero Delphi FMX application. To do this, go to Project Settings.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Now let’s open the Service accounts tab.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Now let’s click on Database secrets.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

How to find the Firebase API code?

Here we can find our secret code. To view it we need to click on Show.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Now we should click on Copy and save our secret code for its further use in our Embarcadero Delphi FMX app.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

How to create a Delphi class save, read, and delete records in a document-oriented Firebase Realtime database?

As an example, we will be saving information about people (their names, surnames, professions) and retrieving this information from the Firebase Realtime Database. We will also implement the option to delete records from the database in our Delphi FMX application using the API.

For ease of working with the Firebase Realtime Database API, we have developed a special class called TFirebaseHelper.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

This class contains a constructor that takes an object of the TNetHttpClient class and string constant DatabaseURL and SecretKey as parameters. These constants hold the URL of the database and the secret key of the database for accessing it in the secure mode.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

The FormatJSON function allows formatting the response received from the Firebase Realtime Database and presenting it to the user in a convenient and readable form.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

The Put method takes an object Data of the TJsonObject class, which includes the necessary data to be added to the database (in our example, this is a name, surname, profession). In the Data object, parameters are stored in the “key-value” format.

The second input parameter is a string constant Path, which contains the path and name of the record being saved in the database. The parameters that should be written to the database (the Data object) are transmitted to the MultiPartFormData object (of the TMultiPartFormData class) with further adding to the database via a PUT request (using FNetHttpClient).

FNetHttpClient takes a string constant DatabaseURL as a parameter, which contains the URL of the Firebase Realtime Database.

In addition to DatabaseURL, another string constant Path is transmitted as well. It contains the path to the record and its name.

Moreover, FNetHttpClient accepts MultiPartFormData. After executing the PUT request, the JSON response from the server is stored in the ResponseContent of the TStringStream class.

The TFirebaseHelper class allows working with the database in the secure mode. Therefore, we will also pass the secret key (auth=SecretKey). The Put method also returns the JSON response from the Firebase Realtime Database as a string with the parameters sent to the database.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

The Get method takes a string constant Path as a parameter, containing the path to the required record and its name. FNetHttpClient performs a GET request to retrieve data from the Firebase Realtime Database. The Get method returns a JSON response from the Firebase Realtime Database as a string, which contains the records obtained from the database.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

The Delete method also takes a string constant Path as a parameter. It contains the path to the required record and its name for its further deletion from the Firebase Realtime Database. FNetHttpClient performs a DELETE request to remove the data from the Firebase Realtime Database.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Where can I find an example of how to access Firebase using Delphi?

Below you can find the source code of the TFirebaseHelper class.

How to add, read, and delete records from a Firebase realtime database with Delphi?

To interact with the Firebase Realtime Database API (to send PUT, GET, DELETE requests to the database in our Delphi FMX application), the TNetHttpClient component is used.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

In our application, we will transmit three parameters to the database: a name, surname, and profession. We will use TEdit components for entering these parameters.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Our application allows writing data to the database. It also enables retrieving all data from the database or a specific record based on the surname. There is also the possibility to delete a specific record using the surname. All these functions are implemented as handlers for corresponding buttons (of the TButton class).

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

The JSON response from the Firebase Realtime Database is displayed with the help of the TMemo component.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

We will declare two fields, FSecretKey and FDatabaseURL (the string type), which store the URL of the database and the secret key.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

In the OnCreate method of the main form (MainForm), we will connect the URL of the database and the secret code.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Let’s consider the implementation of a button handler that performs the writing of parameters to the database (PUT to DB). To carry out this task, we will declare a FirebaseObject object (of the IFirebaseHelper type). Also, for storing data with its further saving to the database, we will declare a JsonToDB object (of the TJSONObject type). For storing the JSON response from the service, we will declare a string variable AnswerFromFirebase.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Next, it is necessary to call the constructor of the TFirebaseHelper class and pass our NetHttpClient1 object (of the TNetHttpClient class) along with the values of the FDatabaseURL and FSecretKey fields.

2024 01 29 14 01 15

We will add parameters such as a name, surname, and profession to the JsonToDB object (parameters are stored in the “key-value” format) with their further saving to the database. We will read the parameter values from the corresponding TEdit input fields (EditName, EditSurname, EditProfession). If necessary, you can send an unlimited number of values using the AddPair method. In our example, we used three parameters (Name, Surname, Profession).

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Next, we will make a PUT request to add our parameters to the database. For this, we will use the Put method of the FirebaseObject. This method takes JsonToDB as input parameters (here our transmitted parameters are stored), the path, and the name of the record (in our case, the surname). It is necessary to perform all database requests using TTask to avoid blocking the interface of our Delphi FMX application. The response from the Firebase Realtime Database is stored in the string variable AnswerFromFirebase.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

To display the JSON response from the Firebase Realtime Database, we will use TThread.Synchronize (to update the TMemo component in the main thread).

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

You can see the code for the “PUT to DB” button handler below.

How do I save records to a Firebase realtime database?

Let’s demonstrate the capability of our Delphi FMX application to save records to the database using the PUT method. We will show the work of the application on the Windows platform.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Let’s consider the implementation of a button handler that reads records from the database (Get All from DB). To read all records from the database using the indicated path, we will apply the Get method of the TFirebaseHelper class. We will display the obtained values of the records in the form of a JSON response from the Firebase Realtime Database using the TMemo component.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

The code of the Get All from DB button handler looks like this:

How do I read data from a Firebase realtime database?

Let’s demonstrate the process of reading data from the database in our Delphi FMX application.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Our Delphi FMX application also allows reading data from the database based on the surname. For this purpose, a similar method Get is used. We will transmit the path to the record and the name of the record (surname).

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

You can see the code of the Get from DB by Surname button handler below.

Let’s demonstrate reading data from the database in our Delphi FMX application based on the surname.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

To delete records from the database in our Delphi FMX application, it is necessary to use the Delete method of the TFirebaseHelper class. As a parameter, we will transmit the path to the record and the surname of the person in the database whose record we want to delete.

We will display the deletion result to the user with the help of the TMemo component.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

The code for the Delete from DB button handler is provided below.

How do I delete data from a Firebase realtime database using Delphi?

Let’s demonstrate the process of deleting data from the database in our Delphi FMX application based on the surname.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

Let’s demonstrate the work of the application on the Android and MacOS platforms.

How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database
How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database

How To Record Exact Operation Execution Time With Delphi Delphi logo

Do you want to try some of these examples for yourself? Why not download a free trial of the latest version of RAD Studio with Delphi?

This article was written by Embarcadero Tech Partner Softacom. Softacom specialize in all sorts of software development focused on Delphi. Read more about their services on the Softacom website.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES