npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-sql-enhanced

v2.0.6

Published

Node.js library for mapping MongoDB-style query objects to SQL queries with enhanced operators and multi-dialect support

Readme

JSON-SQL Enhanced - Modern Edition

A powerful, modern fork of json-sql with comprehensive MongoDB operators and multi-dialect support

Node.js Version License: MIT build status code style styled with prettier license

🚀 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')();

// Basic query
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);
// Output: select * from "users" where "name" ILIKE $p1 and "age" > $p2 and JSON_LENGTH("emails") > $p3

console.log(result.values);
// Output: { p1: '%John%', p2: 18, p3: 0 }

🔍 MongoDB Operators

$regex with $options

// Case-insensitive pattern matching
{ fullName: { $regex: 'John', $options: 'i' } }
// → PostgreSQL: "fullName" ILIKE '%John%'
// → MySQL: LOWER("fullName") LIKE LOWER('%John%')
// → SQLite: "fullName" LIKE '%John%' COLLATE NOCASE

// Pattern optimization
{ name: { $regex: '^John' } }     // → "name" LIKE 'John%'
{ name: { $regex: 'Smith$' } }    // → "name" LIKE '%Smith'
{ name: { $regex: '^John$' } }    // → "name" = 'John'

$elemMatch for Arrays

// Complex array element matching
{
  emails: {
    $elemMatch: {
      value: { $regex: '@company\\.com$', $options: 'i' },
      type: 'work'
    }
  }
}
// → PostgreSQL: EXISTS(SELECT 1 FROM jsonb_array_elements("emails") elem WHERE ...)
// → MySQL: JSON_SEARCH("emails", 'one', '%@company.com%') IS NOT NULL
// → SQLite: EXISTS(SELECT 1 FROM json_each("emails") WHERE ...)

$size for Array Length

{
  tags: {
    $size: 3;
  }
} // → JSON_LENGTH("tags") = 3
{
  emails: {
    $size: {
      $gt: 0;
    }
  }
} // → JSON_LENGTH("emails") > 0

$exists for Field Presence

{
  email: {
    $exists: true;
  }
} // → "email" IS NOT NULL
{
  phone: {
    $exists: false;
  }
} // → "phone" IS NULL

Complex Logical Operations

{
  $and: [
    { age: { $gte: 18 } },
    {
      $or: [{ status: 'active' }, { emails: { $size: { $gt: 0 } } }],
    },
  ];
}

🌐 Multi-Dialect Support

// PostgreSQL optimizations
const pgSql = require('json-sql-enhanced')({ dialect: 'postgresql' });

// MySQL optimizations
const mysqlSql = require('json-sql-enhanced')({ dialect: 'mysql' });

// SQLite optimizations
const sqliteSql = require('json-sql-enhanced')({ dialect: 'sqlite' });

// SQL Server support
const mssqlSql = require('json-sql-enhanced')({ dialect: 'mssql' });

🔄 Migration from Original json-sql

This fork is 100% backward compatible. Simply replace your import:

// Before
const jsonSql = require('json-sql')();

// After
const jsonSql = require('json-sql-enhanced')();

// All existing code works unchanged!

🧪 Example Queries

All these complex MongoDB-style queries are supported:

// Case-insensitive regex with options
{ fullName: { $regex: 'John', $options: 'i' } }

// Array element matching with nested conditions
{
  emails: {
    $elemMatch: {
      value: { $regex: 'john@example\\.com', $options: 'i' }
    }
  }
}

// Complex logical operations
{
  $or: [
    { emails: { $exists: false } },
    { emails: { $size: 0 } }
  ]
}

// Negation with nested operators
{
  $not: {
    emails: {
      $elemMatch: { type: { $regex: '^WORK$', $options: 'i' } }
    }
  }
}

// Multiple conditions with $and
{
  $and: [
    {
      emails: {
        $elemMatch: { value: { $regex: '@example\\.com', $options: 'i' } }
      }
    },
    {
      emails: { $elemMatch: { value: { $regex: '^john', $options: 'i' } } }
    }
  ]
}

🛠️ Development

# Install dependencies
npm install

# Run tests
npm test

# Format code
npm run format

# Lint code
npm run lint

# Fix linting issues
npm run lint:fix

📋 Requirements

  • Node.js 18+
  • Modern JavaScript environment

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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