phanx-redis
TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

Redis wrapper adding promise and typescript support.

Features

  • Provided Typescript source code.
  • Exposes all REDIS commands and wraps them with promises.
  • Provides two helper methods for searching and looping through values: getSearch and delSearch.

install

npm install phanx-redis

Requirements

  • ECMAScript 2016 (ES6)
  • Node.JS 6.x or later (tested on 6.11)

Basic Example


let PhanxRedis = require("phanx-redis");

let config = {
	host: "127.0.0.1"
};

let rs = new PhanxRedis(config);

async function run() {
    await rs.set("key","test");
    console.log("set key!");

    //get key value and return to value after async operation completes
    let keyvalue = await rs.get("key");
    console.log("key=",keyvalue);

}
run();

Helper Methods exclusive to this module:

getDefault(key,defaultValue)

Just like the standard .get(key) method, but instead will allow you to provide a default value if the key is not found or is null.

let value = await rs.getDefault("undefined key","not found");
console.log(value); //outputs "not found"

getJson(key)

Will attempt to automatically parse the JSON and return the object.

await rs.set("an object",JSON.stringify({foo:"bar"}));

let obj = await rs.getJson("an object");
console.log(obj.foo); //outputs: "bar"

setJson(key, object)

Will attempt to convert passed in object to JSON and save to key.

await rs.setJson("key",{foo:"bar"});

// .. later ..

let obj = await rs.getJson("key");
console.log(obj.foo); //outputs: "bar"

getSearch(key, callback(key,value,cbnext) )

This method allows you to loop over key/values that match your search criteria. Internally it uses the SCAN redis command.

	//lets add a whole bunch of keys
	await rs.set("chara_a",1);
	await rs.set("chara_b",2);
	await rs.set("chara_c",3);
	await rs.set("chara_d",4);
	await rs.set("chara_e",5);
	await rs.set("chara_f",6);

	await rs.getSearch("chara_*",function(key,value,cbNext) {
		console.log("   ",key,value);
		cbNext();
	});

	console.log("search complete");

Alternatively, you can return the search result in a Dictionary (dictionaryjs) collection and use all the great dictionaryjs methods to iterate through it.

    let rows = await rs.getSearch("chara_*");
    console.log(result);

    for (let row of rows) {
        console.log(row);
    }

delSearch(key)

This method is similar to the getSearch method, but instead of allowing you to loop through the results, it deletes them, and then returns the delete count.

	let delcount = await rs.delSearch("chara_*");
	console.log("deleted " + delcount + " keys");

multi() Functionality

Multi allows commands to be queued together, and ensures that all commands succeed or all fail.

.mutli() chaining functionality is not supported by this module. You may access its functionality by not chaining it. Such as:

	let multi = rs.multi();

	multi.dbsize();
	multi.set("test","blah");
	multi.set("phanx","games");
	multi.dbsize();

    let results = await multi.exec();
    console.log(results);

Error Handling

You can disable errors being thrown and handle errors by checking if an error existed in the previous operation.


rs.throwErrors = false;

await rs.get("key");

if (rs.error) {
    //handle error
} else {
    //do something with the last operation's result
    let value = rs.result;
}

Otherwise, by default throwErrors is true, and thus you will need to handle errors using try/catch.

Based on node_redis

This module wraps the node_redis module. Please review this module for more information about what methods and commands are available to you.

Readme

Keywords

none

Package Sidebar

Install

npm i phanx-redis

Weekly Downloads

0

Version

0.1.6

License

GPL-3.0

Unpacked Size

97.4 kB

Total Files

10

Last publish

Collaborators

  • thephantomx