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.
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.
ESM-only; module uses export syntax.
import { getWhereQuerys } from 'obj-2-sql'
import { getSelectSql } from 'obj-2-sql'
No default export; only named exports.
import * as obj2sql from 'obj-2-sql'
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
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.