brianc / node-sql

SQL generation for node.js
MIT License
1.05k stars 191 forks source link

Is there a way to apply .order().descending to a postgres function, what is the syntax? #402

Closed calebcgates closed 6 years ago

calebcgates commented 6 years ago

I've without exaggeration spent the last few days digging through node-sql trying to answer this question.

What is the syntax to apply an ORDER BY .......... DESC

I would like to add DESC to the .order() found 4 lines below.

listing_processor.js

const functions = require('sql/lib/functions');
const listingListedDate = functions.getFunctions(`dash.listing_listed_date`);
query.order(listingListedDate(this._getTable()['data'])); //.descending?

Postgres Function dash.listing_listed_date SELECT (data #>> '{listedDate}')::date

query_processor.js

const {listing} = require('../models');
_getTable() {
    return listing;
}

models.js

function defineEntityTable(name){
  return sql.define({
    name: name,
    columns: ['entity_id', 'last_update_on', 'data']
  });
}
module.exports = {
listings: defineEntityTable('listings'),
};
calebcgates commented 6 years ago

I figured out the solution. It's pretty much what I expected.

return query.order(listingListedDate(this._getTable()[filterDataColumnName]).descending());