smallorange-redis-client

1.0.25 • Public • Published

CircleCI

Redis client built with lodash and rxjs!

Compatible with just Node 7 and greater!!!!!!

Hi, this client is built on top of RxJS Observables (a reactive programming library http://reactivex.io/rxjs/), so, would be good to have a small portion of knowledege about that to enjoy all the features which this lib provide, specially the operators. Not all commands were implemented, just the enough to our needs, feel free to do more.

Properties

	client: RedisClient; // http://npmjs.com/package/redis
	onMessage: Observable<{channel: string, message: any}>
	multi: [tansaction block];

Methods

	constructor(options?: {
		retryStrategy: function // see https://www.npmjs.com/package/redis#options-object-properties,
		connection: {
			port: number,
			... // see https://www.npmjs.com/package/redis#options-object-properties,
		}
	});

	enableclients(): void; // enable client and subscribe clients
	flush(): void;
	close(): void;
	multiExec(multi: Redis.Multi): Observable<Array<number>>;
	set(key: string, value: string, expires: number = null, replace: boolean = true): Observable<string>;
	get(key: string): Observable<string>;
	keys(pattern: string || Array<string>): Observable<string>;
	ttl(key: string): Observable<number>;
	hashSet(key: string, field: string, value: string = '', expires: number = null, replace: boolean = true): Observable<number>;
	hashDel(key: string, field: string): Observable<number>;
	hashGet(key: string, field: string): Observable<string>;
	hashExists(key: string, field: string): Observable<boolean>;
	hashAll(key: string): Observable<{field: string, value: string}>;
	hashAllLike(key: string, fieldsLike: Array<string> | string): Observable<{field: string, value: string}>;
	hashKeys(key: string): Observable<string>;
	hashKeysLike(key: string, fieldsLike: Array<string> | string): Observable<Array<string>>;
	publish(channel: string, message: any, ignoreSameNode: boolean = true): void;
	onChannel(patternOrChannel: string, usePAttern: boolean = false): Observable<any>;
	sendCommand(command: string, ...args): Observable<string>;
	wrapObservable(clientMethod: function, ...args): Observable<any>;

Inexistent methods

To implement your own method is simple, follow this sample implementing lpush and lrange:

	import Redis from 'smallorange-redis-client';

	const redis = new Redis();

	function pushList(key, ...values) {
		const op = _.last(values) === true ? ::redis.client.lpush : ::redis.client.rpush;

		op(key, values);
	}

	function listRange(key, start = 0, stop = 0) {
		return redis.wrapObservable(redis.client.lrange, key, start, stop);
	}
	
	pushList('myList', 'a', 'b', 'c');

	listRange('myList', 0, 90)
		.toArray()
		.subscribe(response => {
			expect(response).to.deep.equal(['a', 'b', 'c']);
		}, null, done);

Sample

	import {
		Redis
	} from 'smallorange-redis-client';

	const redis = new Redis();

	for(let i=0; i<=100, i++){
		redis.hashSet('someKey', `field-${i}`, i);
	}

	redis.hashAll('someKey')
		.filter(response => response.value >= 50)
		.subscribe(console.log); // will print 50x from {field: 'field-50', value: 50} to {field: 'field-100', value: 100}

Readme

Keywords

none

Package Sidebar

Install

npm i smallorange-redis-client

Weekly Downloads

19

Version

1.0.25

License

ISC

Last publish

Collaborators

  • feliperohde