Coder Social home page Coder Social logo

killercup / sortstringtosql Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 5 KB

Node Module. Convert Sort Expression to SQL that can be used in 'ORDER BY' statement, e.g. '-aired,id' -> 'aird DESC NULLS LAST, id ASC NULLS LAST'.

Home Page: https://www.npmjs.org/package/sort-string-to-sql

License: MIT License

JavaScript 100.00%

sortstringtosql's Introduction

Sort String To SQL

This is a JavaScript/node.js module to convert 'sort expressions' to SQL expressions that can be used in an 'ORDER BY'. E.g., -date,id will be converted date DESC NULLS LAST, id ASC NULLS LAST.

It can be used to parse a query parameter for sorting and to easily convert it to an expression that can be used in an SQL query.

I'm only using this with Postgres myself, so I can't promise you it will work with other databases.

Build Status

Example

var sortStringToSql = require('sort-string-to-sql');
// In an express server, this might be `req.param('sort')`
var sortParam = '-date,id';
sortStringToSql(sortParam);
// => "date DESC NULLS LAST, id ASC NULLS LAST"

Input Format

Comma separated list of field names. Prepend a '-' for descending order or a '+' dor ascending order (which is the default, so it's optional). Append a '-' if you want records with null values first (this sets NULLS FIRST).

You can find some examples in test.js.

License

MIT

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.