Registry / database / extra-sql-builder

extra-sql-builder

JSON →
library0.3.4jsnpmunverified

A lightweight, composable SQL builder for TypeScript and JavaScript that constructs SQL statements programmatically without string concatenation or ORM overhead. Current stable version 0.3.4 (semver pre-1.0). It provides a flexible `sql` tagged template literal and function, along with a `ParameterCollector` for named or indexed parameters to prevent SQL injection. Key differentiators: no dependency on ORM, intuitive function-based query construction (e.g., `INSERT_INTO`, `SELECT`, `WHERE`), support for conditional fragments via falsy values, and a small API surface focused solely on SQL generation. TypeScript types included.

database
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.

Package is ESM-only. Use named import, not default.

import { sql } from 'extra-sql-builder'

All fragment functions (e.g., INSERT_INTO, VALUES, SELECT) are named exports, not default exports.

import { INSERT_INTO, VALUES, SELECT } from 'extra-sql-builder'

Use named import with ES module syntax.

import { ParameterCollector } from 'extra-sql-builder'

Shows building a parameterized INSERT with conditional rows using ParameterCollector.

import { sql, INSERT_INTO, VALUES, integer, text, ParameterCollector } from 'extra-sql-builder' const cond = true const collector = new ParameterCollector('$param') const result = sql( INSERT_INTO('users', ['id', 'name']), VALUES( [collector.add(1), collector.add('Alice')], cond && [collector.add(2), collector.add('Bob')] ) ) // result: INSERT INTO users (id, name) VALUES ($param1, $param2), ($param3, $param4) const params = collector.toRecord() // { param1: 1, param2: 'Alice', param3: 2, param4: 'Bob' } console.log(result, params)
Debug
Known footguns
gotchaThe `sql` function accepts both tagged template literals and function arguments, but mixing them can cause unexpected results.
gotchaFalsy values (false, null, undefined, 0, '') are omitted from fragments, which may inadvertently drop zero or empty strings used as legitimate values.
gotchaThe `ParameterCollector` stores values by reference; mutating values after adding them does not affect collected parameters.
breakingPre-1.0 semver: minor version bumps may introduce breaking changes. No stable API yet.
deprecatedThe package does not support CommonJS; using `require()` will fail.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
7 hits · last 30 days
gptbot
3
mj12bot
1
Resources