Skip to content

Commit

Permalink
feat: allow connection redis options to be passed directly to constru…
Browse files Browse the repository at this point in the history
…ctor (#8)
  • Loading branch information
kldavis4 authored Nov 16, 2023
1 parent 90c47d0 commit 88829ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "upstash-redis-level",
"description": "@upstash/redis backed abstract-level database for Node.js",
"version": "1.1.0",
"version": "1.1.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "Apache-2.0",
Expand Down
21 changes: 10 additions & 11 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import {RedisLevel} from './index'
import 'isomorphic-fetch'
import {Redis} from '@upstash/redis'

const ModuleError = require('module-error')

Expand All @@ -23,21 +22,17 @@ const keyNotFoundError = (key: string) => new ModuleError(`Key ${key} was not fo
})

describe('redis-level', () => {
let redis: Redis
let level: RedisLevel
beforeEach(() => {
redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL || 'http://localhost:8079',
token: process.env.UPSTASH_REDIS_REST_TOKEN || 'mytoken',
automaticDeserialization: true,
})
})

describe('json encoding', () => {
let namespace = `test-${Math.random()}`
beforeEach(async () => {
level = new RedisLevel<string, string>({
redis,
redis: {
url: process.env.UPSTASH_REDIS_REST_URL || 'http://localhost:8079',
token: process.env.UPSTASH_REDIS_REST_TOKEN || 'mytoken',
automaticDeserialization: true,
},
namespace,
keyEncoding: 'json',
valueEncoding: 'json',
Expand All @@ -62,7 +57,11 @@ describe('redis-level', () => {
describe('default encoding', () => {
beforeEach(async () => {
level = new RedisLevel<string, string>({
redis,
redis: {
url: process.env.UPSTASH_REDIS_REST_URL || 'http://localhost:8079',
token: process.env.UPSTASH_REDIS_REST_TOKEN || 'mytoken',
automaticDeserialization: true,
},
namespace: `test-${Math.random()}`
})
})
Expand Down
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ const encodeFilter = (filter: string) => {
}
return encodeURIComponent(filter)
}

type RedisConnectionOptions = {
url: string
token: string
automaticDeserialization?: boolean
}

export type RedisLevelOptions<K, V> = {
redis: RedisConnectionOptions | Redis
debug?: boolean
redis: Redis
namespace?: string
} & AbstractDatabaseOptions<K, V>

Expand Down Expand Up @@ -226,7 +233,11 @@ export class RedisLevel<KDefault = string, VDefault = string> extends AbstractLe

constructor(options: RedisLevelOptions<KDefault, VDefault>) {
super({ encodings: { utf8: true }, snapshots: false }, options)
this.redis = options.redis
this.redis = (typeof options.redis === 'object' &&
'hmget' in options.redis &&
typeof options.redis.hmget === 'function') ?
options.redis as Redis :
new Redis(options.redis as RedisConnectionOptions)
const namespace = options.namespace || 'level'
this.hKey = `${namespace}:h`
this.zKey = `${namespace}:z`
Expand Down

0 comments on commit 88829ad

Please sign in to comment.