Connect a mobile device to the database without writing server code

Hello, Habr! I want to share my experience using the Foresight mobile platform .



If you are faced with the task of remotely storing data without having to write your own server, the first thing that comes to mind is the Firebase Realtime Database tool. Most readers know what is at stake, but for the rest I’ll explain. Using Firebase Database, you can remotely store data in NoSql form.





Picture from firebase





From the advantages of using:





Of the minuses:





FMP + PostgreSQL



Now I want to tell you about an alternative tool that I have to use.



I’ll go from afar. Somewhere I have a raised PostreSQL server with a database. In the database, I have a table with fruits:







What is PostreSQL how to work with it can be read here .



My task is to get this table on the phone as quickly as possible with the least code writing and ensure security at every stage.



Here is the my_table_fruits table in pgAdmin4 :







Next I find the pgAdmin4 procedure in the tree. I am writing my own procedure for getting the table:







The procedure will be called fruits_get_by_color and will give me fruits of a certain color from my my_table_fruits table.



After that, I switch to the Foresight server of the mobile platform (hereinafter FMF). How to deploy the platform and what you need can be found in the documentation . FMF is a server that has connectors to database servers. It can also connect to the PostgreSQL we need. There is no need to write code for this. What I have to work with is the admin panel via the browser. Enter the necessary addresses of servers, databases. The admin panel has settings and an environment tree:







You can also read more about the settings in the documentation.



Unfortunately, the FMF does not have any trial versions or free periods. Also, the FMF is deployed in a secure customer infrastructure. Therefore, I can’t provide links to the server to try. But, to understand the essence of the work, the following information is quite enough.



I create a new environment in the environment tree, for example, I will call it Leonid_environment. Inside the environment, I create a project, for example, Leonid_project. I go into the project and create a data source there, in our case PostgreSQL. I enter the server and database parameters in which my table is stored:







I go inside the data source and click “Import”. I enter in the name of the source the name of the procedure that I introduced in PostgreSQL, in this case fruits_get_by_color. For the mobile client, you can come up with a different name, although not necessarily. I will name it fruits_get_by_color_for_android:







After clicking the “Import” button, the FMF platform will “see” our procedure and the parameters that we must transfer there.







In particular, she will see that we must pass the fruit_color parameter:







After that, go to the “API Users” tab and create a user. Created a user with a Leonid username and password 123123:







From this moment, you can go to Android Studio to get this data on your mobile phone.



Fmp + android



I am creating a new project. Together with the platform, a framework for working with it is distributed. I pull the framework into the project File → import module → * .aar







In the project, I declare a number of constants into which I insert the server address, as well as the name of the environment that I invented, the name of the project, login, password, resource name from the FMP:



private static final String MY_URL = "http://mobilefmp.dev.fs.world"; private static final VersionAPI MY_VERSION_API = VersionAPI.V_1; private static final String MY_ENVIRONMENT = "Leonid_environment"; private static final String MY_PROJECT = "Leonid_project"; private static final String MY_VERSION = "v1"; private static final String MY_LOGIN = "Leonid"; private static final String MY_PASSWORD = "123123"; private static final String MY_RESOURCE = "fruits_get_by_color_for_android";
      
      





Please note that we are referring specifically to the FMP, and not directly to PostgreSQL. Next, create a HyperHive object and populate the declared variables:



 hyperHive = new HyperHiveState(getApplicationContext()) .setHostWithSchema(MY_URL) .setApiVersion(MY_VERSION_API) .setEnvironmentSlug(MY_ENVIRONMENT) .setProjectSlug(MY_PROJECT) .setVersionProject(MY_VERSION) .buildHyperHive();
      
      





Then you need to perform authorization. To do this, pass the username and password to the auth () method:



 boolean status = hyperHive.authAPI.auth(MY_LOGIN, MY_PASSWORD, true).execute().isOk();
      
      





When true, you can query the contents of the table, which we will do. In the tableStreamCallParams object, we pass the fruit_color parameter in the form of json and enter the yellow value for it. Let me remind you that we created a fruit table and created a procedure in PostreSQL that takes a color parameter as input. This was necessary to enter this parameter on the device:



  TableStreamCallParams tableStreamCallParams = new TableStreamCallParams(); String data = "{\"fruit_color\": \"yellow\"}"; tableStreamCallParams.setData(data); String status = hyperHive.requestAPI.tableStream(MY_RESOURCE, tableStreamCallParams).execute();
      
      





After the request, we turn to the FMP server. The FMP server accesses the PostgreSQL server. As a result, we should get a list of yellow fruits:







Strings stream into the SQLite database. In this case, the colors yellow were only banana and lemon. The download speed of cached data on the FMF side is approximately 10,000 lines per second at normal Internet speed.



All the steps I described can be read in the documentation . Also there is information about connecting to iOs and other operating systems.



I give the full activation code .



I made a request from the main thread and did not process errors to reduce the amount of code.



Functional FMP



The FMF functionality is not limited to this. You can create a procedure on the server side of the database not only for receiving, but also for creating new rows depending on the values ​​passed. You can pass arrays of values. Limitations are only inside the database itself.



I will go through broad steps in the basic functionality of the FMP:





There are frameworks for:





There are connectors to:





Pros and Cons of FMP



I will give the pros and cons that I see from my point of view. Depending on the specifics and size of the project, I think you will not agree with everything.



Pros:





Minuses:





findings



Comparison of the FMF platform with the Firebase Realtime Database was “far-fetched”, as I wanted to at least compare something with the familiar android and iOs developers. In fact, the PMF has a slightly different functionality and purpose. Among the competitors are SAP Mobile Platform, IBM first mobile platform, Optimum CDC.



In conclusion, I want to say that it is worth paying attention to the FMF if you have:






All Articles