Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
sql-template-strings
Advanced tools
Readme
A simple yet powerful module to allow you to use ES6 tagged template strings for prepared/escaped statements in mysql / mysql2 and postgres (and with simple, I mean only 7 lines of code!).
Example for escaping queries (callbacks omitted):
let SQL = require('sql-template-strings');
let book = 'harry potter';
mysql.query('SELECT author FROM books WHERE name = ?', [book]);
// is equivalent to
mysql.query(SQL`SELECT author FROM books WHERE name = ${book}`);
pg.query('SELECT author FROM books WHERE name = $1', [book]);
// is equivalent to
pg.query(SQL`SELECT author FROM books WHERE name = ${book}`);
For mysql2 prepared statements, just replace query
with execute
.
This might not seem like a big deal, but when you do an INSERT with a lot columns writing all the placeholders becomes a nightmare:
db.query(
'INSERT INTO books (name, author, isbn, category, recommended_age, pages, price) VALUES (?, ?, ?, ?, ?, ?, ?)',
[name, author, isbn, category, recommendedAge, pages, price]
);
// is equivalent to
db.query(SQL`
INSERT
INTO books
(name, author, isbn, category, recommended_age, pages, price)
VALUES (${name}, ${author}, ${isbn}, ${category}, ${recommendedAge}, ${pages}, ${price})
`);
Also template strings support line breaks, while normal strings do not.
Please note that postgre requires prepared statements to be named, otherwise the parameters will be escaped and replaced on the client side. You can still use SQL template strings though, you just need to assign a name to the query before using it:
// old way
pg.query({name: 'my_query', text: 'SELECT author FROM books WHERE name = $1', values: [book]});
//with template strings
let query = SQL`SELECT author FROM books WHERE name = ${book}`;
query.name = 'my_query';
pg.query(query);
// or using lodash
pg.query(_.assign(SQL`SELECT author FROM books WHERE name = ${book}`, {name: 'my_query'}))
FAQs
ES6 tagged template strings for prepared statements with mysql and postgres
We found that sql-template-strings 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
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).