@sinclair/linqbox
TypeScript icon, indicating that this package has built-in type declarations

0.7.4 • Public • Published

LinqBox

Language Integrated Query for JavaScript

npm version GitHub CI

If it's possible in C#
using System.Linq;

var query = from n in new int [] { 0, 1, 2 } select n + 1;

Console.WriteLine(query.ToList());
Let it be so for JavaScript
import { linq } from '@sinclair/linqbox'

const query = linq `from n in [0, 1, 2] select n + 1`

console.log([...query])

Overview

LinqBox is an experimental implementation of Language Integrated Query for JavaScript. It is written as an abstraction for JavaScript generators where it allows sequences of generators to be functionally composed through LINQ query expression syntax.

LinqBox provides a sole Tagged Template function as its API. Within it, one can write a typical LINQ expression. LinqBox will parse it, build a syntax tree representation of it; and construct a series of function* generators to execute the query at a later point in time. The queryable object it returns is a Enumerable<T> which houses the parsed syntax tree and which implements a [Symbol.iterator]. The syntax tree itself can be reflected and potentially mapped to other domains such as SQL.

LinqBox was written as a research project to explore leveraging LINQ as a form of unified query syntax for JavaScript. It does require an ES6+ JavaScript runtime, should work ok on most modern browsers.

This project is offered as is to anyone who may find it of use.

License MIT

Contents

Install

$ npm install @sinclair/linqbox

Syntax

Linqbox implements a JavaScript version of LINQ as one would probably imagine it. Internally Linqbox parses for all JavaScript expressions (except functions) and constructs ESTree based expressions trees that are extended to support the standard set of LINQ clauses and keywords. The following is a brief example of its usage. For more comprehensive information on LINQ, refer to the official Microsoft documentation located here.

Example

import { linq } from '@sinclair/linqbox'

const users = [
  { userid: 0, name: 'dave' },
  { userid: 1, name: 'bob' },
  { userid: 2, name: 'alice' },
  { userid: 3, name: 'roger' },
]
const records = [
  { recordid: 0, userid: 0, data : 'toaster' },
  { recordid: 1, userid: 2, data : 'fridge' },
  { recordid: 2, userid: 1, data : 'television' },
  { recordid: 3, userid: 4, data : 'toaster' },
  { recordid: 4, userid: 2, data : 'stove' },
  { recordid: 5, userid: 0, data : 'couch' },
  { recordid: 6, userid: 2, data : 'computer' },
  { recordid: 7, userid: 2, data : 'washing machine' },
  { recordid: 8, userid: 3, data : 'remote control' },
  { recordid: 9, userid: 1, data : 'air conditioner' },
]

const query = linq `
  from user in ${users}
  join record in ${records}
    on user.userid equals record.userid 
      into records
  select {
    user,
    records
  }`

for(const value of query) {
  console.log(value)
}

Results in the following output

{
    user: { userid: 0, name: 'dave' },
    records: [
        { recordid: 0, userid: 0, data: 'toaster' },
        { recordid: 5, userid: 0, data: 'couch' }
    ]
}
{
    user: { userid: 1, name: 'bob' },
    records: [
        { recordid: 2, userid: 1, data: 'television' },
        { recordid: 9, userid: 1, data: 'air conditioner' }
    ]
}
{
    user: { userid: 2, name: 'alice' },
    records: [
        { recordid: 1, userid: 2, data: 'fridge' },
        { recordid: 4, userid: 2, data: 'stove' },
        { recordid: 6, userid: 2, data: 'computer' },
        { recordid: 7, userid: 2, data: 'washing machine' }
    ]
}
{
    user: { userid: 3, name: 'roger' },
    records: [
        { recordid: 8, userid: 3, data: 'remote control' }
    ]
}

Keywords

The following are the keywords supported by LinqBox. Most existing C# LINQ queries should trivially map to LinqBox with minimal changes. The following table lists them all with links to the official Microsoft documentation for additional information on how to use them. All are identical to the C# counterparts with the exception of let which has been renamed to const due to let having conflicting readonly semantics in C# that wouldn't make sense in JavaScript.

Clause Description
from Specifies a data source and a range variable (similar to an iteration variable).
where Filters source elements based on one or more boolean expressions separated by logical AND and OR operators or for truthyness.
select Specifies the type and shape that the elements in the returned sequence will have when the query is executed.
group Groups query results according to a specified key value.
into Provides an identifier that can serve as a reference to the results of a join, group or select clause.
orderby Sorts query results in ascending or descending order based on the default comparer for the element type.
join Joins two data sources based on an equality comparison between two specified matching criteria.
const Same as let. Introduces a range variable to store sub-expression results in a query expression.
in Contextual keyword in a join clause.
on Contextual keyword in a join clause.
equals Contextual keyword in a join clause.
by Contextual keyword in a group clause.
ascending Contextual keyword in a orderby clause.
descending Contextual keyword in a orderby clause.

Package Sidebar

Install

npm i @sinclair/linqbox

Weekly Downloads

6

Version

0.7.4

License

MIT

Unpacked Size

122 kB

Total Files

13

Last publish

Collaborators

  • sinclair