UNPKG

redis-type

Version:
202 lines (201 loc) 7.79 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Hash = void 0; var base_1 = require("./base"); var json = __importStar(require("./json")); /** * Wrapper for HASHMAP Redis storage. * Mocks JavaScript Maps and has most of the Map's methods. * * @example * let human = new Hash(client, 'human', useJSON = true); * await human.set('alice', { isHuman: 'definitely' }); * await human.set('damir', { isHuman: 'could be' }); */ var Hash = /** @class */ (function (_super) { __extends(Hash, _super); function Hash() { return _super !== null && _super.apply(this, arguments) || this; } /** * Get the length of the hash (keys length) * * - Redis command: [HLEN] {@link https://redis.io/commands/hlen} * - JavaScript analogy: [Map.prototype.size] {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size} */ Hash.prototype.size = function () { return this.call("HLEN")(); }; /** * Get array of keys in the hash * * - Redis command: [HKEYS] {@link https://redis.io/commands/hkeys} * - JavaScript analogy: [Map.prototype.keys] {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys} */ Hash.prototype.keys = function () { return this.call("HKEYS")(); }; /** * Delete element in hash by it's key * * - Redis command: [HDEL] {@link https://redis.io/commands/hdel} * - JavaScript analogy: [Map.prototype.delete] {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete} * * @param {String} key Name of the key to delete */ Hash.prototype.delete = function (key) { return this.call("HDEL")(key); }; /** * Check whether key exists in hash * * - Redis command: [HEXISTS] {@link https://redis.io/commands/hexists} * - JavaScript analogy: [Map.prototype.has] {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has} * * @param {String} key Key to check */ Hash.prototype.has = function (key) { return this.call("HEXISTS")(key).then(function (e) { return !!e; }); }; /** * Set value under given key * * - Redis command: [HSET] {@link https://redis.io/commands/hset} * - JavaScript analogy: [Map.prototype.set] {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set} * * @param {String} key Key to set * @param {String} value Value to set under given key */ Hash.prototype.set = function (key, value) { return this.useJSON ? this.call("HSET")(key, json.toJSON(value)) : this.call("HSET")(key, value); }; /** * Get value by it's key * * - Redis command: [HGET] {@link https://redis.io/commands/hget} * - JavaScript analogy: [Map.prototype.get] {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get} * * @param {String} key Key to get from hash */ Hash.prototype.get = function (key) { return this.useJSON ? this.call("HGET")(key).then(json.parse) : this.call("HGET")(key); }; /** * Get array of values from the hash * * - Redis command: [HVALS] {@link https://redis.io/commands/hvals} * - JavaScript analogy: [Map.prototype.values] {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values} */ Hash.prototype.values = function () { return this.useJSON ? this.call("HVALS")().then(json.parseArray) : this.call("HVALS")(); }; /** * Returns 2-dimensional array of kind: * * ```JavaScript * [[key1, value1], [key2, value2]] * ``` * To support Object to Map transformation * * - Redis command: [HGETALL] {@link https://redis.io/commands/hgetall} * - JavaScript analogy: [Map.prototype.entries] {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries} * */ Hash.prototype.entries = function () { return this.getAll().then(function (obj) { if (obj === null) { return []; } return Object.keys(obj).map(function (key) { return [key, obj[key]]; }); }); }; /** * Get multiple keys by passing an array of keys * This command has no analogy in JS * * - Redis command: [HMGET] {@link https://redis.io/commands/hmget} * - JavaScript analogy: none * * @param {String[]} keys Keys to get values for */ Hash.prototype.getMul = function (keys) { return this.useJSON ? this.call("HMGET")(keys).then(json.parseArray) : this.call("HMGET")(keys); }; /** * Returns object representing hash structure * This command has no analogy in JS * * - Redis command: [HGETALL] {@link https://redis.io/commands/hgetall} * - JavaScript analogy: none */ Hash.prototype.getAll = function () { return this.useJSON ? this.call("HGETALL")().then(json.parseObjectValues) : this.call("HGETALL")(); }; /** * Set multiple keys by passing an object with key-value structure * This command has no analogy in JS * * - Redis command: [HMSET] {@link https://redis.io/commands/hmset} * - JavaScript analogy: none * * @param {Object} valuesObj Object with key-value pairs to set * @returns {Promise} */ Hash.prototype.setMul = function (valuesObj) { return this.useJSON ? this.call("HMSET")(json.stringifyObjectValues(valuesObj)) : this.call("HMSET")(valuesObj); }; return Hash; }(base_1.Base)); exports.Hash = Hash;