Registry / database / obj-2-sql

obj-2-sql

JSON →
library1.0.1jsnpmunverified

Obj-2-SQL is a lightweight JavaScript/TypeScript library (v1.0.1) for converting objects into SQL query strings, supporting SELECT, WHERE, JOIN, GROUP BY, HAVING, pagination, and basic SQL injection prevention via type validation and escaping. It offers a simple functional API without heavy ORM configuration, ideal for quick query building. Active development with monthly releases. Differentiators: no external dependencies, TypeScript types included, explicit injection countermeasures.

npm install obj-2-sql
INSTALL
IMPORT
SIG · OBJ-2-SQL
O
obj-2-sql
databasejavascriptv1.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.

getWhereQuerys
import { getWhereQuerys } from 'obj-2-sql'
const getWhereQuerys = require('obj-2-sql')
ESM-only; module uses export syntax.
getSelectSql
import { getSelectSql } from 'obj-2-sql'
default import
import * as obj2sql from 'obj-2-sql'
import obj2sql from 'obj-2-sql'
No default export; only named exports.

Demonstrates generating WHERE clause and full SELECT with JOIN, GROUP BY, HAVING, and pagination from object inputs.

import { getWhereQuerys, getSelectSql } from 'obj-2-sql'; // WHERE clause from array of conditions const conditions = [ { field: 'name', type: '=', value: 'bob' }, { field: 'age', type: '>', value: 18 } ]; const whereQuery = getWhereQuerys(conditions); console.log(whereQuery); // `name` = 'bob' AND `age` > 18 // Full SELECT with JOIN, GROUP BY, pagination const selectSql = getSelectSql({ table: 'order_detail', fields: [ { field: 'product_id', table: 'order_detail' }, { field: 'product_count', table: 'order_detail', aggType: 'SUM', alias: 'total_count' }, { field: 'product_name', table: 'product' } ], where: [{ field: { field: 'product_id', table: 'order_detail' }, type: '>', value: 1 }], join: [ { table: 'product', on: [ { table: 'order_detail', field: 'product_id' }, { table: 'product', field: 'id' } ], type: 'LEFT' } ], group: { field: [ { field: 'product_id', table: 'order_detail' }, { field: 'product_name', table: 'product' } ], having: [{ field: { table: 'order_detail', field: 'product_id' }, type: '>', value: 1 }] }, page: { page: 3, pageSize: 20 } }); console.log(selectSql); // SELECT `order_detail`.`product_id`, SUM(`order_detail`.`product_count`) AS `total_count`, `product`.`product_name` FROM `order_detail` LEFT JOIN `product` ON `order_detail`.`product_id` = `product`.`id` WHERE `order_detail`.`product_id` > 1 GROUP BY `order_detail`.`product_id`, `product`.`product_name` HAVING `order_detail`.`product_id` > 1 LIMIT 40, 20
Debug
Known issues
gotchaThe 'type' field in conditions is not validated for custom operators; arbitrary strings may cause SQL injection unless explicitly rejected.
fix
Ensure user input for 'type' is validated against a whitelist of allowed operators.
affects: <1.0.1
gotchaThe 'field' in conditions can be a string or object; using nested objects may be error-prone.
fix
Use consistent format: for simple fields, use a string; for table-qualified fields, provide { field: 'col', table: 'tab' }.
affects: all
gotchagetWhereQuerys expects an array; passing a single object will cause runtime error.
fix
Always pass an array, even for a single condition: [{ field: 'x', type: '=', value: 1 }]
affects: all
Errors
Common errors & fixes
getWhereQuerys(...) is not a function
Incorrect import (e.g., default import or require).
fix
Use named import: import { getWhereQuerys } from 'obj-2-sql'
Cannot read properties of undefined (reading 'field')
Passing an object instead of array to getWhereQuerys.
fix
Wrap single condition in array: [{field: 'name', type: '=', value: 'bob'}]
不支持的条件类型:<user_input>
Invalid operator string supplied to condition's 'type' field.
fix
Only use supported operators: '=', '>', '<', '>=', '<=', '!=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE'.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

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