import { parseJSON, setOwnProperty } from "../util"; import type { CommandOptions } from "./command"; import { Command } from "./command"; export function deserialize>( fields: string[], result: (string | null)[] ): TData | null { if (result.every((field) => field === null)) { return null; } const obj: Record = {}; for (const [i, field] of fields.entries()) { try { setOwnProperty(obj, field, parseJSON(result[i]!)); } catch { setOwnProperty(obj, field, result[i]); } } return obj as TData; } /** * hmget returns an object of all requested fields from a hash * The field values are returned as an object like this: * ```ts * {[fieldName: string]: T | null} * ``` * * In case the hash does not exist or all fields are empty `null` is returned * * @see https://redis.io/commands/hmget * @see node_modules/@upstash/redis/docs/commands/hash/hmget.mdx * Responses are JSON-parsed automatically: a stored "123" comes back as the number 123, "true" as * true, and '{"a":1}' as an object. Pass automaticDeserialization: false to the Redis constructor * to receive raw strings. * With automaticDeserialization: false, hmget returns an array of values in field order instead, * such as ["123", null], with null for each field when the key is missing. */ export class HMGetCommand> extends Command< (string | null)[], TData | null > { constructor( [key, ...fields]: [key: string, ...fields: string[]], opts?: CommandOptions<(string | null)[], TData | null> ) { super(["hmget", key, ...fields], { deserialize: (result) => deserialize(fields, result), ...opts, }); } }