Skip to content

Commit f473b40

Browse files
committed
feat: initial release
0 parents  commit f473b40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+9439
-0
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: CI
2+
on:
3+
- push
4+
- pull_request
5+
jobs:
6+
build:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
os:
11+
- ubuntu-latest
12+
node_version:
13+
- 18
14+
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Setup node
18+
uses: actions/setup-node@v3
19+
with:
20+
node-version: ${{ matrix.node_version }}
21+
- name: Install dependencies
22+
run: npm install
23+
- name: Run tests
24+
run: npm run test

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.DS_Store
2+
*.log
3+
.idea
4+
node_modules
5+
coverage
6+
.nyc_output
7+
locales/
8+
package-lock.json
9+
yarn.lock
10+
11+
Thumbs.db
12+
tmp/
13+
temp/
14+
*.lcov

.husky/commit-msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npx --no -- commitlint --edit $1

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npx lint-staged

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tests/
2+
examples/
3+
coverage/
4+
docs/
5+
gulpfile.js

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2013-2015 2do2go team <dev.2do2go@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
# JSON-SQL Enhanced - Modern Edition
2+
3+
**A powerful, modern fork of json-sql with comprehensive MongoDB operators and multi-dialect support**
4+
5+
[![Node.js Version](https://img.shields.io/badge/node-%3E%3D18.0.0-brightgreen.svg)](https://nodejs.org/)
6+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7+
[![build status](https://github.com/forwardemail/json-sql-enhanced/actions/workflows/ci.yml/badge.svg)](https://github.com/forwardemail/json-sql-enhanced/actions/workflows/ci.yml)
8+
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
9+
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
10+
[![license](https://img.shields.io/github/license/forwardemail/json-sql-enhanced.svg)](LICENSE)
11+
12+
## 🚀 What's New in This Fork
13+
14+
This is a comprehensive modernization and enhancement of the original [json-sql](https://github.com/2do2go/json-sql) library, featuring:
15+
16+
### **Modern JavaScript & Tooling**
17+
18+
- **Node.js 18+ support** with modern ES features
19+
- **Zero dependencies** - removed underscore.js, using native JavaScript
20+
- **Modern development workflow** with Prettier, XO, ESLint
21+
- **Ava test framework** replacing Mocha/Chai
22+
- **Git hooks** with Husky, lint-staged, and commitlint
23+
- **2-space indentation** throughout codebase
24+
25+
### 🔥 **Enhanced MongoDB Operators**
26+
27+
- **`$regex`** with `$options` support for case-insensitive pattern matching
28+
- **`$size`** for array length queries
29+
- **`$exists`** for field existence checks
30+
- **`$elemMatch`** for complex array element matching
31+
- **Field-level `$not`** for negation
32+
- **Complex `$and`/`$or`** with nested logical operations
33+
34+
### 🗄️ **Multi-Dialect Optimization**
35+
36+
- **PostgreSQL**: Native `~`, `~*` operators, ILIKE, JSONB functions
37+
- **MySQL**: REGEXP operator, JSON functions, case-insensitive handling
38+
- **SQL Server**: Pattern approximation, OPENJSON for arrays
39+
- **SQLite**: Progressive fallback (LIKE → GLOB → REGEXP), JSON1 support
40+
41+
### 🐛 **GitHub Issues Fixed**
42+
43+
- **#57**: Empty objects `{}` convert to `NULL`
44+
- **#56**: Buffer support with automatic hex conversion
45+
- **#55**: BSON ObjectId support with `toHexString()` method
46+
47+
## 📦 Installation
48+
49+
```bash
50+
npm install json-sql-enhanced
51+
```
52+
53+
## 🎯 Quick Start
54+
55+
```javascript
56+
const jsonSql = require('json-sql-enhanced')();
57+
58+
// Basic query
59+
const result = jsonSql.build({
60+
type: 'select',
61+
table: 'users',
62+
condition: {
63+
name: { $regex: 'John', $options: 'i' },
64+
age: { $gt: 18 },
65+
emails: { $size: { $gt: 0 } },
66+
},
67+
});
68+
69+
console.log(result.query);
70+
// Output: select * from "users" where "name" ILIKE $p1 and "age" > $p2 and JSON_LENGTH("emails") > $p3
71+
72+
console.log(result.values);
73+
// Output: { p1: '%John%', p2: 18, p3: 0 }
74+
```
75+
76+
## 🔍 MongoDB Operators
77+
78+
### **$regex with $options**
79+
80+
```javascript
81+
// Case-insensitive pattern matching
82+
{ fullName: { $regex: 'John', $options: 'i' } }
83+
// → PostgreSQL: "fullName" ILIKE '%John%'
84+
// → MySQL: LOWER("fullName") LIKE LOWER('%John%')
85+
// → SQLite: "fullName" LIKE '%John%' COLLATE NOCASE
86+
87+
// Pattern optimization
88+
{ name: { $regex: '^John' } } // → "name" LIKE 'John%'
89+
{ name: { $regex: 'Smith$' } } // → "name" LIKE '%Smith'
90+
{ name: { $regex: '^John$' } } // → "name" = 'John'
91+
```
92+
93+
### **$elemMatch for Arrays**
94+
95+
```javascript
96+
// Complex array element matching
97+
{
98+
emails: {
99+
$elemMatch: {
100+
value: { $regex: '@company\\.com$', $options: 'i' },
101+
type: 'work'
102+
}
103+
}
104+
}
105+
// → PostgreSQL: EXISTS(SELECT 1 FROM jsonb_array_elements("emails") elem WHERE ...)
106+
// → MySQL: JSON_SEARCH("emails", 'one', '%@company.com%') IS NOT NULL
107+
// → SQLite: EXISTS(SELECT 1 FROM json_each("emails") WHERE ...)
108+
```
109+
110+
### **$size for Array Length**
111+
112+
```javascript
113+
{
114+
tags: {
115+
$size: 3;
116+
}
117+
} // → JSON_LENGTH("tags") = 3
118+
{
119+
emails: {
120+
$size: {
121+
$gt: 0;
122+
}
123+
}
124+
} // → JSON_LENGTH("emails") > 0
125+
```
126+
127+
### **$exists for Field Presence**
128+
129+
```javascript
130+
{
131+
email: {
132+
$exists: true;
133+
}
134+
} // → "email" IS NOT NULL
135+
{
136+
phone: {
137+
$exists: false;
138+
}
139+
} // → "phone" IS NULL
140+
```
141+
142+
### **Complex Logical Operations**
143+
144+
```javascript
145+
{
146+
$and: [
147+
{ age: { $gte: 18 } },
148+
{
149+
$or: [{ status: 'active' }, { emails: { $size: { $gt: 0 } } }],
150+
},
151+
];
152+
}
153+
```
154+
155+
## 🌐 Multi-Dialect Support
156+
157+
```javascript
158+
// PostgreSQL optimizations
159+
const pgSql = require('json-sql-enhanced')({ dialect: 'postgresql' });
160+
161+
// MySQL optimizations
162+
const mysqlSql = require('json-sql-enhanced')({ dialect: 'mysql' });
163+
164+
// SQLite optimizations
165+
const sqliteSql = require('json-sql-enhanced')({ dialect: 'sqlite' });
166+
167+
// SQL Server support
168+
const mssqlSql = require('json-sql-enhanced')({ dialect: 'mssql' });
169+
```
170+
171+
## 🔄 Migration from Original json-sql
172+
173+
This fork is **100% backward compatible**. Simply replace your import:
174+
175+
```javascript
176+
// Before
177+
const jsonSql = require('json-sql')();
178+
179+
// After
180+
const jsonSql = require('json-sql-enhanced')();
181+
182+
// All existing code works unchanged!
183+
```
184+
185+
## 🧪 Example Queries
186+
187+
All these complex MongoDB-style queries are supported:
188+
189+
```javascript
190+
// Case-insensitive regex with options
191+
{ fullName: { $regex: 'John', $options: 'i' } }
192+
193+
// Array element matching with nested conditions
194+
{
195+
emails: {
196+
$elemMatch: {
197+
value: { $regex: 'john@example\\.com', $options: 'i' }
198+
}
199+
}
200+
}
201+
202+
// Complex logical operations
203+
{
204+
$or: [
205+
{ emails: { $exists: false } },
206+
{ emails: { $size: 0 } }
207+
]
208+
}
209+
210+
// Negation with nested operators
211+
{
212+
$not: {
213+
emails: {
214+
$elemMatch: { type: { $regex: '^WORK$', $options: 'i' } }
215+
}
216+
}
217+
}
218+
219+
// Multiple conditions with $and
220+
{
221+
$and: [
222+
{
223+
emails: {
224+
$elemMatch: { value: { $regex: '@example\\.com', $options: 'i' } }
225+
}
226+
},
227+
{
228+
emails: { $elemMatch: { value: { $regex: '^john', $options: 'i' } } }
229+
}
230+
]
231+
}
232+
```
233+
234+
## 🛠️ Development
235+
236+
```bash
237+
# Install dependencies
238+
npm install
239+
240+
# Run tests
241+
npm test
242+
243+
# Format code
244+
npm run format
245+
246+
# Lint code
247+
npm run lint
248+
249+
# Fix linting issues
250+
npm run lint:fix
251+
```
252+
253+
## 📋 Requirements
254+
255+
- **Node.js 18+**
256+
- **Modern JavaScript environment**
257+
258+
## 🤝 Contributing
259+
260+
1. Fork the repository
261+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
262+
3. Commit your changes (`git commit -m 'feat: add amazing feature'`)
263+
4. Push to the branch (`git push origin feature/amazing-feature`)
264+
5. Open a Pull Request
265+
266+
## 📄 License
267+
268+
MIT License - see the [LICENSE](LICENSE) file for details.
269+
270+
## 🙏 Acknowledgments
271+
272+
- Original [json-sql](https://github.com/2do2go/json-sql) library by 2do2go
273+
- MongoDB query syntax inspiration
274+
- Modern JavaScript community for best practices
275+
276+
## 📚 Documentation
277+
278+
For detailed documentation, examples, and API reference, see the [docs](./docs) directory:
279+
280+
- [MongoDB Operators Guide](./docs/MONGODB-OPERATORS.md)
281+
- [Multi-Dialect Support](./docs/README.md)
282+
- [GitHub Issues Fixed](./docs/GITHUB-ISSUES.md)
283+
- [Changelog](./docs/CHANGELOG.md)
284+
285+
---
286+
287+
**Made with ❤️ for the JavaScript community**

0 commit comments

Comments
 (0)