Coder Social home page Coder Social logo

sql-condition-builder's Introduction

sql-condition-builder

This module builds SQL conditions from plain javascript objects. Objects are parsed as AND and Arrays are parsed as OR. It's primary use case is to build url queries easily with parsed to SQL.

This gives the possibility to...

`{a:'Hello',b:'!World'} => "a = 'Hello' AND b != 'World'"` (?a=Hello&b=!World)

Installation

npm install sql-condition-builder

Basic usage

var SQLConditionBuilder = require('sql-condition-builder');
var builder = new SQLConditionBuilder();

var obj = {a:'Hello',b:'!World'}
var condition = builder.build(obj); // ->"a = 'Hello' AND b != 'World'"

Examples

  • {a:'[25 TO 250]'} => "a BETWEEN 25 AND 250" (?a=[25 TO 250])
  • {x[{a:'>25'},{b:'<100'}]} => "a > 25 OR b < 100" (?x[a]=>25&x[b]=<100)

NOTE: in last example x is ignored because it's pointing to array of values (or statement). Keys pointing to Object or Array are ignored from statement.

Supported values

  • {a: null} or {a: 'null'} –> a IS NULL
  • {a: '!null'} –> a IS NOT NULL
  • {a: 'value'} –> a = 'value'
  • {a: '!value'} –> a <> 'value'
  • {a: '>25'} –> a > 25
  • {a: '<25'} –> a < 25
  • {a: '>=25'} –> a >= 25
  • {a: '<=25'} –> a <= 25
  • {a: '<="25"'} –> a <= '25'
  • {a: '[2 TO 200]'} –> a BETWEEN 2 AND 200
  • {a: '[1, 2, 3]'} –> a IN (1 , 2 , 3)
  • {a: '[a, "b", 'c']'} –> a IN ('a', 'b', 'c')
  • {a: '![a, "b", 'c']'} –> a NOT IN ('a', 'b', 'c')
  • {a: 'He?lo wor*'} –> a LIKE 'He_lo wor%'

Note that values are evaulated to JavaScript number type if it's a valid number otherwise they are evaluated to a string type. However in the situation you wanto to coerce a number into a string wrap it around double quotes ".

Running tests

npm run test

Build dist library

npm run build

sql-condition-builder's People

Contributors

jakubknejzlik avatar expobrain avatar jrfferreira avatar

Stargazers

 avatar Yuki菌 avatar Reis avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

sql-condition-builder's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.