avoid '%' in url
support two formats: json and pipe
eq (equal), ne (not equal), gt (great than), gte (great than equal), lt (less than), lte (less than equal), ct (contains), stw (startsWith), edw (endsWith), in, nin (not in), bt (between)
import queryOperator from 'query-operator'
// /api/resources?f1=xxx&f2=contains|substring&f3=ne|xxx&f4=gt
const queries = queryOperator.parse('f1=xxx&f2=contains|substring&f3=ne|xxx&f4=gt')
const queries = {
f1: {
operator: 'eq',
operants: ['xxx']
},
f2: {
operator: 'contains',
operants: ['substring']
},
f3: {
operator: 'ne',
operants: ['xxx']
},
f4: {
operator: 'gt',
}
}
queryOperator.defineOperator('bt', operantsString => {
return operantsString.split(queryOperator.OPERANTS_SPLITTER).filter(value => value !== 'null')
})
import { sqlRunner, sequelizeRunner, mongoRunner, jsRunner } from 'query-operator'
sqlRunner.run(queries) // where clause
jsRunner.run(data, queries) // return true or false
const myRunner = queryOperator.defineRunner({
eq: (field, ...operants) => `${field} = ${operants[0]}`,
ne: (field, ...operants) => `${field} != ${operants[0]}`,
in: (field, ...operants) => `${field} in (${operants.join(',')})`
//...
})
const queries = {
f1: {
operator: 'eq',
operants: ['xxx']
},
f2: {
operator: 'contains',
operants: ['substring']
},
f3: {
operator: 'ne',
operants: ['xxx']
},
f4: {
operator: 'gt',
}
}
queryOperator.stringify(queries) // f1=xxx&f2=contains|substring&f3=ne|xxx&f4=gt|