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-syntax

v2.0.1

Published

Tagged template literals utilities for ease of composing SQL queries.

Downloads

339

Readme

SQLSyntax

npm GitHub Workflow Status

Tagged template literals utilities for ease of composing SQL queries. This is a javascript port of ScalikeJBDC's SQLSyntax class.

Please note: from v2.0.0, this package has been released as pure ESM module. To move your CommonJS project to ESM, @sindresorhus's post is worth to read.

API document

https://t83714.github.io/SQLSyntax/

The SQLSyntax class also comes with many useful SQL query composing helper functions. e.g. joinWithAnd, where etc. For more information, please refer to the API document.

Example Usgae

import SQLSyntax, {sqls} from "sql-syntax";
import { Client } = "pg";

const client = new Client();
await client.connect();

const userId = "my-id";
const number = 4;

const query:SQLSyntax = sqls`SELECT * FROM users WHERE user_id = ${userId} AND number = ${number}`;

// generate SQL & binding parameters array for querying database
const [sql, parameters] = query.toQuery();
const res = await client.query(sql, parameters);
//or 
const res = await client.query(...query.toQuery());

// if the interpolated value is an instance of SQLSyntax, it will be merge into the SQL query
const condition1:SQLSyntax = sqls`user_id = ${userId}`;
const condition2:SQLSyntax = sqls`number = ${number}`;

const query2:SQLSyntax = sqls`SELECT * FROM users WHERE ${condition1} AND ${condition2}`;

// this will create SQL: 
// SELECT * FROM users WHERE user_id = $1 AND number = $2

Replace Built-in toQuery Method Logic

The default toQuery of SQLSyntax object will generate SQL targeting postgreSQL. If it doesn't work for you, you can replace the logic with your own logic:

import SQLSyntax from "sql-syntax";
SQLSyntax.customToQueryFunc = (s:SQLSyntax) => {
    //you own implementation...
}

Specify Column or Table Names in Query with Variables

For most cases, you should not need to specify a SQL indentifier (e.g. column name) at runtime (e.g. from a string variable) in a SQL Query. You should think about whether it's possible (for most cases, it should be possible) to avoid using createUnsafely method to specify a SQL indentifier using runtime varilable.

By default, SQLSyntax's sqls function will treat any string interpolation values as query parameters for safety concerns. Thus, you won't be able to insert a SQL indentifier (e.g. column or table names) at runtime (e.g. from a string variable) to your SQL query. e.g.:

import SQLSyntax, {sqls} from "sql-syntax";
const myColumnName: string = "field1";
const query = sqls`SELECT ${myColumnName} FROM users`;
const [sql, parameters] = query.toQuery();
// sql: SELECT $1 FROM users
// parameters: ["field1"]

To make it possible, SQLSyntax offers a createUnsafely method that allow you to create an instance of SQLSyntax class from any plain string. For the same example above, we can use createUnsafely method to create the desired query:

import SQLSyntax, {sqls} from "sql-syntax";
const myColumnName:SQLSyntax = SQLSyntax.createUnsafely("field1");
const query = sqls`SELECT ${myColumnName} FROM users`;
const [sql, parameters] = query.toQuery();
// sql: SELECT field1 FROM users
// parameters: []

As the content of the string variable will become part of SQL query via the createUnsafely method. You need to valiate / process the input string properly by yourself to avoid SQL injection vulnerabilities when use the createUnsafely method.

Alternatively, you can also use the included escapeIdentifier function (for PostgreSQL only) to escape an indentifier. This function will return a SQLSyntax instance as well but will try to filter / escape any invalid characters to make sure the resulted indentifier string is always safe to be included in a SQL query.