Skip to content

nsc-open/query-operator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

avoid '%' in url

support two formats: json and pipe

operators

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',
  }

}

handles null

extend by yourself

queryOperator.defineOperator('bt', operantsString => {
  return operantsString.split(queryOperator.OPERANTS_SPLITTER).filter(value => value !== 'null')
})

run it

import { sqlRunner, sequelizeRunner, mongoRunner, jsRunner } from 'query-operator'
sqlRunner.run(queries) // where clause
jsRunner.run(data, queries) // return true or false

define your runner

const myRunner = queryOperator.defineRunner({
  eq: (field, ...operants) => `${field} = ${operants[0]}`,
  ne: (field, ...operants) => `${field} != ${operants[0]}`,
  in: (field, ...operants) => `${field} in (${operants.join(',')})`
  //...
})

stringify

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|

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published