I managed to use common table expressions (CTE) with knex.js and it was pretty easy.
Assuming you are using socket.io 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;
source share