All files / src/resp/command/hash hgetall-command.ts

91.3% Statements 21/23
50% Branches 1/2
100% Functions 2/2
91.3% Lines 21/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 651x   1x     1x 1x                                                 1x 4x   4x   4x   4x   4x     3x       3x 3x 3x 3x 3x       3x 3x 9x 9x     3x      
import { Logger } from "../../../logger";
import { IRequest } from "../../../server/request";
import { DataType } from "../../data/data-type";
import { Database } from "../../data/database";
import { DatabaseValue } from "../../data/database-value";
import { RedisToken } from "../../protocol/redis-token";
import { IRespCommand } from "../resp-command";
 
/**
 * ### Available since 2.0.0.
 * ### HGETALL key
 * Returns all fields and values of the hash stored at key. In the returned value, every
 * field name is followed by its value, so the length of the reply is twice the size of the
 * hash.
 * ### Return value
 * Array reply: list of fields and their values stored in the hash, or an empty list when key
 * does not exist.
 * ### Examples
 * ```
 * redis> HSET myhash field1 "Hello"
 * (integer) 1
 * redis> HSET myhash field2 "World"
 * (integer) 1
 * redis> HGETALL myhash
 * 1) "field1"
 * 2) "Hello"
 * 3) "field2"
 * 4) "World"
 * redis>
 * ```
 */
export class HgetallCommand extends IRespCommand {
    public DbDataType = DataType.HASH
 
    public maxParams = 1
 
    public minParams = 1
 
    public name = "hgetall"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const key: string = request.getParam(0);
        this.logger.debug(`Getting HASH ${key}`);
        const item: DatabaseValue = db.get(key),
            results: RedisToken[] = [];
        Iif (!item) {
            this.logger.debug(`HASH ${key} not found`);
            results.push(RedisToken.nullString());
        } else {
            const hash = item.getHash();
            for (const field of Object.keys(hash)) {
                results.push(RedisToken.string(field));
                results.push(RedisToken.string(hash[field]));
            }
        }
        return RedisToken.array(results);
    }
}