Awesome Open Source
Awesome Open Source

Functional Redis

A simple Redis client in tune with Functional Programming principles in JavaScript for Deno.

deno land deno version GitHub release GitHub licence

Redis Request

The RedisRequest represents a Redis request. It has three attributes: the first is the Redis command, the second is a typed array named "raw", the last is an array of arguments. The RedisRequest type is mostly interoperable with RedisResponse, Resource, File, (HTTP) Request
and (HTTP) Response.

The RedisRequest type implements the following algebras:

  • [x] Group
  • [x] Comonad
  • [x] Monad

Example

import RedisRequest from "https://deno.land/x/[email protected]/library/RedisRequest.js";

const redisRequest = RedisRequest("GET", new Uint8Array([]), [ "hoge" ]);

assert(RedisRequest.is(redisRequest));

A Symbol named rawPlaceholder may be used as a placeholder for the buffer. In the following example, the request will resolve to: SET hoge piyo.

import { encodeText } from "https://deno.land/x/[email protected]/library/utilities.js";
import RedisRequest from "https://deno.land/x/[email protected]/library/RedisRequest.js";
import { $$rawPlaceholder } from "https://deno.land/x/[email protected]/library/Symbol.js";

const redisRequest = RedisRequest("SET", encodeText("piyo"), [ "hoge", $$rawPlaceholder ]);

assert(RedisRequest.is(redisRequest));

The placeholder can be used multiple times if the buffer has multiple values separated by CLRF (\r\n).

import { encodeText } from "https://deno.land/x/[email protected]/library/utilities.js";
import RedisRequest from "https://deno.land/x/[email protected]/library/RedisRequest.js";
import { $$rawPlaceholder } from "https://deno.land/x/[email protected]/library/Symbol.js";

const redisRequest = RedisRequest(
  "MSET",
  encodeText("piyo\r\nfuga"),
  [ "hoge", $$rawPlaceholder, "hogefuga", $$rawPlaceholder ]
);

assert(RedisRequest.is(redisRequest));

Utilities

The RedisRequest namespace comes with methods for convenience to create an instance of RedisRequest with various commands. The methods are curried.

String commands

RedisRequest.append 📕

const redisRequest = RedisRequest.append("hoge", "piyo");

RedisRequest.bitcount 📕

const redisRequest = RedisRequest.bitcount("hoge", [ 0, 1 ]);

RedisRequest.bitfield 📕

const redisRequest = RedisRequest.bitfield("hoge", [ "GET", "i8", 100 ]);

RedisRequest.bitop 📕

const redisRequest = RedisRequest.bitop("AND", "hoge", [ "piyo", "fuga" ]);

RedisRequest.bitpos 📕

const redisRequest = RedisRequest.bitpos("hoge", [ 0, 1 ]);

RedisRequest.decr 📕

const redisRequest = RedisRequest.decr("hoge");

RedisRequest.decrby 📕

const redisRequest = RedisRequest.decrby("hoge", 3);

RedisRequest.get 📕

const redisRequest = RedisRequest.get("hoge");

RedisRequest.getbit 📕

const redisRequest = RedisRequest.getbit("hoge", 3);

RedisRequest.getrange 📕

const redisRequest = RedisRequest.getrange("hoge", [ 0, 1 ]);

RedisRequest.getset 📕

const redisRequestA = RedisRequest.getset("hoge", "piyo");
const redisRequestB = RedisRequest.getset("hoge", encodeText("piyo"));

RedisRequest.incr 📕

const redisRequest = RedisRequest.incr("hoge");

RedisRequest.incrby 📕

const redisRequest = RedisRequest.incrby("hoge", 3);

RedisRequest.incrbyfloat 📕

const redisRequest = RedisRequest.incrbyfloat("hoge", 0.1);

RedisRequest.mget 📕

const redisRequest = RedisRequest.mget("hoge", "piyo");

RedisRequest.mset 📕

const redisRequestA = RedisRequest.mset("hoge", "piyo", "hogefuga", "fuga");
const redisRequestB = RedisRequest.mset(
  [ "hoge", $$rawPlaceholder, "hogefuga", $$rawPlaceholder ],
  encodeText("piyo\r\nfuga\r\n")
);

RedisRequest.msetnx 📕

const redisRequestA = RedisRequest.msetnx("hoge", "piyo", "hogefuga", "fuga");
const redisRequestB = RedisRequest.msetnx(
  [ "hoge", $$rawPlaceholder, "hogefuga", $$rawPlaceholder ],
  encodeText("piyo\r\nfuga\r\n")
);

RedisRequest.psetex 📕

const redisRequestA = RedisRequest.psetex(1000, "hoge", "piyo");
const redisRequestB = RedisRequest.psetex(1000, "hoge", encodeText("piyo"));

RedisRequest.set 📕

const redisRequestA = RedisRequest.set({}, "hoge", "piyo");
const redisRequestB = RedisRequest.set({}, "hoge", encodeText("piyo"));
const redisRequestC = RedisRequest.set({ EX: 2000 }, "hoge", encodeText("piyo"));
const redisRequestD = RedisRequest.set({ KEEPTTL: true }, "hoge", encodeText("piyo"));

RedisRequest.setbit 📕

const redisRequest = RedisRequest.setbit("hoge", 7, 1);

RedisRequest.setex 📕

const redisRequestA = RedisRequest.setex(10, "hoge", "piyo");
const redisRequestB = RedisRequest.setex(10, "hoge", encodeText("piyo"));

RedisRequest.setnx 📕

const redisRequestA = RedisRequest.setnx("hoge", "piyo");
const redisRequestB = RedisRequest.setnx("hoge", encodeText("piyo"));

RedisRequest.setrange 📕

const redisRequest = RedisRequest.setrange("hoge", 2, "FU");

RedisRequest.stralgo 📕

const redisRequest = RedisRequest.strlen("LCS", "KEYS, "hoge", "piyo");

RedisRequest.strlen 📕

const redisRequest = RedisRequest.strlen("hoge");

Redis Response

The RedisResponse represents a Redis response. It has only one argument, a typed array named "raw". The RedisResponse type is mostly interoperable with RedisRequest, Resource, File, (HTTP) Request and (HTTP) Response.

The RedisResponse type implements the following algebras:

  • [x] Alternative
  • [x] Group
  • [x] Comonad
  • [x] Monad

Example

import RedisResponse from "https://deno.land/x/[email protected]/library/RedisResponse.js";

const redisResponse = RedisResponse.Success(new Uint8Array([]));

assert(RedisResponse.is(redisResponse));


Alternative Project Comparisons
Related Awesome Lists
Top Programming Languages
Top Projects

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,058,747
Redis (23,836
Functional Programming (10,963
Algebra (2,340
Monad (1,928
Deno (1,555
Redis Client (652
Functor (546
Monoid (244
Denoland (130