The context
I have an IoT project where the sensors are sending data to my Postgres database. The sensors are manages by a stand-alone service which provides a REST API to query various information about the sensors (e.g. are they online).
I would like to have a unified interface for querying all the related data regardless of which actual application stores the data.
The idea
I would like to use Postgres as a gateway which could query data from the external services and provide a unified interface.
Something along the lines of:
SELECT sensor_id, client_email
FROM sensors
WHERE is_online=false;
That would be a view integrating together information from the database (sensor_id
, client_email
) and from the external service (is_online
). In the future the number of external services holding some parts of the information might increase.
The technical implementation I am considering would use functions written in the Python procedural language which could query the REST API.
I am treating the database as a first-class citizen: it defines a clear public API for the other applications just as any other service would do.
Alternation solutions
An alternative would be to create separate service which would provide some sort of API – e.g. GraphQL – based on the data from the Postgres database and the other services.
Advantages of the Postgres based solution
In my opinion the Postgres based solution would have the following advantages:
- SQL queries are more flexible than GraphQL queries, especially for
GROUP BY
andHAVING
style usecases. - SQL (and Postgres specifically) has better support for managing users and their access rights.
- Most other applications are interacting with the database directly. It would be nice if they could continue using the database for all the data they need without needing to know where it is actually coming from.
- Managing another application and keeping it up to date with the database is a lot of extra work.
What would be the advantages and disadvantages of that approach? How would you solve that type of a problem?