Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phanxgames/phanx-redis
Redis wrapper adding promise and typescript support for Node.js
https://github.com/phanxgames/phanx-redis
async nodejs promise redis typescript
Last synced: about 5 hours ago
JSON representation
Redis wrapper adding promise and typescript support for Node.js
- Host: GitHub
- URL: https://github.com/phanxgames/phanx-redis
- Owner: phanxgames
- License: mit
- Created: 2017-11-04T19:08:16.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-02-03T02:39:30.000Z (almost 5 years ago)
- Last Synced: 2023-12-24T20:15:52.134Z (11 months ago)
- Topics: async, nodejs, promise, redis, typescript
- Language: TypeScript
- Homepage:
- Size: 32.2 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
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.