safesql is an npm package (v2.0.2) that provides ES6 template tag functions for safely composing MySQL and PostgreSQL query strings. It automatically escapes dynamic values based on context (identifiers, values, raw expressions) to prevent SQL injection. Written by Mike Samuel, a security engineer, it is part of the template-tag-common family. The library is lightweight, has no runtime dependencies, and supports both CommonJS and ESM (via bundlers). Unlike string concatenation or naive escaping, safesql uses tagged template literals to distinguish between identifiers and values, and handles arrays, objects, and nested SqlFragment instances correctly. Release cadence is low, with version 2.0.2 current as of 2024.
npm install safesqlNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates building a safe SQL UPDATE query with identifier escaping, object SET clause, and nested raw SQL via SqlFragment.
Always prefix your SQL string with the mysql or pg tag, e.g., mysql`SELECT ...` not just `SELECT ...`.
Use ${} inside \` `identifier`\` for MySQL or "${identifier}" for PostgreSQL to mark identifiers.Rely on safesql for escaping, but test your queries with a database to ensure correctness.
Use named imports: import { mysql, pg, SqlFragment, SqlId } from 'safesql'.Change to named import: import { mysql } from 'safesql'Ensure template literals use escaped backticks for identifiers: \`table\`
Add import: import { mysql } from 'safesql'Run npm install safesql. TypeScript types are included via @types/safesql if needed.
No dependency data recorded yet.