Skip to content

Commit

Permalink
Merge pull request #43 from Aclify/promisify-redis
Browse files Browse the repository at this point in the history
Promisify redis methods
  • Loading branch information
rimiti authored Apr 30, 2019
2 parents 511da38 + d787c85 commit f82b932
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"homepage": "https://github.com/aclify/aclify#readme",
"dependencies": {
"bluebird": "^3.5.4",
"lodash": "^4.17.11"
},
"devDependencies": {
Expand All @@ -29,7 +30,6 @@
"@types/lodash": "^4.14.123",
"@types/node": "^11.13.8",
"@types/redis": "^2.8.12",
"bluebird": "^3.5.4",
"jest": "^24.7.1",
"mongodb": "^3.2.3",
"node-mocks-http": "^1.7.3",
Expand Down
20 changes: 10 additions & 10 deletions src/stores/redis.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { isFunction } from 'lodash';
import * as Bluebird from 'bluebird';
import { Multi, RedisClient } from 'redis';
import { IStore } from '..';
import { Common } from '../classes/common';
import { IBucket } from '../types';
import { IBucket, IRedisClientAsync } from '../types';

/**
* {@inheritDoc}
* @description Redis store.
*/
export class RedisStore extends Common implements IStore {
public buckets: {};
private readonly redis: RedisClient;
private readonly redis: IRedisClientAsync;
private transaction: Multi;

/**
Expand All @@ -20,13 +20,13 @@ export class RedisStore extends Common implements IStore {
*/
constructor(redis: RedisClient, prefix?: string) {
super();

// @ts-ignore
if (!isFunction(redis.keysAsync) || !isFunction(redis.delAsync) || !isFunction(redis.smembersAsync) || !isFunction(redis.sunionAsync)) {
throw new Error('Redis client must be promisified.');
}

this.redis = redis;
this.redis = redis as IRedisClientAsync;
this.redis.endAsync = Bluebird.promisify(this.redis.end);
this.redis.getAsync = Bluebird.promisify(this.redis.get);
this.redis.sunionAsync = Bluebird.promisify(this.redis.sunion);
this.redis.keysAsync = Bluebird.promisify(this.redis.keys);
this.redis.delAsync = Bluebird.promisify(this.redis.del);
this.redis.smembersAsync = Bluebird.promisify(this.redis.smembers);
this.prefix = prefix !== undefined ? prefix : 'acl';
}

Expand Down
19 changes: 15 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { RedisClient } from 'redis';

export interface IOptions {
buckets: {
meta: string;
Expand All @@ -9,6 +11,19 @@ export interface IOptions {
};
}

export interface IDynamicObject {
[key: string]: any // tslint:disable-line no-any
}

export interface IRedisClientAsync extends RedisClient {
endAsync: any; // tslint:disable-line no-any
getAsync: any; // tslint:disable-line no-any
sunionAsync: any; // tslint:disable-line no-any
keysAsync: any; // tslint:disable-line no-any
delAsync: any; // tslint:disable-line no-any
smembersAsync: any; // tslint:disable-line no-any
}

export type IRolesParent = string;
export type IRolesParents = IRolesParent[];

Expand All @@ -33,7 +48,3 @@ export type IRolesObjects = IRolesObject[];
export type IBucket = string

export type IDemuxed = { roles: IRole | IRoles, resources: IResource | IResources, permissions: IPermission | IPermissions };

export interface IDynamicObject {
[key: string]: any // tslint:disable-line no-any
}

0 comments on commit f82b932

Please sign in to comment.