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

queryt

v0.3.0

Published

Query Template engine.

Downloads

10

Readme

queryT

A query text builder that depends upon the available query parameters. queryT.js is small, it has no dependencies and it is inspired by the simplicity and efficiency of the doT.

Tokens

queryT.tokens = {
  start: '[[',                          //evaluation start token
  end: ']]',                            //evaluation end token
  separators:[',', 'AND', 'OR']         //a list of separators
};

Parameters

The name of the parameters complies with the MsSql rules for variables. It must begin with an at (@) sign.

Evaluation

Evaluation consists in checking the availability of the parameters used. An evaluation is passed only if all the parameters are available, regardless sub-evaluations. For example:

SELECT * FROM Table
[[WHERE Field1 = @Field1 [[AND Field2 = @Field2]]]]

if only the @Field2 is passed then the result will be:

SELECT * FROM Table

if only the @Field1 is passed then the result will be:

SELECT * FROM Table
WHERE Field1 = @Field1

if both parameters are passed then the result will be:

SELECT * FROM Table
WHERE Field1 = @Field1 AND Field2 = @Field2

Separators

Separators helps composing a correct statement. In an evaluation, each sub-evaluation depends on previous sub-evaluations. If one of the previous sub-evaluations was significant then the separator should stay, otherwise it will be removed.

template

SELECT * FROM Table
[[WHERE [[Field1 = @Field1]] [[AND Field2 = @Field2]] [[AND Field3 = @Field3]]]]

Suppose the @Field1 parameter is not available, but @Field2 and @Field3 are. The result will be:

SELECT * FROM Table
WHERE Field2 = @Field2 AND Field3 = @Field3

Notice that the 'AND' separator was removed from [[AND Field2 = @Field2]], but not from [[AND Field3 = @Field3]].

Usage

var parameters = ['@Param1', '@Param3'],
    template = 'SELECT * FROM Table [[WHERE [[Field1 = @Param1]] [[AND Field2 = @Param2]] [[AND Field3 = @Param3]]]]',
    options = {
        hasParameter : function(name){
            return parameters.indexOf(name) !== -1;
        },
        rewriteParameter : function(name, index){
            return name;
        }
    };
var result = queryT.template(template, options);

Result

SELECT * FROM Table WHERE Field1 = @Param1 AND Field3 = @Param3

Parameters rewriting

Not all sql server have the same notation rule for parameters. For example, in MySql the notation for parameters is '?'. In this case, the rewriteParameter function should be provided.

var parameters = ['@Param1', '@Param3'],
    template = 'SELECT * FROM Table [[WHERE [[Field1 = @Param1]] [[AND Field2 = @Param2]] [[AND Field3 = @Param3]]]]',
    options = {
        hasParameter : function(name){
            return parameters.indexOf(name) !== -1;
        },
        rewriteParameter : function(name, index){
            return '?';
        }
    };
var result = queryT.template(template, options);

Result

SELECT * FROM Table WHERE Field1 = ? AND Field3 = ?

Joins

var parameters = ['@Param1', '@Param3'],
    template = 'SELECT * FROM Table [[LEFT JOIN Table2 ON Table.Field1 = Table2.Field1 AND Table2.Field2 = @Param2]] [[WHERE [[Table1.Field1 = @Param1]] [[AND Table1.Field2 = @Param3]]]]',
    options = {
        hasParameter : function(name){
            return parameters.indexOf(name) !== -1;
        },
        rewriteParameter : function(name, index){
            return name;
        }
    };
var result = queryT.template(template, options);

Result

SELECT * FROM Table WHERE Table1.Field1 = @Param1 AND Table1.Field2 = @Param3

Fields on select clause depending on parameters

For fields, an workaround is needed.

var parameters = ['@Param1', '@Param3'],
    template = 'SELECT Table.*[[, Table2.Field2 @Param2Field]] FROM Table [[LEFT JOIN Table2 ON Table.Field1 = Table2.Field1 AND Table2.Field2 = @Param2]] [[WHERE [[Table1.Field1 = @Param1]] [[AND Table1.Field2 = @Param3]]]]',
    options = {
        hasParameter : function(name){
            return (parameters.indexOf(name) >= 0) || (parameters.indexOf(name + 'Field') >= 0);
        },
        rewriteParameter : function(name, index){
            if (name.substr(-5) === 'Field')
                return '';
            return name;
        }
    };
var result = queryT.template(template, options);

Result

SELECT Table.* FROM Table WHERE Table1.Field1 = @Param1 AND Table1.Field2 = @Param3

Author

Mihai Slobozeanu

License

queryT is licensed under the MIT License.