Count
Counts the number of keys in the database.
- Parameters:
pattern
: Optional SQL LIKE pattern to match keys. Defaults to "%" which matches all keys.
- Returns:
- The result of the operation, includes the number of keys in the database or an error if the operation failed.
// Get the total number of keys with result type handling
const result = db.count();
if (result.success) {
console.log(`Total keys: ${result.data}`);
} else {
console.log(result.error.message);
}
// Get the number of keys matching "user:%"
const userCount = db.count("user:%");
info
The count
method is faster than using keys().length
because it directly executes a SQL query optimized for counting rows. Use count
when you need to determine the number of keys, as it provides better performance compared to fetching all keys and measuring their length.