DreamFactory Intermediate Service

DreamFactory is a BaaS (Backend as a Service) that connects a multitude of data sources to APIs that apps can connect to. With our Agile Anti-Pattern App (MAPA), we wanted to move the patterns database out of the app, so that we did not have to publish a new version of the app every time we added or changed patterns. The simplest way for this was to put the complete data set (a tiered json file) on a webserver and tell the app to download from there. This would solve the problem, but be a pain to edit or version. The optimal solution would of course be a somewhat normalized database of patterns, their symptoms, their remedies, categories and a pattern-categories relation.

DreamFactory to the rescue

DreamFactory offers exactly that. Multiple databases or other sources are supported. For our relatively easy case, sqlite is sufficient. For each part of the database, API calls are automatically generated for data definition, manipulation and retrieval. Unfortunately, DreamFactory is thorough but rigid on which API calls are generated and what can be added. Since we normalized the data, all parts could be separately queried, but there was no way to get everything in one request.

A mobile app on the other hand, should be rather limited in how many network calls are made, and how much the app needs to know about the way data is stored in the database. To provide some glue, DreamFactory offers creation of services that do not run on top of any data source. As an example, the documentation implements a math service that offers a REST api for addition. Fortunately, DreamFactory can easily query it’s own APIs, so you can generate an intermediate service that aggregates API calls to yield compound data objects.

Fitting our needs

In the end we want a data structure that looks like this:

First of all, we need to retrieve data from all database tables in question:

Help wanted

Unfortunately DreamFactory apparently thinks joins need some kind of special permission on the database scheme, even though the role for this app has rights to antipattern.*, so we need to resolve the joins manually in PHP. If anyone knows, what is needed to be able to request

$url = 'antipattern/_table/antipattern?related=Remedies%2CSymptoms';

please let us know, we even tried to explicitly allow script access for any verb to antipattern/_table_/remedy, even though antipattern/* was already allowed for any verb for the role in question.

Back to topic

The mapaRequest object is a value object that will be used to yield the correct JSON result later. It can be instantiated, but has no logic of it’s own.

The mapaGenerator class renames some properties and joins symptoms and remedies to their respective patterns and supplements categories with their relations to patterns (see the following example). The part that says ['content']['resource'] just picks the relevant part out of the API response from the database service. After restructuring the data, some ids are offset for backwards compatibility, because the database is 1-indexed while the original JSON is 0-indexed.

Finally, we just need to return the value object as a JSON response, and it yields the correct format for our app.


This way, the MAPA app (german article) can hold the data as before, but query newer versions without needing an update. As an added benefit, we could add another app (probably a web app) that allows people to add and alter patterns in a structured way instead of by database access.

Für neue Blogupdates anmelden:

Ein Gedanke zu “DreamFactory Intermediate Service

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.