Coder Social home page Coder Social logo

phanx-redis's Introduction

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.

phanx-redis's People

Contributors

phanxgames avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.