Connect Mendix with REST-DB for Backup

< Part 1

This is the second part of the journey on how to build a backup database in REST-DB and use it with Mendix.

Build the Backup Database in REST-DB

If you do not already have an account (it’s free) in REST-DB go here for a quick start with link to sign up.

Follow the steps in the quick start to create one collection backup. Create two fields: date (Type datetime) and changes (Type string):

Image for post
Image for post

Define the represenation of the backup database in Mendix with an entity with attributes date and changes (unlimited String):

Image for post
Image for post

As we want to retrieve the data not from the database but from the API of our REST DB via microflow the entity should not be persistent, otherwise we risk to have inconsistencies. Therefore we also do not need a newEdit Page. So create a blank page named Backup_Overview within the folder OverviewPages. Duplicate the layout of the other overview pages and fill the left part of the LayoutGrid with the Entity_Menu Snippet and the main part with a DataGrid with datasource a newly created Microflow RTV_BackupFromRest. The EntityMenu snippet was changed after the creation.

Image for post
Image for post

Before implementing the workflow, create Message-Definitions for Backup. Select both attributes.

Image for post
Image for post

Now the funny part starts, thanks Mendix for these features. Press Generate Mappings and select both import and export. The Mappings are used in the microflow.

Open the RTV_BackupFromRest Microflow and remove all Parameters if there where any. Use the action Call REST Service and set the location to the restdb rest URL.

Image for post
Image for post

The next thing to do is to create an apikey for the acces to the collection in restdb. Go to settings of the database (not collection settings!):

Image for post
Image for post

In Tab API under WebPage API press AddNew and fill the upcoming form with the collection path and the two allowed methods GET and POST

Image for post
Image for post

For productive data you may limit the access to your site url. But you won’t be able to test locally if you limit them now.

This api key is then added to HTTP Headers of the CallRest Action:

Image for post
Image for post

Go to the response tab, select apply import mapping, select our backup import mapping and store in variable. OK.

Image for post
Image for post

Right-click on the Action and select the Set BackupList as return value. Save.

Image for post
Image for post

With these few steps we have setup all we need to retrieve the data from the restDB.

Putting things in

To test the grid we need data in the DB. So we add a button NewBackup to the Grid ControlBar. The Button calls the microflow EXE_Backup:

Image for post
Image for post

As we are still in development phase we just need two strings as data to test the interface. We will implement the real backup part later.

After we have created the Backup with a date and a string we call the REST (Post) with Export mapping for the entire request.

Image for post
Image for post

So first Create the Backup (non-persistent) and fill it with a date and a string representing Date and Changes. Then use this NewBackup in the Backup export mapping. Add a new action Call Rest and select the Post method, as every backup has to be a new entry. Location and x-apikey are the same as above. But you need to specify the content, otherwise the JSON will not be accepted by the server. So add a Content-Type header with application/json as value.

Image for post
Image for post

Thats it. Now we can create new entries in our rest DB and list them on our BackupOverview page.

So for the last part we need to really backup our Person and Address data.

Part 3 >

Full Stack Developer. Building digital things from the early 90s until now. Currently focused on low code development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store