5

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
}
  • Ruby on Rails fits this description fairly easy. When you create models (bindings in the code that connect SQL tables to classes), you can automatically create routes and endpoints for REST-ful requests that can return JSON. It is, however, in Ruby, not PHP, Java, or C++, so I'm not sure if it works for you. – Aeolingamenfel Jan 13 '16 at 19:25
1

Pay a look at http://www.bravo-kernel.com/2015/04/how-to-build-a-cakephp-3-rest-api-in-minutes/

I've used CakePHP in the past to make a fast & easy GUI to access the DB, but didn't tried the REST API part. Seems easy to use though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.