Coder Social home page Coder Social logo

linqbox's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.