What libraries/frameworks, if any, exist to bind/"scaffold" SQL relational databases to a RESTful API in a declarative manner?
In short, by specifying the names of the tables and fields (alongside rules and flags), the library/framework would generate a RESTful API to access/modify the database.
I'd like powerful (but optional) control over the bindings, in order to specify custom logic easily if required.
I'm looking for something similar to this (C++ pseudocode):
context ctx;
// Bind to a MySQL database. Send/receive JSON.
api_binder<mysql, json> ab(ctx);
ab.bind_table("users", "api/users")
.bind_field<int>("id", flags::readonly | flags::primary_key)
.bind_field<string>("name")
.bind_field<unsigned int>("age")
.on_change([&ctx](auto old_value, auto new_value)
{
if(new_value > 0 && new_value < 150)
{
return new_value;
}
else
{
// Reply with an informative message.
ctx.send_msg("Invalid value for field `age`.");
return old_value;
}
});
The above pseudocode could work like this with the following requests:
// Request:
GET site.com/api/users
// Response:
[
{
"id": 0,
"name": "Name0",
"age": 30
},
{
"id": 1,
"name": "Name1",
"age": 40
}
]
// Request:
GET site.com/api/users/by/name?value=Name0
// Response:
[
{
"id": 0,
"name": "Name0",
"age": 30
}
]
// Request:
POST site.com/api/users
// Request body:
{
"name": "Name2",
"age": 900
}
// Response (error):
{
"msg": "Invalid value for field `age`."
}
// Request:
POST site.com/api/users
// Request body:
{
"name": "Name2",
"age": 90
}
// Response (OK):
{
"id": 2,
"name": "Name2",
"age": 90
}