From c59c0f5e8fc46b83a03d3f356b06a57b536a5d8e Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 15 Sep 2018 14:04:43 -0600 Subject: [PATCH] default to using order by desc on created_at (if no order specified) --- lib/dbwrap.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/dbwrap.js b/lib/dbwrap.js index 38f4e29..059a5c2 100644 --- a/lib/dbwrap.js +++ b/lib/dbwrap.js @@ -147,6 +147,10 @@ function wrap(db, dir, dbsMap) { dir.indices.forEach(normalizeColumn); DB._indices = dir.indices; + DB._indicesMap = {}; + DB._indices.forEach(function (col) { + DB._indicesMap[col.name] = col; + }); function simpleParse(row) { if (!row) { @@ -251,12 +255,16 @@ function wrap(db, dir, dbsMap) { if (params) { if (params.orderBy) { - sql += " ORDER BY \"" + db.escape(snakeCase(params.orderBy) + "\" "); + sql += " ORDER BY \"" + db.escape(snakeCase(params.orderBy)) + "\" "; if (params.orderByDesc) { - sql += 'DESC '; + sql += "DESC "; } + } else if (DB._indicesMap.updated_at) { + sql += " ORDER BY \"updated_at\" DESC "; + } else if (DB._indicesMap.created_at) { + sql += " ORDER BY \"created_at\" DESC "; } - if (params.limit) { + if (isFinite(params.limit)) { sql += " LIMIT " + parseInt(params.limit, 10); } }