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); } }