Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Read named SQL statements from .sql files. Also named parameters for prepared statements.
Put your statements in a .sql file and name them with a comment above.
e.g. /myproject/sql/pokemon.sql
-- getPokemon
SELECT * from pokemon
WHERE id = ?; -- raw style
-- addPokemon
INSERT INTO pokemon(name, price)
VALUES ($name, $price); -- SQLite named parameter style
-- updatePokemon
UPDATE pokemon
SET price = :price; -- PostgreSQL / MySQL named parameter style
Use them in code by giving the directory where .sql files(s) are
const sql = require('yesql')('/myproject/sql/')
const db = new sqlite3.Database('/myproject/sql/db.sqlite3')
db.all(sql.getPokemon, 1337, (err, rows) => {...})
db
.prepare(sql.addPokemon)
.run({name: 'pikachu', price: 99}, err => {...}
Prepared statements for MySQL / MariaDB are supported
const sql = require('yesql')('/myproject/sql/', {type: 'mysql'})
const named = require('yesql').mysql
const mysql = require('mysql').createConnection...
// read from file
mysql.query(sql.updatePokemon({price: 5}), (err, result) => {...})
// use only named parameters
mysql.query(named('UPDATE ::ptable SET price = :price;')({price: 5, ptable: 'pokemon'}), (err, result) => {...})
Prepared statements for node-postgres (pg) are supported
const sql = require('yesql')('/myproject/sql/', {type: 'pg'})
const named = require('yesql').pg
const pg = require('pg').connect...
// read from file
pg.query(sql.updatePokemon({price: 5}), (err, result) => {...})
// use only named parameters
pg.query(named('UPDATE pokemon SET price = :price;')({price: 5}), (err, result) => {...})
By default MySQL and PG versions throw an error if a parameter is not given. Passing a flag "useNullForMissing" a null value is used instead. Example only for PG, but works for MySQL also.
const sql = require('yesql')('/myproject/sql/', {type: 'pg', useNullForMissing: true})
const named = require('yesql').pg
const pg = require('pg').connect...
// read from file and insert null values for missing parameters (price)
pg.query(sql.updatePokemon(), (err, result) => {...})
// use only named parameters with nulls for missing values
pg.query(named('UPDATE pokemon SET price = :price;', {useNullForMissing: true})({}), (err, result) => {...})
FAQs
Read named SQL statements from .sql files. Also named parameters for prepared statements.
We found that yesql 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.