Registry / database / yesql
library7.0.0jsnpmunverified

A Node.js library for reading named SQL statements from .sql files and using named parameters in prepared statements. Version 7.0.0 supports PostgreSQL, MySQL/MariaDB, and raw/SQLite drivers. It parses SQL files with named queries and converts named parameters (e.g. :name, $name) into database-specific parameterized formats. Key differentiator: it handles multiple database dialects, supports quoted strings, missing parameter handling, and throws on duplicate query names. Released on npm, maintained, with active development history.

npm install yesql
INSTALL
IMPORT
SIG · YESQL
Y
yesql
databasejavascriptv7.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (require function)
const sql = require('yesql')('/path/to/sql', {type: 'pg'})
const yesql = require('yesql'); const sql = yesql('/path/to/sql')()
The default export is a factory function that takes a directory path and options object.
mysql
const named = require('yesql').mysql
const { mysql } = require('yesql')
Named exports are available as .mysql and .pg.
pg
const named = require('yesql').pg
const pg = require('yesql').pg
Use .pg for PostgreSQL prepared statements.

Demonstrates reading SQL from files and using named parameters with PostgreSQL.

const sql = require('yesql')('/path/to/sql', {type: 'pg'}); const pg = require('pg'); const client = new pg.Client(); // query with named parameters client.query(sql.updatePokemon({price: 5, name: 'Pikachu'}), (err, res) => { if (err) throw err; console.log(res.rows); }); // also works without directory: use named directly const named = require('yesql').pg; const query = named('UPDATE pokemon SET price = :price WHERE id = :id;'); client.query(query({price: 10, id: 1}), (err, res) => { if (err) throw err; });
Debug
Known issues
breakingDuplicate SQL query names now throw an error (since v5.0.0). Before v5.0.0 duplicates silently overwrote.
fix
Ensure each query name in .sql files is unique, remove or rename duplicates.
affects: >=5.0.0
deprecatedThe 'useNullForMissing' flag changes the behavior of missing parameters, potentially masking errors by inserting NULL instead of throwing.
fix
If you rely on missing parameters being an error, do not set useNullForMissing: true. If you want NULLs, continue using it.
affects: >=4.1.0
gotchaThe directory path passed to yesql() should be the path to the SQL files directory, not the files themselves. The library reads all .sql files in that directory.
fix
Pass a directory path: yesql('./sql/'), not yesql('./sql/query.sql').
affects: all
gotchaFor PostgreSQL, the named parameter syntax uses :name (colon prefix). For MySQL, it also uses :name. For SQLite, use $name. This is database-specific and not interchangeable.
fix
Use the correct parameter syntax for your database type. For PG/MySQL use :name; for SQLite use $name; for raw SQL use ?.
affects: all
Errors
Common errors & fixes
Error: Cannot find module 'yesql'
yesql is not installed.
fix
npm install yesql
TypeError: sql.getPokemon is not a function
Calling a query without parenthesized arguments. yesql returns functions that must be called with parameters object.
fix
Use sql.getPokemon({param: value}) instead of sql.getPokemon.
Error: Duplicate query name found: getPokemon
Two queries in .sql files have the same comment-based name.
fix
Rename one of the queries to have a unique name.
Upgrade
Version history
7.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
24
OpenAI (training)
1
Resources
packageyesql
yesql — npm install yesql · libregistry