Skip to content

Instantly share code, notes, and snippets.

@brianc
Created March 16, 2015 17:03
Show Gist options
  • Save brianc/35e36fb4db6b645ed65f to your computer and use it in GitHub Desktop.
Save brianc/35e36fb4db6b645ed65f to your computer and use it in GitHub Desktop.
var moment = require('moment')
var pg = require('pg')
pg.connect(function(err, client, done) {
client.query('CREATE TEMP TABLE items(name text, created_at timestamptz)')
var insert = 'INSERT INTO items(name, created_at) VALUES($1, $2)'
client.query(insert, ['2012', new Date(2012, 01, 01)])
client.query(insert, ['2013', new Date(2013, 01, 01)])
client.query(insert, ['2014', new Date(2014, 01, 01)])
client.query(insert, ['2015', new Date(2015, 01, 01)])
client.query('SELECT * FROM items ORDER BY created_at', function(err, result) {
console.log('order by', result.rows)
})
client.query('SELECT * FROM items ORDER BY created_at DESC', function(err, result) {
console.log('order by DESC', result.rows)
done()
pg.end()
})
})
@brianc
Copy link
Author

brianc commented Mar 16, 2015

$ node order.js
order by [ { name: '2012',
    created_at: Wed Feb 01 2012 00:00:00 GMT-0500 (EST) },
  { name: '2013',
    created_at: Fri Feb 01 2013 00:00:00 GMT-0500 (EST) },
  { name: '2014',
    created_at: Sat Feb 01 2014 00:00:00 GMT-0500 (EST) },
  { name: '2015',
    created_at: Sun Feb 01 2015 00:00:00 GMT-0500 (EST) } ]
order by DESC [ { name: '2015',
    created_at: Sun Feb 01 2015 00:00:00 GMT-0500 (EST) },
  { name: '2014',
    created_at: Sat Feb 01 2014 00:00:00 GMT-0500 (EST) },
  { name: '2013',
    created_at: Fri Feb 01 2013 00:00:00 GMT-0500 (EST) },
  { name: '2012',
    created_at: Wed Feb 01 2012 00:00:00 GMT-0500 (EST) } ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment