Hapi-sequelize-dynamic-fields
Sometimes we return values at a given endpoint that not always we need all the information, with the plugin you can go through the header which fields you want to return.
For example, we have a model of Tasks (sequelize) that has a relationship for the user. In this way, we could structurer the following query sequelize:
const options = attributes: 'id' 'descriptions' 'observation' include: model: requestdatabaseUser attributes: 'id' 'username' 'lastname' 'email' ;
In this case, all the fields entered in the attributes properties will be returned, including to the related table. But, will all this information always be used?
We can do in the following manner:
model;
The plugin provide a call , request.fieldsAll
, in which checks if there a property in the header called fields
, if there is, the query will be mounted according to the fields informed, in case the field there isn’t, an exception will be thrown AttributesInvalidError
.
In the return of our request we can inform which fields are permitted, by the function request.fieldsHeaders
, for example:
return ;
Response Headers:
"allowing-fields": "id,descriptions,observation,User.id,User.username,User.firstName,User.lastName,User.email",
Example:
Configuration Hapijs and K7
===
const Hapi = ;const path = ; let server = ; const dir = path; const register = register: options: models: dir adapter: connectionOptions: options: dialect: 'sqlite' register: ; server; server;
Create models
===
const User = sequelize; const Tasks = sequelize;
Create routes
===
... server;...
Create controllers
===
const list = async { try const model = requestdatabaseUser; const options = attributes: 'id' 'username' 'lastname' 'email' ; const values = await model; return ; catch err return reply; }; const listTasks = async { try const model = requestdatabaseTasks; const options = attributes: 'id' 'descriptions' 'observation' include: model: requestdatabaseUser attributes: 'id' 'username' 'lastname' 'email' ; const values = await model; return ; catch err return reply; };
Example Users
===
request
curl -X GET --header 'Accept: application/json' --header 'fields: id' 'http://localhost:3000/user'
SQL
SELECT 'id' FROM 'users' AS 'User';
request
===
curl -X GET --header 'Accept: application/json' --header 'fields: id, email' 'http://localhost:3000/user'
SQL
SELECT 'id' 'email' FROM 'users' AS 'User';
Response Headers
{ "allowing-fields": "username,firstName,lastName,email", ... }
Example Tasks
===
curl -X GET --header 'Accept: application/json' 'http://localhost:3000/tasks'
SQL
SELECT 'Tasks''id' 'Tasks''descriptions' 'Tasks''observation' 'User''id' AS 'User.id' 'User''username' AS 'User.username' 'User''first_name' AS 'User.firstName' 'User''last_name' AS 'User.lastName' 'User''email' AS 'User.email' FROM 'tasks' AS 'Tasks' LEFT OUTER JOIN 'users' AS 'User' ON 'Tasks''user_id' = 'User''id';
===
curl -X GET --header 'Accept: application/json' --header 'fields: id, User.id, User.username' 'http://localhost:3000/tasks'
SQL
SELECT 'Tasks''id' 'User''id' AS 'User.id' 'User''username' AS 'User.username' FROM 'tasks' AS 'Tasks' LEFT OUTER JOIN 'users' AS 'User' ON 'Tasks''user_id' = 'User''id';
Response Headers
{ "allowing-fields": "id,descriptions,observation,User.id,User.username,User.firstName,User.lastName,User.email", ... }