From 54f7b8744d7bc59d94f59de7d8ae8486e0cc4f79 Mon Sep 17 00:00:00 2001 From: Yusuf Yilmaz Date: Fri, 27 May 2022 15:22:24 +0200 Subject: [PATCH] Fix config and redis settings --- config/jest.config.js | 2 +- src/lib/document-stores/redis.ts | 30 ++++++++++++------------------ src/types/config.ts | 2 -- test/document-stores/redis.test.ts | 3 --- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/config/jest.config.js b/config/jest.config.js index 67e8891..b7b2420 100644 --- a/config/jest.config.js +++ b/config/jest.config.js @@ -2,7 +2,7 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', - rootDir: './', + rootDir: '../', testRegex: '\\.test\\.ts$', reporters: ['default'] } diff --git a/src/lib/document-stores/redis.ts b/src/lib/document-stores/redis.ts index f0368d7..c2e3101 100644 --- a/src/lib/document-stores/redis.ts +++ b/src/lib/document-stores/redis.ts @@ -30,23 +30,18 @@ class RedisDocumentStore implements Store { connect = (options: RedisStoreConfig) => { winston.info('configuring redis') - const url = process.env.REDISTOGO_URL || options.url - const host = options.host || '127.0.0.1' - const port = options.port || 6379 + const url = process.env.REDISTOGO_URL || options.url || 'redis://redis:6379' const index = options.db || 0 - - if (url) { - this.client = createClient({ url }) - this.client.connect() - } else { - this.client = createClient({ - url: `http://${host}:${port}`, - database: index as number, - username: options.username, - password: options.password, - }) + const config = { + url, + database: index as number, + ...(options.username ? { username: options.username } : {}), + ...(options.password ? { username: options.username } : {}), } + this.client = createClient(config) + this.client.connect() + this.client.on('error', err => { winston.error('redis disconnected', err) }) @@ -54,9 +49,7 @@ class RedisDocumentStore implements Store { this.client .select(index as number) .then(() => { - winston.info( - `connected to redis on ${url || `${host}:${port}`}/${index}`, - ) + winston.info(`connected to redis on ${url}/${index}`) }) .catch(err => { winston.error(`error connecting to redis index ${index}`, { @@ -85,7 +78,8 @@ class RedisDocumentStore implements Store { callback: Callback, skipExpire?: boolean | undefined, ): void => { - this.client?.set(key, data, this.getExpire(skipExpire)) + this.client + ?.set(key, data, this.getExpire(skipExpire)) .then(() => { callback(true) }) diff --git a/src/types/config.ts b/src/types/config.ts index d095afe..37fee16 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -52,8 +52,6 @@ export interface RethinkDbStoreConfig extends BaseStoreConfig { export interface RedisStoreConfig extends BaseStoreConfig { url?: string - host?: string - port?: string db?: string user?: string username?: string | undefined diff --git a/test/document-stores/redis.test.ts b/test/document-stores/redis.test.ts index aca525a..0911682 100644 --- a/test/document-stores/redis.test.ts +++ b/test/document-stores/redis.test.ts @@ -14,7 +14,6 @@ describe('Redis document store', () => { store = new RedisDocumentStore({ expire: 10, type: 'redis', - url: 'http://localhost:6666', }) return store.set('hello1', 'world', async () => { const res = await store.client?.ttl('hello1') @@ -26,7 +25,6 @@ describe('Redis document store', () => { store = new RedisDocumentStore({ expire: 10, type: 'redis', - url: 'http://localhost:6666', }) store.set( @@ -43,7 +41,6 @@ describe('Redis document store', () => { it('should not set an expiration when expiration is off', async () => { store = new RedisDocumentStore({ type: 'redis', - url: 'http://localhost:6666', }) store.set('hello3', 'world', async () => {