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

sql-named-parameters

v1.0.6

Published

This non-opinionated library transforms named parameters into traditional positional parameters.

Downloads

19

Readme

sql-named-parameters

This non-opinionated library transforms named parameters into traditional positional parameters.

  const result = transform({
    query:'select * from hello where id=$id', 
    params:{ id: "foo" }
  });
  console.log( result.transformedQuery ); //  'select * from hello where id=$1'
  console.log( result.positionalParams ); // [ 'foo' ]

For those who want to save your precious finger power,

  const result = transform({
    query:'select * from hello where id=$id',
    params:{ id: "foo" }
  });
  console.log( result.query  ); //  'select * from hello where id=$1'
  console.log( result.params ); // [ 'foo' ]

About Binding DEFAULT as a Parameter

Suppose there is a table as such :

CREATE TABLE foos (
  foo_id   uuid    DEFAULT gen_random_uuid() NOT NULL
 ,foo_name varchar DEFAULT 'Mr.FOO' NOT NULL
 ,PRIMARY KEY ( foo_id )
)

Then, you want to insert rows :

const conn  = db.connect();
const exec =({query,params})=>{
  const q = transform({query, params});
  conn.query(q.query, q.params);
};
const query=`
    INSERT INTO foos (  foo_name )
    VALUES           ( $foo_name )`;

exec({query, params:{foo_name:'foo'}});
exec({query, params:{foo_name:'bar'}});

Then, sometimes you want to set the default value which is defined as 'Mr.FOO', but unfortunately DEFAULT cannot be specified as a parameter value.

exec({query, params:{foo_name:'baz'}});
exec({query, params:{foo_name:DEFAULT}}); // bummer, you cannot do that.

That's where this library comes in.

  const result = transform({
    query:'INSERT INTO foos (foo_name) VALUES ($foo_name)', 
    params:{ foo_name : Symbol.for('DEFAULT') }
  });

  console.log( result.query  ); // 'INSERT INTO foos (foo_name) VALUES ( DEFAULT )'
  console.log( result.params ); // [ ]

Variable Names with Any Dollar Signs in Themself

While this library regards any word starts with a dollar sign as a variable name, it will exclude those in which there are any dollar signs in the middle/in the end of itself.

This feature is intended to pass PostgreSQL's dollar-quoted string constants.

  const result = transform({
    query:
    `
      CREATE OR REPLACE FUNCTION test_func() RETURNS uuid AS
      $SQL$
      BEGIN
        RETURN gen_random_uuid();
      END
      $SQL$ LANGUAGE plpgsql;
    `,
    params: []
  });
  console.error( result.transformedQuery );

>      CREATE OR REPLACE FUNCTION test_func() RETURNS uuid AS
>      $SQL$
>      BEGIN
>        RETURN gen_random_uuid();
>      END
>      $SQL$ LANGUAGE plpgsql;

About Escape Sequence $$

This function is deprecated

This function is deprecated; all newly started projects should not use this feature. As of Mar 2 2023, PostgreSQL's dollar-quoted strings are safely ignored. This feature is not necessary anymore.

Description

Double dollar $$ will be replaced with $

  const result = transform({
    query:
    `
      CREATE OR REPLACE FUNCTION test_func() RETURNS uuid AS
      $$SQL$$
      BEGIN
        RETURN gen_random_uuid();
      END
      $$SQL$$ LANGUAGE plpgsql;
    `,
    params: []
  });
  console.error( result.transformedQuery );

>      CREATE OR REPLACE FUNCTION test_func() RETURNS uuid AS
>      $SQL$
>      BEGIN
>        RETURN gen_random_uuid();
>      END
>      $SQL$ LANGUAGE plpgsql;

CAUTION

This library is a kind of SQL generator. Any libraries that generate SQL are inherently vulnerable for SQL injection. For me, it seems safe enough to use in production; but you cannot believe me especially if you are using this in a system at a nuclear plant, a serious banking system or something you think it is precious.

IF YOU DON'T KNOW WHAT YOU ARE DOING, THEN DON'T USE THIS LIBRARY.

You are warned.

Otherwise, this library may be going to work well and reduce size of your code.

History

  • 0.1.0 : The first version.
  • 0.1.1 : Supported DEFAULT.
  • 0.1.2 : Updated the document.
  • 0.1.3 : Updated the document.
  • 0.1.4 : Throw more informative error messages. (Mon, 07 Nov 2022 18:43:17 +0900)
  • 0.1.5 : Added escape sequence $$. (Fri, 09 Dec 2022 18:54:53 +0900)
  • 1.0.6 : All variable names contains any dollar signs should be ignored (Thu, 02 Mar 2023 13:58:55 +0900)

Conclusion

That's all. Thank you very much for your kind attention.