Giter VIP home page Giter VIP logo

sqlqs's Introduction

sqlqs - sql query strings

Build Status

Coverage Status

A very simple and rudimentary query string to SQL predicate parser.

Heavily influenced by the query string methods from the PostgREST API server, this module will create SQL WHERE clause predicates from the query string. With PostgREST the query string handles almost all of the database filtering. For instance, to filter the database the query string may look like the following, ?x=eq.10. Where x is the column to filter, eq is the operator to use, and 10 is the criteria to filter. I call these a predicate object. I found this query string structure very flexible and decided to try and recreate it with Node.

Use

const { parse, sqlize, where } = require('sqlqs')

let qs = {
  class: 'in.Mammal,Bird',
  genus: 'eq.Neotoma',
  id: 'gt.1000'
}

// parse query string into query object
const QueryObj = parse(qs)

// returns
[
  {
    column:"class",
    operator:"IN",
    criteria:["Mammal","Bird"]
  }, {
    column:"genus",
    operator:"=",
    criteria:"Neotoma"
  }, {
    column:"id",
    operator:">",
    criteria:"1000"
  }
]

// parse query object into a WHERE clause
sqlize(QueryObj)

// returns
"class IN ('Mammal','Bird') AND genus = 'Neotoma' AND id > 1000"

// where pipes these two methods together
where(qs)

// returns
"class IN ('Mammal','Bird') AND genus = 'Neotoma' AND id > 1000"

Try it on RunKit

Caveats

This will be helpful for writing an API using database tools like pg-promise, pg, etc. This will not be useful for an ORM.

A query string like this ?species=eq.arctos&species=eq.americanus is attempting to return all records where species is arctos or americanus. However it creats this query SELECT * FROM animals where species = 'arctos' AND species = 'americanus'. While this is a valid SQL string, it will not return any records. The query string should be ?species=in.arctos,americanus which will create this SQL query SELECT * FROM animals WHERE species IN ('arctos','americanus').

sqlqs assumes that numbers provided in the query string map to fields with number datatypes in the database. I can for see this being an issue if numbers with leading zeros are being stored in a database as a text datatype.

sqlqs's People

Watchers

James Cloos avatar

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.