Home
| | | | | | | | | | | |

Introducing gq-to-sql: Graph-Style URL Filtering for SQL

6/18/2025

If you've ever worked with the Microsoft Graph API or OData, you're probably familiar with query strings like this: ?$select=name,age&$filter=age gt 20 and city eq 'Vienna'&$orderby=age desc&$top=10&$skip=5 This format is powerful—but not easily usable when working with SQL databases. That's why I built gq-to-sql: a JavaScript utility that converts Graph-style URL queries into safe, parameterized SQL statements. ## ✅ What It Does - Parses $filter, $select, $orderby, $top, and $skip - Supports nested filters ((age gt 18 or age lt 10) and city eq 'Vienna') - Outputs safe SQL using parameter placeholders (?) - Lightweight and framework-agnostic ## 🔧 Example Input: ?$select=name,age&$filter=age gt 21 and city eq 'Vienna'&$orderby=age desc&$top=10&$skip=5 Output: sql SELECT name, age FROM users WHERE age > ? AND city = ? ORDER BY age desc LIMIT 10 OFFSET 5 Params: [21, 'Vienna'] ## 📦 Get Started bash npm install gq-to-sql js import { buildSQL } from 'gq-to-sql'; const query = "$select=name,age&$filter=age gt 20 and name eq 'Mario'"; const { sql, params } = buildSQL(query, { table: 'users' }); ## 🌐 Use Cases - Building REST APIs with SQL backends - Internal tooling that mimics Graph behavior - Debugging complex query interfaces --- ### 🔗 Try it on npm ### 💻 View Source on GitHub Happy querying!