Registry / database / sql-dialect

sql-dialect

JSON →
library5.0.1jsnpmunverified

SQL builder library providing query builders independent of any database connection library. Current stable version is 5.0.1. It supports MySQL, PostgreSQL, and SQLite dialects, and uses prefix notation (Polish notation) for building complex queries. Key differentiators: provides SELECT, UPDATE, INSERT, DELETE, CREATE TABLE, and DROP TABLE operations; automatic quoting and escaping to prevent SQL injection; allows injecting custom quoter functions for database-specific quoting; does not handle query execution, only SQL generation. Release cadence is irregular; minor versions occasionally introduce breaking changes.

npm install sql-dialect
INSTALL
IMPORT
SIG · SQL-DIALECT
S
sql-dialect
databasejavascriptv5.0.1
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.

MySql
const { MySql } = require('sql-dialect');
const MySql = require('sql-dialect').MySql;
CommonJS require destructuring works, but direct require on property is also valid. ESM: import { MySql } from 'sql-dialect';
PostgreSql
const { PostgreSql } = require('sql-dialect');
const PostgreSql = require('sql-dialect/PostgreSql');
Do not include path to submodule; all dialects are exported from the main package.
Dialect (base class)
const { Dialect } = require('sql-dialect');
const Dialect = require('sql-dialect').Dialect;
Base Dialect class is also exported. Used for custom dialects.
Sqlite
const { Sqlite } = require('sql-dialect');
const Sqlite = require('sql-dialect/sqlite');
Case-sensitive: 'Sqlite' not 'SQLite'.

Shows creating MySQL dialect, building SELECT with where condition, ordering, limit, and INSERT with set.

const { MySql } = require('sql-dialect'); // Create a MySQL dialect instance const dialect = new MySql(); // Create a SELECT statement const select = dialect.statement('select'); select.from('users'); select.fields(['id', 'name']); select.where([{ age: { '>': 18 } }]); // uses prefix notation select.order('name'); select.limit(10); // Generate SQL string const sql = select.toString(); console.log(sql); // Output: SELECT "id", "name" FROM "users" WHERE "age" > 18 ORDER BY "name" LIMIT 10 // Insert example const insert = dialect.statement('insert'); insert.into('users'); insert.set({ name: 'John', age: 30 }); console.log(insert.toString()); // Output: INSERT INTO "users" ("name", "age") VALUES ('John', 30)
Debug
Known issues
breakingVersion 5.0 changed how statements are created: `dialect.statement('select')` instead of `dialect.select()` or similar. Old method removed.
fix
Use dialect.statement('select') to create statement objects.
affects: <5.0.0
deprecatedVersion 5.0 deprecates direct method calls on dialect for statement creation (e.g., dialect.select()). Use dialect.statement() instead.
fix
Migrate to dialect.statement('select') for all query types.
affects: >=5.0.0
gotchaPrefix notation: conditions must be passed as arrays of objects. Simple key-value pairs (e.g., {field: value}) are treated as equality by default. Use operators like { '>': value } inside nested objects.
fix
Use array syntax with objects: [{ field: { '>': 5 } }] for comparisons.
affects: >=1.0.0
gotchaTable and column names are automatically quoted (escaped). If you need to disable quoting, you must pass a custom quoter that returns unquoted strings.
fix
Set quoter option to null or a no-op function: new MySql({ quoter: s => s });
affects: >=1.0.0
gotchaThe library does not execute queries; it only generates SQL strings. You must pass the generated SQL to a database driver.
fix
Use a database connection library like 'pg', 'mysql2', or 'sqlite3' to run the SQL.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: dialect.select is not a function
Using old API where methods like dialect.select() existed; in v5, they are removed.
fix
Replace dialect.select() with dialect.statement('select').
SyntaxError: Unexpected token '.'
Attempting to call chained methods on undefined because statement creation returned undefined due to wrong method name.
fix
Ensure you use dialect.statement('select') and then chain methods on the returned statement object.
Error: Unsupported operator: ==
Using JavaScript equality operators in condition objects; prefix notation uses SQL operators like '=', '>', etc.
fix
Use SQL operators as strings: { field: { '=': value } } or just { field: value } for equality.
Error: Cannot find module 'sql-dialect/PostgreSql'
Importing from a subpath that doesn't exist; all exports are from the root package.
fix
Use require('sql-dialect') and destructure PostgreSql from it.
Upgrade
Version history
5.0.1latest on npm
Audit
Dependencies
sql-dialectrequiredThe package itself; all imports come from this package.
Agent activity
18 hits · last 30 days
node
16
Meta
1
OpenAI (training)
1
Resources
sql-dialect — npm install sql-dialect · libregistry