Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Redis wrapper for Node.JS, using ES6 Proxies & native Redis data types.

License

NotificationsYou must be signed in to change notification settings

Ovyerus/redite

Repository files navigation

MaintainabilityTest CoverageBuild Status
NPM VersionNode Version

Redite is aRedis wrapper for Node.JS that usesproxies toemulate accessing regular objects, similar toRebridge.

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

constRedite=require("redite");constdb=newRedite();// 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.awaitdb.users.ovyerus.set({id:"1",email:"Ovyerus@users.noreply.github.com",});constme=awaitdb.users.ovyerus;console.log(me.id);// 1console.log(me.email);// Ovyerus@users.noreply.github.com

With a user-made Redis connection.

constRedis=require("ioredis");constRedite=require("redite");constclient=newRedis();constdb=newRedite({ client});awaitclient.hset("users","ovyerus",JSON.stringify({id:"1",email:"Ovyerus@users.noreply.github.com",}));constme=awaitdb.users.ovyerus;console.log(me.id);// 1console.log(me.email);// Ovyerus@users.noreply.github.com

API

class Redite (extendsProxy)

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

NameTypeDescription
$redisioredis.RedisThe Redis connection that gets piggybacked by the wrapper.
$serialiseFunctionFunction used to serialise data for Redis.
$parseFunctionFunction used to parse data from Redis.
$deletedStringStringString used as a temporary placeholder when deleting list values.

Constructor

new Redite([options])

NameTypeDefaultDescription
options.clientioredis.Redisnew Redis(options.url)The Redis connection to piggyback off of.
options.urlStringThe Redis URL to use for the automatically created connection. Not used if a client is passed.
options.serialiseFunctionJSON.stringifyFunction that takes in a JS object and returns a string that can be sent to Redis.
options.parseFunctionJSON.parseFunction that takes in a string and returns the JS object that it represents.
options.deletedStringString@__DELETED__@String to use as a temporary placeholder when deleting root indexes in a list.
options.unrefBooleantrueWhether to run.unref on the Redis client, which allows Node to exit if the connection is idle.
options.ignoreUndefinedValuesBooleanfalseWhether to ignoreundefined 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 regularobject. However, it has to end with.then or.catch due to promises (or you canuse the shinyawait syntax shown here). The object tree can be as long as you wish,however it should be an object that exists in Redis.

Example:

constdb=newRedite();// Using async/awaitconstresult=awaitdb.foo.bar.fizz.buzz;// Using regular promises.db.foo.bar.fizz.buzz.then((result)=>{});

This also works for arrays:

constresult=awaitdb.foo[0].bar[1];

Setting Values

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

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

Example:

awaitdb.foo.bar.fizz.buzz.set("Magic");constresult=awaitPromise.all([db.foo.bar.fizz.buzz,db.foo.bar.fizz]);console.log(result);// ["Magic", {buzz: "Magic"}];
awaitdb.foo[0].bar[1].set('Magic');constresult=awaitPromise.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 tothese methods, they will be applied to the last key before them. There is also.existswhich is an alias for.has, which makes more sense when not passing a key to it.

constdb=newRedite();awaitdb.foo.bar.fizz.buzz.set("Hello world!");constfirstExists=awaitdb.foo.has("bar");console.log(firstExists);// trueawaitdb.foo.bar.delete("fizz");constsecondExists=awaitdb.foo.bar.exists();console.log(secondExists);// false

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

constdb=newRedite();awaitdb.foo.set("Hello world!");constfirstExists=db.has("foo");console.log(firstExists);// trueawaitdb.delete("foo");constsecondExists=awaitdb.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 availablehere.

Licence

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

About

A Redis wrapper for Node.JS, using ES6 Proxies & native Redis data types.

Topics

Resources

License

Stars

Watchers

Forks

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp