Registry / database / knight-sql

knight-sql

JSON →
library2.2.2jsnpmunverified

A TypeScript data structure for building SQL queries programmatically. Current version 2.2.2, updated regularly. It concatenates strings and objects to produce SQL strings, supporting MySQL and PostgreSQL parameter styles. Differentiates from ORMs by offering a lightweight, composable query builder without database access. Ships TypeScript types.

npm install knight-sql
INSTALL
IMPORT
SIG · KNIGHT-SQL
K
knight-sql
databasejavascriptv2.2.2
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
import sql from 'knight-sql'
import { sql } from 'knight-sql'
Default export only; named export 'sql' does not exist.
parameter
import { parameter } from 'knight-sql'
import parameter from 'knight-sql'
parameter is a named export, not the default.
Query
import { Query } from 'knight-sql'
import Query from 'knight-sql'
Query type is a named export, no default export for it.

Shows building a SELECT query with JOIN and parameterized WHERE clause, then rendering to MySQL and PostgreSQL syntax.

import sql, { parameter } from 'knight-sql'; const query = sql .select('id', 'name') .from('users') .where('age >', parameter(18)) .join('orders', 'users.id = orders.user_id'); const mysqlStr = query.mysql(); // 'SELECT id, name FROM users JOIN orders ON users.id = orders.user_id WHERE age > ?' const postgresStr = query.postgres(); // 'SELECT id, name FROM users JOIN orders ON users.id = orders.user_id WHERE age > $1' const values = query.values(); // [ 18 ]
Debug
Known issues
gotchaDefault import 'sql' is a factory function, not an object. Must call methods on it directly; cannot create reusable instances.
fix
Use sql.select(...).from(...)... each time, or wrap in a function.
affects: >=0.0.0
gotchaThe `values()` method returns an array only if `parameter()` is used for placeholders; inline values are embedded directly in the SQL string and not included in `values()`.
fix
Use `parameter(value)` to ensure value is parameterized and included in `values()`.
affects: >=0.0.0
gotchaDifferent SQL dialects (mysql vs postgres) produce different parameter placeholders (? vs $1), but the same query object is mutated: calling both mysql() and postgres() on the same query may lead to unexpected results if parameters are added mid-rendering.
fix
Render to one dialect per query instance; do not switch dialects on the same query object.
affects: >=0.0.0
deprecatedThe `CustomSqlPiece` interface is mentioned in docs but may be renamed in future versions; reliance on it is discouraged.
fix
Use `parameter()` for value injection instead of custom objects.
affects: >=2.0.0
gotchaThe `join()` method overloads: first argument can be a join type ('left', 'right', 'inner') or table name. Pass a string first to specify join type, otherwise it's treated as table.
fix
For a LEFT JOIN, call `.join('left', 'table', 'alias', 'onClause')`.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: sql.select is not a function
Default import used incorrectly as named import.
fix
Use `import sql from 'knight-sql'` (no curly braces).
TypeError: parameter is not a function
parameter imported as default instead of named import.
fix
Use `import { parameter } from 'knight-sql'`.
Property 'mysql' does not exist on type '...'
TypeScript types missing or outdated, or import path wrong.
fix
Ensure you have version 2.x and import the default export correctly.
Upgrade
Version history
2.2.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
knight-sql — npm install knight-sql · libregistry