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

node-redis-objects

v1.0.7

Published

Redis Storage Interface for Javascript Objects

Downloads

24

Readme

node-redis-objects

Node Redis Objects - Storage Interface for Javascript Objects

Overview

Easy to use package for storing objects in Redis, including preservation of complex data structures and original data types. Includes an optional object cache abstraction layer that saves data changes in real-time to Redis.

Features

  • Easily store and retrieve objects from Redis.
  • Handles complex structures and data types.
  • Method to ensure changes are saved in execution sequence.
  • Compatible with on-change package. (see examples)

How Data Is Stored

Objects are automatically "flattened" via ":" separators so they can be stored in Redis and "unflattened" when retrieved.

{
  "foo": true,
  "a": {
    "b": [
      {
        "c": true
      }
    ]
  }
}

Is stored in Redis as:

object:foo = true
object:foo.meta = {"type":"boolean"}
object:a:b:0:c = true
object:a:b:0:c.meta = {"type":"boolean"}

Getting Started

Installation

Using NPM:

$ npm install node-redis-objects

Basic Usage

// Start by importing redis-objects.
const RedisObjects = require("node-redis-objects");
// or:
//import RedisObjects from "node-redis-objects";

// Create new RedisObjects interface (config optional)
const redisObjects = new RedisObjects();

// Save an object
await redisObjects.put("test", {
  a: "a",
});

// Get an object
const test = await redisObjects.get("test");

Config

const config = {
  redis: false, // Supply already-created ioRedis connection (spawns its own if absent)
  ioRedisOptions: false, // ioRedis connection options (optional, connects to localhost:6379 if absent)
  storagePath: false, // Optional: root redis path, ie "cache"
  allowFunctions: false, // Optional: 'true' allows objects containing functions to be stored (WARNING: Vulnerable to code injection if your Redis store is not secure.)
  heartbeat: 1000, // Optional value: interval (in ms)
};

const redisObjects = new RedisObjects(config);

Commands

put( objectName, objectValue, ttl )

Writes an object to Redis (Promise, use await or standard Promise chains)

await redisObjects.put("someObject", {
  key1: "1234",
  key2: {
    a: "key2 a string",
    b: false,
  },
});

Stores

someObject:key2:a = key2 a string
someObject:key2:b = false
someObject:key1 = 1234
someObject:key2:b.meta = {"type":"boolean"}

get( objectName )

Gets an object from Redis (Promise, use await or standard Promise chains)

await redisObjects.get("someObject");

Returns

{
  "key1": "1234",
  "key2": {
    "a": "key2 a string",
    "b": false
  }
}

update ( { name, path, value, oldValue, ttl } )

Save (or delete) object values. (Structurally compatible with on-change package.) (Promise, use await or standard Promise chains)

Note: When using the on-change package, see the 'queueUpdate' method below.

await redisObjects.update({
  name: `someObject`, // Object name
  path: `key2.b`,
  value: "9876",
  oldValue: false, // Optional, supplied by on-change. (oldValue=value is skipped)
  ttl: false, // Optional TTL (in seconds)
});

Stores

someObject:key2:b = 9876

queueUpdate ( { name, path, value, oldValue, ttl } )

Queue a request to save (or delete) object values. (Structurally compatible with on-change package.) (Promise, use await or standard Promise chains)

The update queue is utilized to ensure changes are processed in the order received.

await redisObjects.queueUpdate({
  name: `someObject`, // Object name
  path: `key2.b`,
  value: "9876",
  oldValue: false, // Optional, supplied by on-change. (oldValue=value is skipped)
  ttl: false, // Optional TTL (in seconds)
});

Stores

someObject:key2:b = 9876

close()

Closes Redis connection.

redisObjects.close();

call()

Call an arbitrary ioredis command. (See ioredis/redis for commands documentation.)

const redisDbSize = await redisObjects.call("dbsize");

ping()

Redis PING. Returns PONG.

await redisObjects.ping();

getLastHeartbeat()

Returns timestamp from last RedisObjects heartbeat. (Useful for testing age of data is using in a caching application.)

const lastHeartbeat = await redisObjects.getLastHeartbeat();
const downMs = lastHeartbeat > 0 ? Math.floor(+Date.now() - lastHeartbeat) : 0;
const cacheDownSecs = downMs > 0 ? Math.ceil(downMs / 1000) : 0;

startHeartBeat( interval )

Starts a loop with the optional interval (default 1000ms), updating the "redisObjectsHeartbeat" with the current timestamp. This happens automatically if the config parameter heartbeat is set.

redisObjects.startHeartBeat();

writeHeartBeat()

Writes the current timestamp to "redisObjectsHeartbeat".

redisObjects.writeHeartBeat();

flushall()

Flushes (erases) entire Redis database.

await redisObjects.flushall();

Events

Events are passthrough from ioredis.

| Event | Description | | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | connect | emits when a connection is established to the Redis server. | | ready | If enableReadyCheck is true, client will emit ready when the server reports that it is ready to receive commands (e.g. finish loading data from disk).Otherwise, ready will be emitted immediately right after the connect event. | | error | emits when an error occurs while connecting.However, ioredis emits all error events silently (only emits when there's at least one listener) so that your application won't crash if you're not listening to the error event. | | close | emits when an established Redis server connection has closed. | | reconnecting | emits after close when a reconnection will be made. The argument of the event is the time (in ms) before reconnecting. | | end | emits after close when no more reconnections will be made, or the connection is failed to establish. | | wait | emits when lazyConnect is set and will wait for the first command to be called before connecting. |

(Refer to ioredis documentation for more information on these events.)