1.0.6 • Published 3 years ago

redis-simple-client v1.0.6

Weekly downloads
7
License
MIT
Repository
github
Last release
3 years ago

Package

Simple Redis

This package is an abstraction layer on redis package to make functions more easy and with async/await usage in NodeJS. It's completelly written in TypeScript. It lets you use it for the Dictionary and also for the pub/sub system.

Installation

npm install redis-simple-client

Usage

const RedisSimpleClient = require('redis-simple-client');
const redis = new RedisSimpleClient();

Usage for dictionary

async function test(){
  // You can set the URL for the connection, if the third argument is FALSE you will NOT use it for pub/sub.
  // The prefix helps you using the same redis client for different apps in the same database.
  // You will have prefix_variable, prefix_variable2, etc. in the redis store. 
  await redis.connect('redis://localhost:6379', 'prefix_', false);
  
  // Set and get variables.
  await redis.set('variable','value');
  const variable = redis.get('variable');
  console.log(variable); // outputs: 'value'
  
  // Set and get many variables.
  const variables = {
    var1: 'Testing 1',
    var2: 'Testing 2',
    var10: 'Testing 3',
  };
  await redis.setMany(variables);
  let results = redis.getMany([var1,var2]);
  console.log(results); // Outputs: {var1: 'Testing 1', var2: 'Testing 2'}
  
  results = redis.getManyWithPattern('var1*');
  console.log(results); // Outpus: {var1: 'Testing 1', var10: 'Testing 3'}
  
  // Deleting values
  await redis.del(['var1']);
  await redis.delWithPattern('var1*'); // Will delete var1 and var10.
  
  // Counting and getting keys with patterns.
  await redis.keys('*'); //  Output: ['var2']
  await redis.count('*'); // Output: 1.
  
  // Get how many functions you realized.
  console.log(redis.getCounts()); / Output {get: 2, set: 5, count: 3...}.

  // Close the connection.
  await redis.close();
}

Usage for pub/sub system

async function test(){
  // You will still be able to use dictionary functions, but with TRUE you will also be able to use pub/sub.
  await redis.connect('redis://localhost:6379', 'prefix_', true);
  
  await redis.subscribe('CHANNEL', (message) => console.log(message));
  await redis.publish('CHANNEL', 'Hello!'); // Console will log "Hello!".
  await redis.unsubsribe('CHANNEL');
  await redis.close();
}

All methods

MethodDescription
getRedisGets original Redis Client.
getCountsGets an object with how many times each function was executed.
setPrefixEstablishes the prefix for all variables in Redis.
isConnectedGets if redis is connected or not.
resetCountersSets all counters to zero.
connectConnects to the specified url and sets the prefix.
getGets the entry with the specified key.
getManyGets many entries with the specified keys.
getWithPatternGets all the entries that fit the specified pattern.
countCounts how many entries fit the specified pattern.
setSets the entry with the specified key
setManySets many entries at the same time.
incrByIncreases a variable by a specified number.
decrByDecreases a variable by a specified number.
incrExecutes incrBy with 1.
decrExecutes decrBy with 1.
delDeletes all the specified entries.
delWithPatternDeletes all the entries that fit the specified pattern.
emptyDeletes all entries.
publishSends a message to the specified channel.
subscribeListens to a specified channel.
unsubscribeStops listening to a specified channel.
closeCloses the connection.

License

MIT © Matías Puig

1.0.6

3 years ago

1.0.5

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago