npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mjplabs/redis-async

v1.0.11

Published

A TypeScript-friendly, feature-complete, asynchronous Redis client for Node.JS

Downloads

4

Readme

redis-async

A feature-complete asynchronous client for Redis, with support for TypeScript

Installation

npm install @mjplabs/redis-async

Initializing a Redis client

Javascript

var redis = require('redis-async');
var client = redis.createAsyncClient();

TypeScript

import { createAsyncClient } from '@mjplabs/redis-async';
const client = createAsyncClient();

Note: createAsyncClient takes exactly the same arguments as createClient in the node_redis package.

Running Redis commands asynchronously

To run async commands, you will need to use either client.runSingle or client.runMultiple (unless you want to use a database helper method). Let's look at runMultiple first.

runMultiple takes a function as a parameter. This function is passed an empty node_redis.Multi object, and it must return another Multi object loaded with all of the commands you wish to run. runMultiple returns a Promise linked to an array of the Redis cache's response to each command in the chain.

Javascript and TypeScript

let response = await client.runMultiple(m => m
  .get("key1")
  .get("key2")
  .get("key3")
);
console.log(response);
// ----> ['value1', 'value2', 'value3']

Note: Refer to the node_redis documentation for a complete list of commands which may be applied to a Multi object.

runSingle works the exact same way, except it assumes that only one command is being run. This means that it knows to return a single string instead of an array of strings.

Javascript and TypeScript

await client.runSingle(m => m.set('key', 'value'));
response = await client.runSingle(m => m.get('key')));
console.log(response);
// ----> 'value'

Working with databases

To cut down on the clutter of selecting databases, redis-async provides helper methods runInDb and runMultipleInDb. All commands passed to either method are guaranteed to be run in the correct database. The database index is the first parameter of both methods.

Javascript and TypeScript

await client.runInDb(2, m => m.set('key', 'greetings from database 2'));
await client.runInDb(0, m => m.set('key', 'greetings from database 0'));

response = await client.runInDb(2, m => m.get('key'));
console.log(response);
// ----> 'greetings from database 2'

response = await client.runInDb(0, m => m.get('key'));
console.log(response)
// ----> 'greetings from database 0'
await client.runMultipleInDb(2, m => m
  .set("key1", "2_1")
  .set("key2", "2_2")
);

await client.runMultipleInDb(0, m => m
  .set("key1", "0_1")
  .set("key2", "0_2")
);

response = await client.runMultipleInDb(2, m => m
  .get("key1")
  .get("key2")
);
console.log(response);
// ----> ['2_1', '2_2']

response = await client.runMultipleInDb(0, m => m
  .get("key1")
  .get("key2")
);
console.log(response);
// ----> ['0_1', '0_2']

We also provide a function getAllKeys which returns a promise to a list of all keys in a given database.

Javascript and TypeScript

response = await client.getAllKeys(2);
console.log(response);
// ----> ['key1', 'key2']

Testing

npm test

Note on TypeScript support

This module has built-in typing for every possible Redis command. The specification of the actual Redis commands is handled by node_redis's existing Multi command-chaining system, meaning that the only thing we need to manually promisify and type is the multi.exec method. As a result, any command that works in node_redis will work perfectly in redis-async (even with strict type checks).