
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
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.
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.
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
toQuery Method LogicThe 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...
}
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
createUnsafelymethod 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.
FAQs
Tagged template literals utilities for ease of composing SQL queries.
The npm package sql-syntax receives a total of 13,562 weekly downloads. As such, sql-syntax popularity was classified as popular.
We found that sql-syntax demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.