Created
February 17, 2019 11:36
-
-
Save jirevwe/ac07766a3a3dc20aa541248d415db381 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis, { RedisClient } from "redis"; | |
import { promisify } from "util"; | |
export class Redis implements IRedis { | |
redis: RedisClient; | |
hdel: (hash: string, field: string) => Promise<any>; | |
hget: (hash: string, field: string) => Promise<string>; | |
hgetall: (key: string) => Promise<object>; | |
hset: (hash: string, field: string, value: any) => Promise<any>; | |
quit: () => Promise<void>; | |
set: ( | |
key: string, | |
value: any, | |
mode?: string, | |
duration?: number | |
) => Promise<any>; | |
get: (key: string) => Promise<string>; | |
constructor() { | |
this.redis = redis.createClient({ | |
url: process.env.REDIS_URL, | |
password: process.env.REDIS_PASSWORD | |
}); | |
this.hdel = promisify(this.redis.hdel).bind(this.redis); | |
this.hget = promisify(this.redis.hget).bind(this.redis); | |
this.hgetall = promisify(this.redis.hgetall).bind(this.redis); | |
this.hset = promisify(this.redis.hset).bind(this.redis); | |
this.set = promisify(this.redis.set).bind(this.redis); | |
this.get = promisify(this.redis.get).bind(this.redis); | |
this.quit = promisify(this.redis.quit).bind(this.redis); | |
this.redis.on("connect", (...args) => { | |
console.log("👻 Connected to Redis db"); | |
}); | |
this.redis.on("error", err => { | |
console.log("An error occured while initialzing redis client"); | |
console.log(err); | |
}); | |
} | |
} | |
export interface IRedis { | |
redis: RedisClient; | |
hdel(hash: string, field: string): Promise<any>; | |
hget(hash: string, field: string): Promise<string>; | |
hgetall(key: string): Promise<object>; | |
hset(hash: string, field: string, value: any): Promise<any>; | |
set(key: string, value: any, mode?: string, duration?: number): Promise<any>; | |
get(key: string): Promise<string>; | |
} | |
const connections: Redis[] = [new Redis()]; | |
export default connections[0]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment