JSON-SQL Enhanced - Modern Edition
A powerful, modern fork of json-sql with comprehensive MongoDB operators and multi-dialect support

🚀 What's New in This Fork
This is a comprehensive modernization and enhancement of the original json-sql library, featuring:
✨ Modern JavaScript & Tooling
- Node.js 18+ support with modern ES features
- Zero dependencies - removed underscore.js, using native JavaScript
- Modern development workflow with Prettier, XO, ESLint
- Ava test framework replacing Mocha/Chai
- Git hooks with Husky, lint-staged, and commitlint
- 2-space indentation throughout codebase
🔥 Enhanced MongoDB Operators
$regex
with $options
support for case-insensitive pattern matching
$size
for array length queries
$exists
for field existence checks
$elemMatch
for complex array element matching
- Field-level
$not
for negation
- Complex
$and
/$or
with nested logical operations
🗄️ Multi-Dialect Optimization
- PostgreSQL: Native
~
, ~*
operators, ILIKE, JSONB functions
- MySQL: REGEXP operator, JSON functions, case-insensitive handling
- SQL Server: Pattern approximation, OPENJSON for arrays
- SQLite: Progressive fallback (LIKE → GLOB → REGEXP), JSON1 support
🐛 GitHub Issues Fixed
- #57: Empty objects
{}
convert to NULL
- #56: Buffer support with automatic hex conversion
- #55: BSON ObjectId support with
toHexString()
method
📦 Installation
npm install json-sql-enhanced
🎯 Quick Start
const jsonSql = require('json-sql-enhanced')();
const result = jsonSql.build({
type: 'select',
table: 'users',
condition: {
name: { $regex: 'John', $options: 'i' },
age: { $gt: 18 },
emails: { $size: { $gt: 0 } },
},
});
console.log(result.query);
console.log(result.values);
🔍 MongoDB Operators
$regex with $options
{ fullName: { $regex: 'John', $options: 'i' } }
{ name: { $regex: '^John' } }
{ name: { $regex: 'Smith$' } }
{ name: { $regex: '^John$' } }
$elemMatch for Arrays
{
emails: {
$elemMatch: {
value: { $regex: '@company\\.com$', $options: 'i' },
type: 'work'
}
}
}
$size for Array Length
{
tags: {
$size: 3;
}
}
{
emails: {
$size: {
$gt: 0;
}
}
}
$exists for Field Presence
{
email: {
$exists: true;
}
}
{
phone: {
$exists: false;
}
}
Complex Logical Operations
{
$and: [
{ age: { $gte: 18 } },
{
$or: [{ status: 'active' }, { emails: { $size: { $gt: 0 } } }],
},
];
}
🌐 Multi-Dialect Support
const pgSql = require('json-sql-enhanced')({ dialect: 'postgresql' });
const mysqlSql = require('json-sql-enhanced')({ dialect: 'mysql' });
const sqliteSql = require('json-sql-enhanced')({ dialect: 'sqlite' });
const mssqlSql = require('json-sql-enhanced')({ dialect: 'mssql' });
🔄 Migration from Original json-sql
This fork is 100% backward compatible. Simply replace your import:
const jsonSql = require('json-sql')();
const jsonSql = require('json-sql-enhanced')();
🧪 Example Queries
All these complex MongoDB-style queries are supported:
{ fullName: { $regex: 'John', $options: 'i' } }
{
emails: {
$elemMatch: {
value: { $regex: 'john@example\\.com', $options: 'i' }
}
}
}
{
$or: [
{ emails: { $exists: false } },
{ emails: { $size: 0 } }
]
}
{
$not: {
emails: {
$elemMatch: { type: { $regex: '^WORK$', $options: 'i' } }
}
}
}
{
$and: [
{
emails: {
$elemMatch: { value: { $regex: '@example\\.com', $options: 'i' } }
}
},
{
emails: { $elemMatch: { value: { $regex: '^john', $options: 'i' } } }
}
]
}
🛠️ Development
npm install
npm test
npm run format
npm run lint
npm run lint:fix
📋 Requirements
- Node.js 18+
- Modern JavaScript environment
🤝 Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
)
- Commit your changes (
git commit -m 'feat: add amazing feature'
)
- Push to the branch (
git push origin feature/amazing-feature
)
- Open a Pull Request
📄 License
MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Original json-sql library by 2do2go
- MongoDB query syntax inspiration
- Modern JavaScript community for best practices
📚 Documentation
For detailed documentation, examples, and API reference, see the docs directory:
Made with ❤️ for the JavaScript community