require explicit limit for find(null, { limit: <<int>> })
This commit is contained in:
		
							parent
							
								
									da85732bbf
								
							
						
					
					
						commit
						5119cb1bf0
					
				@ -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 (i !== 0) {
 | 
			
		||||
          sql += 'AND ';
 | 
			
		||||
        }
 | 
			
		||||
        sql += db.escape(snakeCase(key)) + " = '" + db.escape(opts[key]) + "'";
 | 
			
		||||
      });
 | 
			
		||||
      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);
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user