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

redite

v4.0.0

Published

A wrapper for Redis using proxies to emulate regular objects.

Downloads

11

Readme

redite

Maintainability Test Coverage Build Status
NPM Version Node Version

Redite is a Redis wrapper for Node.JS that uses proxies to emulate accessing regular objects, similar to Rebridge.

Differences to Rebridge

  • Uses native Redis data types where possible, instead of a single hash (e.g. lists for arrays, hashs for objects).
  • Get syntax looks more like accessing a regular object (in async/await at least).
  • No "synchronous" capabilities.
  • Allows access to internal objects such as the internal Redis connection.
  • Minimal dependencies (only relies on ioredis).
  • Automatically creates an object tree when setting.
  • Array methods which can mutate objects in-database.

Installation

npm install redite

Usage

const Redite = require("redite");
const db = new Redite(); // If not passed a Redis connection to use, it'll make its own.
// You can also pass a `url` parameter to the options object to connect using a Redis URL.

await db.users.ovyerus.set({
  id: "1",
  email: "[email protected]",
});
const me = await db.users.ovyerus;

console.log(me.id); // 1
console.log(me.email); // [email protected]

With a user-made Redis connection.

const Redis = require("ioredis");
const Redite = require("redite");

const client = new Redis();
const db = new Redite({ client });

await client.hset(
  "users",
  "ovyerus",
  JSON.stringify({
    id: "1",
    email: "[email protected]",
  })
);

const me = await db.users.ovyerus;

console.log(me.id); // 1
console.log(me.email); // [email protected]

API

class Redite (extends Proxy)

An interface for Redis created using ES6 Proxies.
The properties defined for this are not private and are only prefixed with an underscore (_) so as to not clash with things the user may want to set.

Properties

| Name | Type | Description | | --------------- | ------------- | ----------------------------------------------------------------- | | $redis | ioredis.Redis | The Redis connection that gets piggybacked by the wrapper. | | $serialise | Function | Function used to serialise data for Redis. | | $parse | Function | Function used to parse data from Redis. | | $deletedString | String | String used as a temporary placeholder when deleting list values. |

Constructor

new Redite([options])

| Name | Type | Default | Description | | ----------------------------- | ------------- | ------------------------ | ------------------------------------------------------------------------------------------------- | | options.client | ioredis.Redis | new Redis(options.url) | The Redis connection to piggyback off of. | | options.url | String | | The Redis URL to use for the automatically created connection. Not used if a client is passed. | | options.serialise | Function | JSON.stringify | Function that takes in a JS object and returns a string that can be sent to Redis. | | options.parse | Function | JSON.parse | Function that takes in a string and returns the JS object that it represents. | | options.deletedString | String | @__DELETED__@ | String to use as a temporary placeholder when deleting root indexes in a list. | | options.unref | Boolean | true | Whether to run .unref on the Redis client, which allows Node to exit if the connection is idle. | | options.ignoreUndefinedValues | Boolean | false | Whether to ignore undefined as a base value when setting values. |

Accessing Objects

To get an object using Redite, you just write it as if you were accessing a regular object. However, it has to end with .then or .catch due to promises (or you can use the shiny await syntax shown here). The object tree can be as long as you wish, however it should be an object that exists in Redis.

Example:

const db = new Redite();

// Using async/await
const result = await db.foo.bar.fizz.buzz;

// Using regular promises.
db.foo.bar.fizz.buzz.then((result) => {});

This also works for arrays:

const result = await db.foo[0].bar[1];

Setting Values

You can set values in the same fashion as getting them, however instead of returning a direct promise, it returns a function which must be passed the value to set. There is no need to worry about creating the objects before hand, as Redite will automatically generate one based on the keys given.

(Side note: any keys which imply an array is being accessed (numbers) will result in an array at that location instead of a regular object. If the number is not zero, there will be that amount of nulls before it)

Example:

await db.foo.bar.fizz.buzz.set("Magic");
const result = await Promise.all([db.foo.bar.fizz.buzz, db.foo.bar.fizz]);

console.log(result); // ["Magic", {buzz: "Magic"}];
await db.foo[0].bar[1].set('Magic');
const result = await Promise.all([[db.foo[0].bar[1], db.foo[0].bar]);

console.log(result) // ["Magic", [null, "Magic"]];

Other Methods

The library also has .has and .delete which work in the same fashion as .set, but check for existance or delete the object respectively. If a key is not given to these methods, they will be applied to the last key before them. There is also .exists which is an alias for .has, which makes more sense when not passing a key to it.

const db = new Redite();

await db.foo.bar.fizz.buzz.set("Hello world!");
const firstExists = await db.foo.has("bar");
console.log(firstExists); // true

await db.foo.bar.delete("fizz");
const secondExists = await db.foo.bar.exists();
console.log(secondExists); // false

.has and .delete are also the only methods that can be run on the main Redite object.

const db = new Redite();

await db.foo.set("Hello world!");
const firstExists = db.has("foo");
console.log(firstExists); // true

await db.delete("foo");
const secondExists = await db.has("foo");
console.log(secondExists); // falses

Working with Arrays

Redite has support for several methods that help when working with arrays.
Documentation for these is available here.

Licence

The code contained within this repository is licenced under the MIT License. See LICENSE for more information.