CSNW / sql-bricks

Transparent, Schemaless SQL Generation
http://csnw.github.io/sql-bricks
MIT License
204 stars 25 forks source link

Finish ORDER BY support: ASC, DESC & collations #73

Open prust opened 9 years ago

prust commented 9 years ago

My current thought (though I'm open on this) would be to support all the functionality via passing an array of objects to ORDER BY:

.orderBy({column: 'age', order: 'DESC'}, {column: 'name', collate: 'Latin1_General_CI_AS'})

And to add .asc(), .desc() and .collate() convenience functions which augment the column most recently passed to .orderBy():

.orderBy('age').desc().orderBy('name').collate('Latin1_General_CI_AS')

See #39 for more details.

Suor commented 8 years ago

If you want my 2 cents on it - trying to support everything with some structure is fools errand. Structure is only needed to be able to manipulate unfinished query. How many people really need to pass around partially ordered query to be able to add more order on it?

So if we accept that one doesn't need to refine order then we can just recommend to use sql() for hard cases:

query.orderBy(sql('age desc, name collate Latin1_General_CI_AS'))

For simple cases current behavior is almost sufficient. The only case is simple direction flip is absent. Proposed .asc() and .desc() could do, but often when we decide on user input some parameterized method is better:

// .asc() and .desc()
var query = query.orderBy('name')
args.asc ? query.asc() : query.desc()
// order direction as param, chains better
query.orderBy('name').orderDir(args.asc)
// or
query.orderBy('name', args.asc)