Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a JS library that supports writing linq to sql queries with nodejs?

I see jslinq, and see tds js libraries for using node and SQL together... So has anyone ever used those technologies together?

I want to be able to write linq to sql queries in a nodejs app...

like image 494
Daniel D Avatar asked Feb 01 '12 19:02

Daniel D


3 Answers

I've started with JS variant of LINQ to Entities this week. Check UniMapperJS

Example

var comments = await Comment.getAll()
.where(e => e.author.endsWith("something") || e.author.startsWith("somethingElse"))
.orderByDescending("created")
.limit(10)
.skip(0)
.exec();

But cuz of limitation of JS, it was hard to figure out, how to accept variables in where because that arrow functions are parsed as string. I've solved it by "virtual" variable ($) and list of args as last param.

var comments = await Comment.getAll()
.where(e => e.author.endsWith($) || e.author.startsWith($), filter.endsWith, filter.name)
.orderByDescending("created")
.limit(10)
.skip(0)
.select(e => e.author)
.exec();
like image 84
Hookyns Avatar answered Oct 01 '22 01:10

Hookyns


Today, JayData http://jaydata.org/ do it.

You can use the new ES6 Arrow Function syntax that looks very similar to C# (with a browser that supports it, like lastest Firefox, or a transpiler, like TypeScript or Traceur):

todoDB.Todos
    .filter(todo => todo.Completed == true)
    .map(todo => todo.Task )
    .forEach(taskName => $('#list')
    .append('Task: ' + taskName + ' completed'));

The query will be converted to a SQL Query (select Task from Todos where Completed = true) or a $filter URL parameter (http://.../?$filter=Completed%20eq%201&$select=Task), depending of the data source...

like image 33
lmcarreiro Avatar answered Oct 01 '22 02:10

lmcarreiro


You should checkout the edge.js framework, it connects node.js with .Net. One way would be to use the built in T-SQL support in edge, and then use something like linq.js to manipulate the results.

var getTop10Products = edge.func('sql', function () {/*
    select top 10 * from Products 
*/});

getTop10Product(null, function (error, products) {
    if (error) throw error;
    console.log(products);
});

Otherwise, you could setup an EF datacontext in a .Net library and call out to that using Linq

var getTop10Product = edge.func(function () {/*
    async (input) => { 
      using (var db = new ProductContext())
      {
        //Linq to EF query
        var query = from b in db.Products
                    orderby b.Name
                    select b;
        return query.Take(10).ToList();
      }
    }
*/});
like image 37
Neil Avatar answered Oct 01 '22 02:10

Neil