Sequelice-Query
Sequelice-Query is a helpers that simplify querying Sequelize from Query URL
Installation
$ npm install --save sequelice-query
Query String
Sequelice-Query read key filtered
and sorted
from URL Query for example:
http://example.com?filtered=[{"id":"firstName", "value":"john"}]&sorted=[{"id":"id", "desc":true}]
both of them is JSON Stringify Array and it's contain object:
Key | Value | Object Key |
---|---|---|
filtered |
Array Object | id, value |
sorted |
Array Object | id, desc |
filtered=[{
"id": "firstName", // for column name
"value": "john" // for value
}]
sorted=[{
"id": "id", // for column name
"desc": true // boolean value true/false
}]
Quick Usage
const models = const sQuery = const Departement = modelsDepartement { const condition = await sQuery const include queryFilter: where querySort: order = condition const data = await Departement return resstatus200}
API
.generate({req, model, configs})
.generate({
req: object req express,
model: Model,
configs: object
})
const sQuery = const models = const User = modelsUserconst Role = modelsRole { const condition = await sQuery }
configs
These are the available config options
key | value | description |
---|---|---|
include | Array Object/Model | use as usual in the sequelize |
optFilter | Object | optFilter options |
optSort | Object | optSort options |
lets say we have table and data like this for example:
Departement
id | name | createdAt | updatedAt |
---|---|---|---|
1 | WiGen | 2019-08-24 20:09:04 | 2019-08-24 20:09:04 |
2 | Niku | 2019-08-24 20:14:57 | 2019-08-24 20:14:57 |
3 | Kuma | 2019-08-24 20:09:37 | 2019-08-24 20:09:40 |
optFilter options and example
key | value |
---|---|
initValues | object |
defaultValues | object |
isSkipKey | function |
customIncludeOptions | object |
transformValue | Function, Array Function |
transformValueByKey | object |
transformKey | Function, Array Function |
transformKeyByKey | object |
#initValues: object
GET URL: /departement
const condition = await sQuery const include queryFilter: where querySort: order = condition const data = await Departement return data /*resultdata: [ { "id": 1, "name": "WiGen", "createdAt": "2019-08-24T20:09:04.000Z", "updatedAt": "2019-08-24T20:09:04.000Z" }]*/
so basically initValues is just initialization your condition if there's no filter condition request by user
#defaultValues: object
GET URL: /departement
const condition = await sQuery const include queryFilter: where querySort: order = condition const data = await Departement return data /*result"data": [ { "id": 2, "name": "Niku", "createdAt": "2019-08-24T20:14:57.000Z", "updatedAt": "2019-08-24T20:14:57.000Z" }, { "id": 3, "name": "Kuma", "createdAt": "2019-08-24T20:09:37.000Z", "updatedAt": "2019-08-24T20:09:40.000Z" }]*/
if no condition for name
then the default values is LIKE %u%
#isSkipKey: function(args)
const condition = await sQuery
#customIncludeOptions: function(args)
const condition = await sQuery
#transformValue: array function (args, cont) | function (args, cont)
args = key: 'Departement.id' value: 1 3 configs: ... //configs optFilter include: //include model model: Departement // Model Object newKey: 'between$id' //generated from transformKeyByKey type: INTEGER //type column STRING/INTEGER/DATE .etc /* return cont value if you want to continue to next function transformValue or handle by default, if it's return undefined then it will not handle anything nor to next function transformValue*/cont
GET URL: /departement?filtered=[{"id":"between$id", "value":[2, 3]}]
const condition = await sQuery const include queryFilter: where querySort: order = condition const data = await Departement return data /*result"data": [ { "id": 2, "name": "Niku", "createdAt": "2019-08-24T20:14:57.000Z", "updatedAt": "2019-08-24T20:14:57.000Z" }, { "id": 3, "name": "Kuma", "createdAt": "2019-08-24T20:09:37.000Z", "updatedAt": "2019-08-24T20:09:40.000Z" }]*/
value: function (args, cont)
#transformValueByKey: object, GET URL: /departement?filtered=[{"id":"between$id", "value":[2, 3]}]
const condition = await sQuery const include queryFilter: where querySort: order = condition const data = await Departement return data /*result"data": [ { "id": 2, "name": "Niku", "createdAt": "2019-08-24T20:14:57.000Z", "updatedAt": "2019-08-24T20:14:57.000Z" }, { "id": 3, "name": "Kuma", "createdAt": "2019-08-24T20:09:37.000Z", "updatedAt": "2019-08-24T20:09:40.000Z" }]*/
it's same like transformValue but with spesific key
#transformKey: function (args, cont) | array function (args, cont)
const condition = await sQuery const include queryFilter: where querySort: order = condition const data = await Departement return data
value: function (args) | string
#transformKeyByKey: object, const condition = await sQuery
//or simply just passing stringtransformKeyByKey: 'between$id': 'id'
optSort options and example
key | value |
---|---|
initValues | object |
defaultValues | object |
transformValue | Function, Array Function |
transformValueByKey | object |
transformDesc | Function, Array Function |