SchemaBasedApi
I've always been a interested in effortless, no-fuss, conventions' based development, DRYness, and pragmatic programming, I've always thought that at this point of the technology evolution, we need not to configure too much to have our software run, and have the software adapt to data layers, from there build up automatically the APIs, visualizations etc. This is a first step to have a schema driven API, based on the data it has to serve, it also gives, thanks to meta programming, an insight on the actual schema, the translations available and the DSL which can change the way the data is presented, leading to a strong base for automatically build of UIs consuming the API (react, vue, angular based PWAs, maybe! ;-) ).
Doing this means also narrowing a bit the scope of the tools, taking decisions, at least for the first iteratons of the project, so, this works well if the data is relational, so this is a convention taken as a prerequisite (postgres, mysql, mssql, etc.).
Goal
To have a comprehensive and meaningful API right out of the box by just creating migrations in your rails app.
v2?
Yes, the v1 was were it all started, many ideas are ported from there, but it was too coupled with thecore's rails_admin UI, making it impossible to create an UI-less, API only application, out of the box and directly from the DB schema, with all the bells and whistles I needed (mainly self adapting, data and schema driven API functionalities).
Standards Used
- JWT for authentication.
- CanCanCan for authorization.
- Active Hash Relation for DSL.
- Ransack query engine for complex searches going beyond CRUD's listing scope.
- Catch all routing rule to add basic crud operations to any AR model in the app.
TODO
- Integrate Authorization within
GET info/schemarequests in order to send to the client just the models for which a user has authorization.
Usage
How to use my plugin.
Installation
Add this line to your application's Gemfile:
gem 'schema_based_api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install schema_based_api
Then run the migrations:
$ rails db:migrate
This will setup a User model, Role model and the HABTM table between the two.
Then, if you fire up your rails server you can already get a jwt and perform different operations.
The default admin user created during the migration step has a randomly generated password you can find in a .passwords file in the root of your project, that's the initial password, in production you can replace that one, but for testing it proved handy to have it promptly available.
If you want to manually test the API using Insomnia
References
THanks to all these people for ideas:
- Daniel For a smart way to manage token expiration.
License
The gem is available as open source under the terms of the MIT License.