Registry / database / sql-js-builder

sql-js-builder

JSON →
library0.0.18jsnpmunverified

A lightweight, driver-agnostic SQL WHERE clause builder that uses plain JavaScript data structures and the builder pattern. Version 0.0.18 ships TypeScript types and is released on npm. Unlike many SQL builders, this library does not depend on any specific database driver or ORM, outputting raw SQL fragments with `?` placeholders. It is designed to simplify constructing parameterized WHERE clauses without string concatenation, with a focus on maintainability and minimal external dependencies. The library currently supports basic comparison operators (eq, neq, gt, gte, lt, lte) and logical grouping, but does not support indexed placeholders, joins, or subqueries. Its release cadence is infrequent, with the last update in 2024.

npm install sql-js-builder
INSTALL
IMPORT
SIG · SQL-JS-BUILDER
S
sql-js-builder
databasejavascriptv0.0.18
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.

where
import { where } from 'sql-js-builder'
import where from 'sql-js-builder'
Use named import, not default. The library has no default export.
and
where().and('col', 'eq', val)
where().and("col", "eq", val)
Operator strings must be lowercase (eq, neq, gt, gte, lt, lte). Double quotes around identifiers are fine but not mandatory.
build
const { sql, values } = where().build()
const result = where().build(); result.sql; result.values
Destructure the result object to get sql and values arrays. build() does not accept arguments.

Shows how to build a WHERE clause with multiple conditions, destructure the SQL and values, and adapt for databases with different placeholder styles.

import { where } from 'sql-js-builder'; const { sql, values } = where() .and('age', 'gte', 18) .and('country', 'eq', 'Brazil') .build(); // Use the fragment with your own query const query = `SELECT * FROM users WHERE ${sql}`; console.log(query); // SELECT * FROM users WHERE 1 = 1 AND "age" >= ? AND "country" = ? console.log(values); // [18, 'Brazil'] // For databases using indexed placeholders (e.g. PostgreSQL $1, $2): const reindexedSql = sql.replace(/\?/g, (match, offset) => { let idx = 1; // simple replacement: assumes ? are not in strings return `$${idx++}`; });
Debug
Known issues
gotchaPlaceholders are always `?` which may not be compatible with all databases (e.g. PostgreSQL requires $1, $2). User must manually re-index placeholders.
fix
Write a helper function to replace `?` with indexed placeholders based on the order of values.
affects: all
gotchaColumn names are not escaped or quoted automatically. If column names contain special characters or are reserved words, SQL injection is possible through column names.
fix
Avoid dynamic column names; if necessary, sanitize or quote them manually using backticks or double quotes.
affects: all
gotchaOnly supports equality and comparison operators (eq, neq, gt, gte, lt, lte). No IN, LIKE, BETWEEN, IS NULL, or other SQL operators.
fix
Use raw SQL fragments for complex conditions or consider an alternative library with broader operator support.
affects: all
gotchaNo support for OR clauses, nested grouping, or complex boolean logic. The builder only chains AND conditions.
fix
Construct your own WHERE fragments manually for advanced logic, or use a more comprehensive SQL builder.
affects: all
deprecatedThe package has not been updated since 2024; future compatibility with newer Node.js or driver versions is uncertain.
fix
Monitor the repository for updates or consider migrating to a maintained alternative.
affects: all
Errors
Common errors & fixes
TypeError: (0 , sql_js_builder_1.where) is not a function
Using default import instead of named import.
fix
Change 'import where from "sql-js-builder"' to 'import { where } from "sql-js-builder"'.
The column name "status" is not recognized.
Trying to use a column name that contains spaces or special characters without quoting.
fix
Quote column names manually: .and('"user status"', 'eq', 'active')
Operator "in" is not supported.
SQL JS Builder only supports eq, neq, gt, gte, lt, lte.
fix
Use raw SQL: `{ sql: \`"col" IN (${values.map(() => '?').join(',')})\`, values: [...] }`
Upgrade
Version history
0.0.18latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
Meta
2
OpenAI (training)
1
Resources
sql-js-builder — npm install sql-js-builder · libregistry