npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mysql-qs-parser

v1.2.3

Published

Middleware to transform query strings in a format that's recognized by MYSQL database.

Downloads

38

Readme

mysql-qs-parser

Middleware to transform query strings in a format that is recognized by the MySQL.

Prerequisites

It is recommended that your application use express.js or hapi.js frameworks (or frameworks that use them) for the middleware to work correctly. Tests with other frameworks were not performed.

Installing

Use the follow command:

npm i --save mysql-qs-parser

Usage Examples

Using default configuration

const express = require('express');
const qs = require('mysql-qs-parser');

const app = express();

app.use(qs());

app.get('/', (req, res) => {
    res.send({query: req.query, sql: qs.buildSql('users', req.query)});
});

app.listen(8080, () => console.log('app listening on port 8080'));

/**
 * Request: GET http://localhost:8080
 * Response:
 {
    "query": {
        "pagination": "LIMIT 100 OFFSET 0",
        "fields": "*",
        "sort": "",
        "filters": ""
    },
    "sql": "SELECT * FROM users LIMIT 100 OFFSET 0;"
}
 */

/**
 * Request: GET http://localhost:8080/?limit=10&page=2&sort=-name&fields=name,age&name=*A
 * Response:
{
    "query": {
        "pagination": "LIMIT 10 OFFSET 10",
        "fields": "name,age",
        "sort": "name DESC",
        "filters": "name LIKE %A"
    },
    "sql": "SELECT name,age FROM users WHERE name LIKE %A LIMIT 10 OFFSET 10 ORDER BY name DESC;"
}
 */

Using custom configuration

const express = require('express');
const qs = require('mysql-qs-parser');

const app = express();

app.use(qs({
    sort: 'created_at ASC',
    pagination: 'LIMIT 10 OFFSET 0'
}));

app.get('/', (req, res) => {
    res.send({query: req.query, sql: qs.buildSql('users', req.query)});
});

app.listen(8080, () => console.log('app listening on port 8080'));

/**
 * Request: GET http://localhost:8080
 * Response:
{
    "query": {
        "pagination": "LIMIT 10 OFFSET 0",
        "fields": "*",
        "sort": "created_at ASC",
        "filters": ""
    },
    "sql": "SELECT * FROM users LIMIT 10 OFFSET 0 ORDER BY created_at ASC;"
}
 */

Available Functions

parser()

The library has a function that transform query strings into query objects, in the format defined by the library. You can also pass custom settings, to mount the query object, as well as in the library instance as middleware.

Note: To work as expected, it is necessary to inform the query with the question mark at the beginning, as if it were to inform a GET request. Example: ?name=*A

If you enter without the question mark (as name=*A), the function will not identify your query string, and will return a query object with default values (or custom values, if you enter them).

Example:

const qs = require('mysql-qs-parser');

console.log('Default options:', qs.parser('?sort=-name&fields=name,age&name=*A'))
console.log('Custom options:', qs.parser('?sort=-name&fields=name,age&name=*A', { pagination: 'LIMIT 10 OFFSET 0'}))

/*
    Default options: {
        original: '?sort=-name&fields=name,age&name=*A',
        pagination: 'LIMIT 100 OFFSET 0',
        fields: 'name,age',
        sort: 'name DESC',
        filters: 'name LIKE %A'
    }
    Custom options: {
        original: '?sort=-name&fields=name,age&name=*A',
        pagination: 'LIMIT 10 OFFSET 0',
        fields: 'name,age',
        sort: 'name DESC',
        filters: 'name LIKE %A'
    }
*/

buildSql()

The library has a function that transforms the query object into a query string that is understandable by MySQL. For that, you can inform the name of the table where you want to apply the query, and the query string in MySQL format will be built based on the object of consultation.

Example:

const qs = require('mysql-qs-parser');

console.log('Default options:', qs.buildSql('users', qs.parser('?sort=-name&fields=name,age&name=*A')))
console.log('Custom options:', qs.buildSql('users', qs.parser('?sort=-name&fields=name,age&name=*A', { pagination: 'LIMIT 10 OFFSET 0'})))

/*
    Default options: SELECT name,age FROM users WHERE name LIKE %A LIMIT 100 OFFSET 0 ORDER BY name DESC;
    Custom options: SELECT name,age FROM users WHERE name LIKE %A LIMIT 10 OFFSET 0 ORDER BY name DESC;
*/