Skip to main content
@usenarrator/plugin-sql Raw SQL in a TypeScript file is usually the part a reviewer reads most carefully, and the part plain code narration can do least with. The SQL plugin finds SQL at its call sites, parses it, and reads it clause by clause. Interpolated values and $1 or ? placeholders are replaced by the JavaScript values they are bound to. It understands postgres.js, Bun.sql, pg, mysql2, better-sqlite3, bun:sqlite, Prisma’s raw queries, Neon, slonik and kysely.
The parser is injectable because it is the heaviest part. pgsqlParser uses pgsql-ast-parser, a peer dependency you install yourself. Without a parser the plugin still recognises SQL call sites but shows the SQL text as it is. You can also pass a getter to load the parser lazily.

Examples

A tagged template
English
pg with placeholders
English
Upserts
English
An update without a WHERE clause
English

What it covers

  • Tagged templates (sql, Bun.sql, Prisma’s $queryRaw and $executeRaw, slonik), sql.unsafe, and kysely’s .execute(db).
  • Query functions on database handles such as db, pool, client and tx, with $1, ? and $name placeholders, value arrays, and { text, values } objects.
  • Prepared statements in better-sqlite3 and bun:sqlite, including statements declared once and run later.
  • SQL constants, narrated where they are declared and where they run.
  • Selects with joins, grouping, window functions, CASE, subqueries, CTEs (including recursive ones) and UNION, plus inserts, updates, deletes, upserts, RETURNING, row locking and JSONB operators.
When the Drizzle plugin is loaded, Drizzle’s own sql template is left to the Drizzle plugin.

Translating

The English phrasebook is exported as en and typed as SqlPhrases.