Registry / database / msql
library1.0.3jsnpmunverified

msql is a lightweight JavaScript library for programmatically building SQL query strings (SELECT, INSERT, UPDATE, DELETE) without raw string concatenation, reducing SQL injection risk. Version 1.0.3 is the latest stable release. It supports a fluent API with methods like .where(), .field(), .config() for primary key assignment, enabling chainable query construction. Unlike ORMs, msql outputs raw SQL strings and targets Node.js with CommonJS only (no ESM). It is suitable for simple projects needing dynamic SQL generation, but lacks async support, prepared statements, and broader SQL dialect coverage.

npm install msql
INSTALL
IMPORT
SIG · MSQL
M
msql
databasejavascriptv1.0.3
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
const msql = require('msql')
import msql from 'msql'
msql is CommonJS-only; ESM imports will fail. Use require() in Node.js.
default (with table name)
const qb = msql('tableName')
const qb = new msql('tableName')
msql() is a factory function, not a constructor. Do not use 'new'.
config (optional primary key)
msql('tableName').config({ pk: 'id' })
msql('tableName').config({ primaryKey: 'id' })
The config option is named 'pk', not 'primaryKey' or 'key'.

Demonstrates building SQL queries (SELECT, INSERT, UPDATE, DELETE) using msql's fluent API with configurable primary key.

const msql = require('msql'); // Create a query builder for the 'users' table const qb = msql('users').config({ pk: 'id' }); // SELECT with where var sqlSelect = qb.where({ id: 1 }).select(); console.log(sqlSelect); // SELECT * FROM users where id = 1 // INSERT var sqlInsert = msql('users').create({ name: 'Alice', age: 30 }); console.log(sqlInsert); // INSERT INTO users(name, age) VALUES ("Alice", 30) // UPDATE var sqlUpdate = msql('users').config({ pk: 'id' }).update({ id: 1, name: 'Bob' }); console.log(sqlUpdate); // UPDATE users SET name = "Bob" where id = 1 // DELETE var sqlDelete = msql('users').delete({ id: 1 }); console.log(sqlDelete); // DELETE FROM users where id = 1
Debug
Known issues
gotchaValues are not sanitized; library does not use parameterized queries or escaping. Direct string interpolation can lead to SQL injection if values come from user input.
fix
Manually sanitize or escape user-provided values before passing to msql methods, or switch to a library that supports prepared statements.
affects: >=0.0.0
deprecatedThe README shows deprecated usage: `var Msql = Msql('img').config(...)` reassigns the Msql variable, which overwrites the original require reference. This is error-prone.
fix
Use a new variable: `const imgTable = msql('img').config(...)` and continue with imgTable instead of reusing Msql.
affects: >=0.0.0
gotchaThe `find` method with a primary key value requires that `config({ pk: ... })` has been called on the same query builder instance; otherwise find({ id: 1 }) works but find(1) will produce incorrect SQL.
fix
Always call .config({ pk: 'id' }) before using .find(value) where value is not an object.
affects: >=0.0.0
gotchamsql does not support JOINs, subqueries, aggregate functions, or LIMIT/OFFSET clauses natively. Complex SQL must be manually written.
fix
Use a more full-featured SQL builder library like knex.js if you need advanced query generation.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: msql is not a function
Using ES6 import syntax 'import msql from 'msql'' on a CommonJS-only module.
fix
Replace with const msql = require('msql');
SELECT * FROM users where id = undefined
Calling .find() with no arguments or with an undefined primary key value.
fix
Ensure you pass a valid value to .find() or call .where() before .select().
Uncaught ReferenceError: Msql is not defined
Overwriting the 'Msql' variable in the example: var Msql = Msql('img').config(...) redefines Msql as a query builder, losing the require reference.
fix
Use a different variable name: const img = msql('img').config(...);
INSERT INTO users(path, con) VALUES ("path1", "This is a new record") -> but content says 'This is content'
The README example has a mismatch: the data object has con: 'This is content' but the output shows 'This is a new record'. This is a documentation bug.
fix
Ignore the discrepancy; the library will use the actual value provided in the data object.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
packagemsql
msql — npm install msql · libregistry