require explicit limit for find(null, { limit: <<int>> })

This commit is contained in:
AJ ONeal 2015-10-20 08:00:12 +00:00
parent da85732bbf
commit 5119cb1bf0
1 changed files with 21 additions and 9 deletions

View File

@ -100,24 +100,36 @@ function wrap(db, dir) {
}
DB.find = function (opts, params) {
var sql = 'SELECT * FROM ' + tablename + ' WHERE ';
var sql = 'SELECT * FROM \'' + tablename + '\' ';
var keys = opts && Object.keys(opts);
Object.keys(opts).forEach(function (key, i) {
if (opts && keys.length) {
sql += 'WHERE ';
keys.forEach(function (key, i) {
if (i !== 0) {
sql += 'AND ';
}
sql += db.escape(snakeCase(key)) + " = '" + db.escape(opts[key]) + "'";
});
}
else if (null !== opts || (params && !params.limit)) {
return PromiseA.reject(new Error("to find all you must explicitly specify find(null, { limit: <<int>> })"));
}
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 ';
}
}
if (params.limit) {
sql += " LIMIT " + parseInt(params.limit, 10);
}
}
console.log('sql', sql);
return db.allAsync(sql, []).then(simpleMap);
};