Skip to content

Commit

Permalink
Use node URL to generate redis connection url
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Sep 26, 2022
1 parent 0a0f8bf commit 2c5f6f5
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { createClient } from 'redis';
import {URL} from 'url';

export const generateConnectUrl = (config = {}) => {
let auth = '';
let connection = `${config.host}:${config.port ?? 6379}`;
let db = '';
if (config.username || config.password) {
auth = `${config.user ?? ''}${config.password !== undefined ? `:${config.password}` : ''}@`;
const redisUrl = new URL(`redis://${config.host}`);
redisUrl.port = config.port ?? '6379';
if(config.username !== undefined) {
redisUrl.username = config.username;
}
if (config.db !== undefined) {
db = `/${config.db}`;
if(config.password !== undefined) {
redisUrl.password = config.password;
}
return `redis://${auth}${connection}${db}`;
if(config.db !== undefined) {
redisUrl.pathname = `/${config.db}`;
}

return redisUrl.href;
}

const redisStore = (args) => {
Expand Down

0 comments on commit 2c5f6f5

Please sign in to comment.