import RedisClient , { Redis , Pipeline } from 'ioredis' ; import { map } from 'lodash' ; import { loadLuaScripts , LuaScriptName } from '../lua' ; export type RedisConfig = { host ? : string ; port ? : number ; db ? : number ; password ? : string ; url ? : string ; lazyConnect ? : boolean ; enableReadyCheck ? : boolean ; enableOfflineQueue ? : boolean ; tls ? : any ; } ; export const createClientAndLoadLuaScripts = ( { host , port , db = 0 , password , url , lazyConnect , enableReadyCheck , enableOfflineQueue , tls , } ) => { const client = url ? new RedisClient ( url , { lazyConnect , maxRetriesPerRequest : null , enableReadyCheck , enableOfflineQueue , tls , } ) : new RedisClient ( { host , port , db , password , lazyConnect , maxRetriesPerRequest : null , enableOfflineQueue , enableReadyCheck , tls , } ) ; const updatedClient = loadLuaScripts ( { client } ) ; return updatedClient ; } ; export const callLuaScriptMulti = ( { script , args , multi , } ) => { ( multi as any ) [ script ] ( ... args ) ; } ; export const callLuaScript = ( { script , args , client , } ) => { return ( client as any ) [ script ] ( ... args ) as Promise < string | string [ ] > ; } ; export const publish = ( { channel , message , client , } ) => { return new Promise ( ( resolve , reject ) => { client . publish ( channel , message , ( err , res ) => err ? reject ( err ) : resolve ( res ) ) ; } ) ; } ; export const ensureConnected = async ( { client } ) => { try { await client . connect ( ) ; } catch ( e ) { if ( e . message !== 'Redis is already connecting/connected' ) { throw e ; } } } ; export const tryIgnore = async < T > ( f , shouldThrow ) => { try { return await f ( ) ; } catch ( e ) { if ( shouldThrow ( e ) ) { throw e ; } } } ; export const ensureDisconnected = ( { client } ) => { return client . status === 'end' ? Promise . resolve ( ) : new Promise ( ( resolve ) => { client . disconnect ( ) ; client . on ( 'end' , ( ) => resolve ( ) ) ; } ) ; } ; export const exec = ( multi_ ) => { return new Promise ( ( resolve , reject ) => { multi_ . exec ( ( err , results ) => err ? reject ( err ) : resolve ( map ( results , ( result ) => result [ 1 ] ) ) ) ; } ) as Promise < ( string | number ) [ ] > ; } ; export const rpoplpush = ( { client , fromKey , toKey , } ) => { return new Promise ( ( resolve , reject ) => { client . rpoplpush ( fromKey , toKey , ( err , result ) => err ? reject ( err ) : resolve ( result ) ) ; } ) ; } ; export const zrangebyscore = ( { client , key , min , max , } ) => { return client . zrangebyscore ( key , min , max ) ; } ; export const set = ( { key , value , client , ttl , } ) => new Promise ( ( resolve , reject ) => { if ( ttl ) { client . set ( key , value , 'px' , ttl , ( err , result ) => err ? reject ( err ) : resolve ( result ) ) ; } else { client . set ( key , value , ( err , result ) => err ? reject ( err ) : resolve ( result ) ) ; } } ) ; export const brpoplpush = ( { fromKey , toKey , timeout = 0 , client , } ) : Promise < string | null > => new Promise ( ( resolve , reject ) => { client . brpoplpush ( fromKey , toKey , timeout , ( err , result ) => err ? reject ( err ) : resolve ( result ) ) ; } ) ; export const flushAll = ( { client } ) => new Promise ( ( resolve , reject ) => { client . flushall ( ( err , result ) => ( err ? reject ( err ) : resolve ( result ) ) ) ; } ) ; export const quit = ( { client } ) => new Promise ( ( resolve , reject ) => { client . quit ( ( err , result ) => ( err ? reject ( err ) : resolve ( result ) ) ) ; } ) ; export const hkeys = ( { client , key , } ) : Promise < string [ ] > => new Promise ( ( resolve , reject ) => { client . hkeys ( key , ( err , result ) => ( err ? reject ( err ) : resolve ( result ) ) ) ; } ) ; export const lrange = ( { start , stop , key , client , } ) : Promise < string [ ] > => new Promise ( ( resolve , reject ) => { client . lrange ( key , start , stop , ( err , result ) => err ? reject ( err ) : resolve ( result ) ) ; } ) ; export const keys = ( { pattern , client , } ) : Promise < string [ ] > => new Promise ( ( resolve , reject ) => { client . keys ( pattern , ( err , result ) => err ? reject ( err ) : resolve ( result ) ) ; } ) ; export const mget = ( { keys : mgetKeys , client , } ) => client . mget ( ... mgetKeys ) ; export const get = ( { key , client } ) => client . get ( key ) ; export const del = ( { key , client } ) => client . del ( key ) ;