Jinqu, based on the LINQ design, is the ultimate querying API for Javscript & Typescript. It provides:
- LINQ syntax
- Lazy (i.e. deferred) Evaluation
- Local and Remote Evaluation w/ provider model for libraries implementing remote evaluation (e.g.
jinqu-odata
) - Static Typing (in Typescript)
- Dynamic queries (in strings)
- Even works on IE 11 (with transpilation)
Jinqu works perfectly in both Javascript and Typescript, but is optimized for Typescript. Jinqu is itself written in Typescript.
npm install jinqu
First, make sure you've imported jinqu. You actually only need to this once, such as in the entry point for your application, to ensure the Array prototype extensions are set up.
import 'jinqu'
Now let's filter an array:
const array = [1,2,3,4,5]
const query = array.where(c => n % 2 == 0).toArray()
for (var n of query)
console.log (n) // outputs 2,4
We can chain query operators:
const array = [1,2,3,4,5]
const query = array
.where(c => n % 2 == 0)
.orderByDescending (n => n)
.toArray()
for (var n of query)
console.log (n) // outputs 4,2
Importantly, results aren't evaluated until toArray
is called.
const filtered = orders.where('c => c.id > value', { value: 3 })
The additional argument is a variable scope that lets you pass in variables dynamically.
The full range of LINQ operators are supported:
where
ofType
cast
select
selectMany
join
groupJoin
orderBy
orderByDescending
thenBy
thenByDescending
take
takeWhile
skip
skipWhile
groupBy
distinct
concat
zip
union
intersect
except
defaultIfEmpty
reverse
first
firstOrDefault
last
lastOrDefault
single
singleOrDefault
elementAt
elementAtOrDefault
contains
sequenceEqual
any
all
count
min
max
sum
average
aggregate
toArray
As well as:
range
repeat
Jinqu has the following remote providers:
- Jinqu-OData - Query OData endpoints
- Linquest - Query Remote LINQ endpoints
Remote queries always return promises so are awaited. So rather than toArray
to obtain the results of the query, you'll call toArrayAsync
:
...
const result = await remoteQuery.toArrayAsync()
for (var item of result)
...
Jinqu query operators build queries with a simple model: they take a query type as an input, transform it, and output another query type. However, the very first operator in a query needs to take an array type as the original source for the query. To make this work, jinqu extends the Array prototype.
Extending the array prototype is very convenient, but occasionally has conflict issues with Array's built-in prototype or other 3rd party prototype extensions.
To overcome this, you can call asQueryable
or the convenience q
for short, to ensure you're working with an IQuery<T>
type:
[1,2,3].asQueryable().reverse()
[1,2,3].q().reverse() // same as above
In addition, the concat
, join
, and reverse
methdos (which are built into the Array prototype), have special support: call concatWith
, joinWith
, and reverseTo
, to start a query with these operators on the array type.
jinqu is licensed with the MIT License.