Monday, February 2nd, 2009

JSINQ: LINQ style access for JavaScript objects

Category: JavaScript


  1. from customer in $0
  2. group customer by customer.lastname into g
  3. select {lastname: g.getKey(), count: g.count()}
  4. into r
  5. orderby r.count descending
  6. select r

The code above should look similar to LINQ in .NET and runs with JSINQ a framework created by Kai Jäger.

With JSINQ, you can write SQL-like queries (or LINQ-like if you will) against arrays, DOM node lists or your own enumerable types. It’s a complete implementation of LINQ in that it has the complete set of LINQ extension methods and also comes with a query compiler that translates LINQ queries to JavaScript code.

Not only do you have a query language in JavaScript, but also a lot more. For example you can use JSINQ to create DOM nodes from JSON or XML you have received over XMLHttpRequest, you can build sortable tables with ease and you can write less code by doing things in a more declarative manner.

The playground lets you tinker around with queries in real time:

Posted by Dion Almaer at 7:09 am

3.8 rating from 18 votes


Comments feed TrackBack URI

I don’t understand whether this is supposed to be a client-only solution or what’s the server-side involvement.
If it’s client-side only than it looks like a copy of:


Comment by Rusco — February 2, 2009

JSINQ is indeed a client-only solution, although you might be able to use it with mod_v8 or ASP.NET.

It is not a clone of jLINQ for several reasons:
for one, it is API compatible with System.Linq.Enumerable which jLINQ is not (jLINQ for example lacks the selectMany, join and groupJoin operators which are really useful when writing more complex queries) and it has a query compiler that allows you to write queries using LINQ-style query expressions rather than by chaining together method calls (although that is what the queries are compiled into).

I’m not trying to belittle jLINQ (which I think is great), it’s just that JSINQ is catering to a different audience (those who already know LINQ).

Comment by HarryHunt — February 2, 2009

Why not just learn how to use JavaScript, instead of adding more dependencies for very leaky abstractions?

Comment by MorganRoderick — February 2, 2009

>>Why not just learn how to use JavaScript, instead of adding more dependencies for very leaky abstractions?

If you’re doing a lot of this stuff, code can be smaller and easier to read or debug.

Comment by Nosredna — February 2, 2009

Learning how to use JavaScript and using a DSL that allows for a more declarative programming style aren’t mutually exclusive. You might as well ask yourself: Why use XPath and not a bunch of nested loops?

JSINQ is an abstraction (I’ll give you that) and it is very leaky, but it’s not trying to be anything else. It’s not trying to create its own “SQL world”, instead it takes what JavaScript already knows how to do well (Lambdas, function composition, etc.) and exposes it through a slightly more convenient syntax. If you don’t see the need for a specialized DSL, you can get most of the benefits of JSINQ through its library part alone (jsinq-enumerable.js).

Comment by HarryHunt — February 2, 2009

You might as well ask yourself: Why use XPath and not a bunch of nested loops?
The syntax doesn’t necessarily have to be verbose:
var result = [i for each (i in list) if (i > 3)];

Comment by TNO — February 2, 2009

Well, array or list comprehensions are essentially a DSL in their own right. Their domain is the creation of arrays or lists.

I think the boundaries between syntactic sugar and DSLs are extremely blurry with the only real distinction being that one is built into the language and the other is bolted on-top of it. But if you take LINQ for example or generics in Java or lambda expressions in C#, you’ll realize that even that distinction doesn’t work too well (LINQ, Java generics and C# lambdas are dealt with by a preprocessor and not by the language compiler itself).

With LINQ in C# or VB.NET, the benefit of it not being a DSL in the traditional sense is that you get the compile-time type checking. But with JavaScript, that argument becomes almost negligible, because there typically isn’t a “compile-time” with JavaScript.

I’m not saying that JSINQ wouldn’t be more useful if it was built into the language itself, I’m just saying that I don’t see any severe disadvantages with it being a library.

Comment by HarryHunt — February 2, 2009

I wonder whether SQL is syntax you want for manipulating js data objects. Seems rather old fashioned. I’m sure it would be simple to create a jquery-ish syntax like $(dob).filter({lastname:’blah’,firstname:’blah’}).sort(‘ascending’)

or something more well thought out.

Comment by nataxia — February 2, 2009

Actually you can do that with JSINQ, except it is slightly more verbose since it involves anonymous functions:

people.where(function(p) { return p.lastname == ‘blah’ && p.firstname == ‘blah’; }).orderBy(function(p) { return p.lastname; })

You could do without the anonymous functions (see your example), but then you’d have to sacrifice a good amount of expressivenes (you can put anything in an anonymous function, but there’s only so much “logic” you can express with object literals, unless you want to write ASTs by hand).

With the LINQ query syntax, you can write

from p in people where p.lastname == ‘blah’ && p.firstname == ‘blah’ select p

which is not only shorter but also more readable (even than your already short and readable example).

I think the reason why LINQ (and XQuery for that matter) picked a SQL-like syntax is because that’s something people already know. It’s incredbly difficult to get developers to learn new things (you’d think otherwise, but unfortunately that’s not how it is) and so building on something people are already familiar with tends to be a good idea.

Comment by HarryHunt — February 2, 2009

>>I wonder whether SQL is syntax you want for manipulating js data objects.
A lot more people know SQL than jQuery.

Comment by Nosredna — February 2, 2009

Cant get it to work from the provided URL. I see a “welcome dialog” and the page event handlers are halted by some broken JS. Doesn’t work in IE, FF, or Opera.

I only get “An id, string or number was expected” JS error (translated from Danish).

Comment by MichaelSchoeler — February 2, 2009

I’m sorry to hear that. I have tested it in the three browsers you have mentioned and it works for me (without errors).

Are you by any chance using Ad-Block software or anything similar that might interfere with how the JavaScript is loaded?

If it continues to not work for you, you might want to go to and download it from there. (The playground is not currently included in the source or binary distribution, but other examples are).

Please let me know if you continue to have problems with JSINQ and I’ll upload the playground so you can run it from your local machine.

Comment by HarryHunt — February 2, 2009

Started playing with it, and I would have to agree it was very natural to use that “SQL mind” that seems to be built into most developers. I’m considering including this as a module in the framework I’ve built for my company.

Very well done, and I’m sad that your book is in german :)

Comment by nataxia — February 3, 2009

Thanks! I’m glad you found a use for it ;-)

As for my book: When I started writing it, there were already a bunch of great English books on the subject but only very few if none at all in German. I sometimes wish I had written it in English though…

Comment by HarryHunt — February 3, 2009

Leave a comment

You must be logged in to post a comment.