7

Is there a way to run an SQL query against javascript objects to find data within them? Specifically I'm looking for a way to query a few objects (each representing a table of data) and perform a join on them.

I figured this was a long shot, but worth asking nonetheless.

Jonathan Beebe
  • 5,241
  • 3
  • 36
  • 42
  • Do you mean some interface on the browser that accepts SQL? – n8wrl Mar 16 '11 at 14:59
  • Please could you provide a more detailed description of your problem along with some sample JavaScript objects. – a'r Mar 16 '11 at 14:59
  • @n8wrl No, not an interface, it's more for the behind-the-scenes manipulation of data, so I can take in javascript objects (say from an ajax call) and perform advanced queries on them before displaying to the user. @a'r No sample js objects yet, I wanted to know if there was a way to query js objects before building a solution that relied on it. – Jonathan Beebe Mar 16 '11 at 15:54

5 Answers5

3

Try JSLinq

See this Example from the site:

var myList = [
        {FirstName:"Chris",LastName:"Pearson"},
        {FirstName:"Kate",LastName:"Johnson"},
        {FirstName:"Josh",LastName:"Sutherland"},
        {FirstName:"John",LastName:"Ronald"},
        {FirstName:"Steve",LastName:"Pinkerton"}
        ];

var exampleArray = JSLINQ(myList)
                   .Where(function(item){ return item.FirstName == "Chris"; })
                   .OrderBy(function(item) { return item.FirstName; })
                   .Select(function(item){ return item.FirstName; });
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • Never heard of this, but I like the looks of it. Doesn't appear to do joins though. – Jonathan Beebe Mar 16 '11 at 16:27
  • Well someone claims on their site he has developed something like this, no ide if its true but you can try to contact him. See folowing link: http://jslinq.codeplex.com/discussions/41842?ProjectName=jslinq – CloudyMarble Mar 17 '11 at 05:25
2

You can use async-linq which support both sync and async LINQ operations in JavaScript and feature on-par with C# version. You can get it by npm install async-linq.

Sync operation

linq([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
    .where(function (v) { return v % 2 === 1; })
    .select(function (v) { return v * 100; })
    .run();

Async operation

linq(['abc.txt', 'def.txt', 'xyz.txt'])
    .async
    .select(function (filename, index, callback) {
        fs.stat(filename, function (err, stat) {
            callback(err, err ? null : {
                filename: filename,
                size: stat.size
            });
        });
    })
    .run(function (err, result) {
        console.log(result);
    });
Compulim
  • 1,148
  • 10
  • 18
2

Never used it before, but a quick search on Google yields LINQ to Javascript. While it isn't SQL Syntax, it allows you to do SQL Like things on Javascript objects, or so it appears. You may also want to check out this question about LINQ and Javascript

Community
  • 1
  • 1
Kibbee
  • 65,369
  • 27
  • 142
  • 182
2

SQLike also is good to emulate SQL or linq syntax:

http://code.google.com/p/sqlike/

Achilleterzo
  • 742
  • 6
  • 16
1

There's TrimQuery - hasn't been updated for some time, but it's solid. http://code.google.com/p/trimpath/wiki/TrimQuery

badbamboo
  • 11
  • 1