Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node-based SQL builder with Common Table Expression (WITH clause) support

I'm building a Node.js app that needs to query a Redshift database (based on postgres 8.0.2) using CTEs. Unfortunately, the SQL query builders I've looked at thus far (node-sql, knex.js and sequelize) don't seem to support common table expressions (CTEs).

I had great success forming common table expressions in Ruby using Jeremy Evans' Sequel gem, which has a with method that takes two arguments to define the tables' aliased name and the dataset reference. I'd like something similar in Node.

Have I missed any obvious contenders for Node.js SQL query builders? From what I can tell these are the four most obvious:

  • node-sql
  • nodesql (no postgres support?)
  • knex.js
  • sequelize
like image 823
felguerez Avatar asked Jan 07 '15 23:01

felguerez


People also ask

What is common table expression and which version of SQL supports the common table expression?

A CTE (Common Table Expression) is a temporary result set that you can reference within another SELECT, INSERT, UPDATE, or DELETE statement. They were introduced in SQL Server version 2005. They are SQL-compliant and part of the ANSI SQL 99 specification. A SQL CTE always returns a result set.

How do you use common table expressions in SQL?

The CTE starts with the WITH keyword, after which you specify the name of your CTE, then the content of the query in parentheses. The main query comes after the closing parenthesis and refers to the CTE. Here, the main query (also known as the outer query) is SELECT a,c FROM my_cte WHERE … .

Does Hive support CTE?

The CTE is defined only within the execution scope of a single statement. One or more CTEs can be used in a Hive SELECT, INSERT, CREATE TABLE AS SELECT, or CREATE VIEW AS SELECT statement.

What is CTE in SQL with example?

CTE was introduced in SQL Server 2005, the common table expression (CTE) is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. You can also use a CTE in a CREATE a view, as part of the view's SELECT query.


2 Answers

knex.js now supports WITH clauses:

knex.with('with_alias', (qb) => {
  qb.select('*').from('books').where('author', 'Test')
}).select('*').from('with_alias')

Outputs:

with "with_alias" as (select * from "books" where "author" = 'Test') select * from "with_alias"
like image 135
Steve Sanders Avatar answered Oct 31 '22 21:10

Steve Sanders


I was able to use common table expressions (CTEs) with knex.js and it was pretty easy.

Assuming you're using socket.io along with knex.js,

knex-example.js:

function knexExample (io, knex) {
  io.on('connection', function (socket) {
    var this_cte = knex('this_table').select('this_column');

    var that_cte = knex('that_table').select('that_column');

    knex.raw('with t1 as (' + this_cte + 
    '), t2 as (' + that_cte + ')' + 
    knex.select(['this', 'that'])
      .from(['t1', 't2'])
    )
    .then(function (rows) {
      socket.emit('this_that:update', rows);
    });
  })
}

module.exports = knexExample;
like image 3
felguerez Avatar answered Oct 31 '22 22:10

felguerez