Tech News
← Back to articles

Show HN: FilterQL – A tiny query language for filtering structured data

read original related products more articles

FilterQL

A tiny query language for filtering structured data πŸš€

There are two main parts of this repository: the TypeScript library and the FilterQL language specification. Implementations in other languages are more than welcome!

TypeScript Library

Installation

bun add filterql # or: npm install filterql

Usage

Define a schema for your data and create a FilterQL instance:

import { FilterQL } from "filterql" // The schema determines what fields are allowed in the query const schema = { title : { type : "string" , alias : "t" } , year : { type : "number" , alias : "y" } , monitored : { type : "boolean" , alias : "m" } , rating : { type : "number" } , genre : { type : "string" } , } const filterql = new FilterQL ( { schema } ) const movies = [ { title : "The Matrix" , year : 1999 , monitored : true , rating : 8.7 , genre : "Action" } , { title : "Inception" , year : 2010 , monitored : true , rating : 8.8 , genre : "Thriller" } , { title : "The Dark Knight" , year : 2008 , monitored : false , rating : 9.0 , genre : "Action" } , ] // Filter movies by genre const actionMovies = filterql . filter ( movies , "genre == Action" ) // Field aliases and multiple comparisons const recentGoodMovies = filterql . filter ( movies , "y >= 2008 && rating >= 8.5" ) // Sort the filtered data by using the built-in SORT operation const recentGoodMovies = filterql . filter ( movies , "year >= 2008 | SORT rating desc" ) // Filter using boolean shorthand const monitoredMovies = filterql . filter ( movies , "monitored" )

A more realistic example

... continue reading