• Stars
    star
    117
  • Rank 290,972 (Top 6 %)
  • Language
    TypeScript
  • License
    Other
  • Created about 4 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Language Integrated Query for JavaScript

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.

More Repositories

1

typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
TypeScript
3,931
star
2

zero

A 3D renderer written in JavaScript and rendered to the terminal.
TypeScript
2,399
star
3

smoke

Turns a Web Browser into a Web Server with WebRTC
TypeScript
487
star
4

hammer

Build Tool for Browser and Node Applications
TypeScript
229
star
5

threadbox

Recursive Worker Threads in NodeJS
TypeScript
226
star
6

typescript-bundle

A Bundling Tool for TypeScript
TypeScript
124
star
7

typebox-codegen

Code Generation for TypeBox Types
TypeScript
67
star
8

blender-node

NodeJS binding to Blenders Python Scripting Environment
TypeScript
56
star
9

sidewinder

Type Safe Micro Services for Node
TypeScript
55
star
10

reactor

Asynchronous Event Driven IO for .NET
C#
44
star
11

typebox-workbench

Type Transform Tool for Runtime Type Systems
TypeScript
37
star
12

ts-8-bit

Using TypeScript's Type System to do 8-bit Arithmetic
TypeScript
36
star
13

tesseract

WebGL 2.0 GPGPU compute library for JavaScript.
TypeScript
32
star
14

fastify-typebox

Enhanced TypeBox support for Fastify
TypeScript
31
star
15

typescript.api

A typescript 0.9 compiler as a service api for nodejs.
TypeScript
27
star
16

black

A Software Rasterizer written in Rust
Rust
24
star
17

servicebox

Typed Web Services for NodeJS
TypeScript
22
star
18

esbuild-wasm-resolve

File Resolution for Esbuild running in the Browser
TypeScript
19
star
19

appex

develop nodejs web applications with typescript
TypeScript
15
star
20

drift

Run Chrome from the Terminal
TypeScript
15
star
21

fs-effects

A library for composing various file, folder, shell and watch operations in node.
TypeScript
10
star
22

smoke-task

Runs JavaScript functions from a terminal
TypeScript
9
star
23

neuron

Neural network implemented in JavaScript
TypeScript
9
star
24

corsa

Asynchronous uni-directional channels in node using async iteration.
TypeScript
9
star
25

smoke-rs

lightweight async task and stream library for Rust
Rust
8
star
26

stream-cortex

real-time live video streaming experiments with node + ffmpeg
TypeScript
7
star
27

magnum

general purpose template engine for nodejs.
TypeScript
7
star
28

runtime-type-benchmarks

High Performance Validation Benchmarks for JavaScript
TypeScript
6
star
29

vector-cs

.NET opengl graphics library
C#
6
star
30

tasksmith

Task automation library for node.
TypeScript
5
star
31

phantom-network-service

run phantomjs as a network service
TypeScript
5
star
32

neuron-render

An experiment using neural networks to approximate various stages of graphics pipeline for the purpose of creating interesting things.
TypeScript
5
star
33

neuron-gpgpu

GPGPU based implementation of a multi layer perceptron network for the browser.
TypeScript
5
star
34

fpv32

Benchmarks for fast 32-bit floating point vector math for JavaScript.
TypeScript
5
star
35

statebox

An observable JavaScript state container
TypeScript
4
star
36

hexagon

WebGL 2.0 graphics renderer written in TypeScript
TypeScript
4
star
37

smoke-run

Runs shell commands on file system watch events.
TypeScript
4
star
38

crimson-rust

CSP experiments in the rust programming language
Rust
4
star
39

fsweb

Static HTTP development server with live reload on save.
TypeScript
4
star
40

crimson

Actor system in JavaScript
TypeScript
3
star
41

merc

blender scene renderer demo
TypeScript
3
star
42

pubsub-rs

simple tcp based pubsub for rust
Rust
3
star
43

three-instanced-mesh

A reference project enabling geometry instancing for threejs materials
TypeScript
3
star
44

bayes

An implementation of a naive bayes classifier in TypeScript
JavaScript
2
star
45

signature

Overloaded function signatures in JavaScript.
TypeScript
2
star
46

smoke-web

A static file server that live reloads on file change.
TypeScript
2
star
47

smoke-hub-appengine

messaging hub for webrtc targeting the google app engine standard environment.
Go
2
star
48

fsrun

Restart OS processes on file system watch events.
JavaScript
2
star
49

smoke-pack

A npm project provisioning and build system for browser, electron, node and library projects.
TypeScript
1
star
50

vlc.web.stream

Example and documentation about streaming from VLC to a browser.
JavaScript
1
star
51

taxman

simple book keeping application for nodejs
JavaScript
1
star
52

neuron-function-approximation

An experiment using neural networks to approximate pure functions
TypeScript
1
star
53

nx-transform

angular + threejs + css experiment
HTML
1
star
54

deno-minifb

Render 32-bit RGBA Buffers to Desktop Windows
Rust
1
star
55

vector-rs

vector math library and utilities for Rust.
Rust
1
star
56

brainfuck-rs

A brainfuck interpreter implemented in Rust.
Rust
1
star
57

pang

A simple dependency injection library for node
TypeScript
1
star