sails-hook-openapi v1.1.7
Install:
npm install --save sails-hook-openapi
This module adds a route in a sails project, which allows to make a FIND query on any model of the sails backend, with filters:
GET {{url}}/api/v1/request/:model
or
POST {{url}}/api/v1/request/:model
Configuration
In
./config/openapi.js
file :
module.exports.openapi = {
//POLICIES
//By default:
policies: {
production: false,
staging: false,
development: true
},
/* Works the same as sails policies
policies: false,
policies: true,
policies: {
production: "tokenAuth",
staging: "tokenAuth",
development: true,
}
policies: ["tokenAuth", "otherPolicy"],
*/
///////////////////////////////////////////////////////////////////////
//ROUTES
//By default:
//routes: ["get /api/v1/request/:model", "post /api/v1/request/:model"]
/*
Routes can be overwritten (it must contain :model in the route name or it can be included as a parameter)
routes: "get /api/v1/openapi/:model"
routes: ["get /:model", "post /:model"]
*/
};
In
./config/env/[development|staging|production].js
file :
module.exports = {
openapi: {
policies: [...],
routes: [...]
}
}
Parameters for all queries
- model : name of the model to query on (case insensisitve) required
- limit : defaults to 10, min 1, max 50
- offset : defaults to 0
- sort: defaults to "createdAt desc"
Parameters specific to a Model
GET query
You can filter on any string, number and boolean fields of the model (except for previous parameters: model, limit, offset and sort).
For strings, the value can contain '%' to make a "LIKE" query on the field
Numbers sent in parameters are casted as number, so the query on a number saved as a string won't work. To avoid that, add a "%" before or after the number.
Example:
- first_name : Fred%
- last_name : %iso%
- activated : true
POST query
For native query, sort option must be an object | 1 = asc / -1 = desc | {"email": 1}
Examples:
{
"first_name": { "like": "Fred%" },
"activated" : true,
"limit": 5,
"sort": "createdAt"
}
{
"native": true,
"query": { "email": {"$regex":".*name.*"} },
"limit": 8,
"sort": { "email" : 1 }
}
{
"native": "aggregate",
"query": [
{
"$group": {
"_id": "$first_name"
}
}
]
}
Response:
It always return an array, even with limit=1
{
"success": true,
"result": []
}
Coming soon:
- populate parameter to chose which fields to populate in the result