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

qo-sql

v0.0.5

Published

qo-sql (Query Object with Sql) can be a babel plugin to query object in template literals with sql syntax or as lib

Downloads

2

Readme

qo-sql

Build Status codecov Code Climate NPM version

qo-sql(Query Object with Sql)

a simple way to query object/array with sql, it's will compile to lodash/underscore you can use it as babel-plugin or lib. different with other javascript sql lib is, as babel-plugin will compile to target code, qo-sql most like a compiler.

parse sql-syntax at run-time not best choice,better choice it's work at compile time; babel is compiler, and not only es6 transformer.

In

// ..
var res =  `sql:select * from ${testData} where id=${id}`;

Out

 // rumtime
 //.. lodash mode
var res = function (params) {
        var source = params[0];var res = _.chain(source).filter(function (item) {
            return item['id'] == params[1];
        }).map(function (item) {
            return _extends({}, item);
        }).value();return res;
    }([testData, id])

Install

npm install qo-sql
npm install babel-plugin-syntax-object-rest-spread
npm install babel-plugin-transform-object-rest-spread

Usage

Lib Mode

es5

var _ = require('lodash');
var qos = require('qo-sql/lib');
var testData = require('../test/gen/test_data.js').arrayData;


var res = qos.exec("select (id + 1) as index, name  from ${testData} where id > ${minid} and type = 'C'", {
    testData : testData,
    minid : 2
})

es6

var _ = require('lodash');
var qos = require('qo-sql/lib/tagged');
var testData = require('../test/gen/test_data.js').arrayData;
var id=2;

var res = qos`select * from ${testData} where id=${id}`;

Babel-plugin Mode (recommend)

parse in compile time

babel-plugin mode support query object in template-literals

such as

var testData = require('../test/gen/test_data.js').arrayData;;
var minid = 2;

var result = `sql:select (id + 1) as index, name  from ${testData} where id > ${minid} and type = 'C'`;

Via .babelrc

{
    "plugins": [
        ["qo-sql", {
            prefix:'sql:',
            mode:'lodash'
        }
    ]
}

Via Node API

var babel = require('babel-core');
var _ = require('lodash');

var options = {
    presets: [
        require('babel-preset-es2015')
    ],
    plugins: [
        require('babel-plugin-syntax-object-rest-spread'),
        require('babel-plugin-transform-object-rest-spread'),
        [require('qo-sql'), { prefix: 'sql:', mode :'lodash' }]
    ],
    babelrc : false
};

var result = babel.transform('code', options);

Options

  • prefix:(string) default = 'sql:'
  • mode:(lodash|underscore) default = 'underscore'

Example

  • SELECT
    `sql:select id as ID, (id + 1) as ID2, (id / 3) as ID3, (city + '@' + country)  as address, testCase.formatter.formatMoney(count) from ${testData} where type=${type}`;
  • WHERE
    `sql:select * from ${testData} where id=${id}`;
    `sql:select * from ${testData} where id!=${excludeId} and id > ${minId}`;
    `sql:select * from ${testData} where id=${id} and name=${name}`;
    `sql:select * from ${testData} where id=${id} or count>${mincount}`;
    `sql:select * from ${testData} where id>=${minid} and id<=${maxid}`;
    `sql:select * from ${testData} where country IN ${ctyList} AND type NOT IN ${typeList}`;
  • ORDER、LIMIT
    `sql:select * from ${testData} where id>=${minid} and id<=${maxid} order by type, count desc limit ${start}, ${count}`;
  • GROUP and Aggregation

support min, max, count, sum, avg

    `sql:select type, country, min(count) as min, max(count) as max, count(*) as total, sum(count) as sum from ${testData} WHERE id >${minId} group by type, country order by country, type`;
  • Custom function
   `sql:select *, testCase.formatter.formatMoney(count) as money from ${testData} where type=${type}`;

More example

Examples

after compiling

Compile to lodash Compile to underscore