Skip to content

Commit 042d10b

Browse files
committed
feat: allow passing url to adapter to connect to redis
1 parent 63ae2b5 commit 042d10b

File tree

12 files changed

+502
-147
lines changed

12 files changed

+502
-147
lines changed

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
deno task staged

.lintstagedrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"*.{md,json,js,ts}": ["deno fmt"],
3+
"README.md": [
4+
"deno run -A --no-lock npm:markdown-toc-gen@1 insert",
5+
"deno fmt"
6+
]
7+
}

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,59 @@
22
<p align="center">A Cache port adapter for Redis in the <a href="https://hyper.io/">hyper</a> service framework</p>
33
</p>
44
<p align="center">
5-
<a href="https://nest.land/package/hyper-adapter-redis"><img src="https://nest.land/badge.svg" alt="Nest Badge" /></a>
65
<a href="https://github.com/hyper63/hyper-adapter-redis/actions/workflows/test-and-publish.yml"><img src="https://github.com/hyper63/hyper-adapter-redis/actions/workflows/test-and-publish.yml/badge.svg" alt="Test" /></a>
76
<a href="https://github.com/hyper63/hyper-adapter-redis/tags/"><img src="https://img.shields.io/github/tag/hyper63/hyper-adapter-redis" alt="Current Version" /></a>
87
</p>
98

109
---
1110

12-
## Table of Contents
11+
<!-- toc -->
1312

1413
- [Getting Started](#getting-started)
1514
- [Installation](#installation)
1615
- [Features](#features)
1716
- [Methods](#methods)
1817
- [Contributing](#contributing)
18+
- [Testing](#testing)
1919
- [License](#license)
2020

21+
<!-- tocstop -->
22+
2123
## Getting Started
2224

2325
```js
24-
import { default as redis } from 'https://x.nest.land/hyper-adapter-redis@1.2.9/mod.js'
26+
import { default as redis } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-redis/v3.1.0/mod.js'
2527

2628
export default {
2729
app: opine,
2830
adapter: [
2931
{
3032
port: 'cache',
3133
plugins: [
32-
redis({
33-
hostname: Deno.env.get('REDIS_HOST'),
34-
port: Deno.env.get('REDIS_PORT'), // defaults to 6379
35-
}),
34+
redis({ url: 'http://user@[email protected]:6379' }),
3635
],
3736
},
3837
],
3938
}
4039
```
4140

41+
You can also pass a separate `hostname` and `port` to the adapter:
42+
43+
```js
44+
redis({ hostname: 'redis.host', port: 6380 }),
45+
```
46+
47+
> `port` will always default to `6379` if not provided, then `443` if the `url` protocol is `https`
48+
> then finally `80`
49+
50+
To connect to a Redis Cluster, pass the `cluster` flag:
51+
52+
```js
53+
redis({ url: 'http://user@[email protected]:6379', cluster: true })
54+
```
55+
56+
The adapter will automatically discover all nodes in the Cluster.
57+
4258
## Installation
4359

4460
This is a Deno module available to import from
@@ -47,7 +63,7 @@ This is a Deno module available to import from
4763
deps.js
4864

4965
```js
50-
export { default as redis } from 'https://x.nest.land/hyper-adapter-redis@1.2.9/mod.js'
66+
export { default as redis } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-redis/v3.1.0/mod.js'
5167
```
5268

5369
## Features

adapter.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,20 @@ const mapTtl = (ttl) =>
4141
/**
4242
* @typedef Options
4343
* @property {number} scanCount
44+
* @property {any} client
4445
*
45-
* @param {*} client
4646
* @param {Options} options
47-
* @returns
4847
*/
49-
export default function (client, options) {
48+
export default function ({ redis, scanCount }) {
5049
// redis commands
5150
// key: Promise<string>
52-
const get = Async.fromPromise(client.get.bind(client))
51+
const get = Async.fromPromise(redis.get.bind(redis))
5352
// key, value, { px, ex }: Promise<string>
54-
const set = Async.fromPromise(client.set.bind(client))
53+
const set = Async.fromPromise(redis.set.bind(redis))
5554
// key, key, key: Promise<string[]>
56-
const del = Async.fromPromise(client.del.bind(client))
55+
const del = Async.fromPromise(redis.del.bind(redis))
5756
// cursor, { type, pattern }: Promise<[string, string[]]>
58-
const scan = Async.fromPromise(client.scan.bind(client))
59-
60-
const { scanCount } = options
57+
const scan = Async.fromPromise(redis.scan.bind(redis))
6158

6259
const index = () => {
6360
return Promise.resolve(HyperErr({ status: 501, msg: 'Not Implemented' }))

adapter.test.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ const baseStubClient = {
1111
scan: resolves(),
1212
}
1313

14-
const baseOptions = { scanCount: 100 }
15-
16-
const createAdapter = (client, options) => cachePort(factory(client, options))
14+
const createAdapter = (redis) => cachePort(factory({ redis, scanCount: 100 }))
1715

1816
Deno.test('adapter', async (t) => {
1917
await t.step('listDocs', async (t) => {
@@ -33,7 +31,7 @@ Deno.test('adapter', async (t) => {
3331
...baseStubClient,
3432
get: resolves(JSON.stringify({ bam: 'baz' })),
3533
scan,
36-
}, baseOptions)
34+
})
3735

3836
results = await adapter.listDocs({
3937
store: 'word',
@@ -58,7 +56,7 @@ Deno.test('adapter', async (t) => {
5856
...baseStubClient,
5957
get: resolves(JSON.stringify(doc)),
6058
scan: resolves(['0', ['key']]),
61-
}, baseOptions)
59+
})
6260

6361
const result = await adapter.listDocs({
6462
store: 'foo',
@@ -73,7 +71,7 @@ Deno.test('adapter', async (t) => {
7371

7472
await t.step('createStore', async (t) => {
7573
await t.step('should create a logical keyspace in redis', async () => {
76-
const adapter = createAdapter(baseStubClient, baseOptions)
74+
const adapter = createAdapter(baseStubClient)
7775

7876
const result = await adapter.createStore('foo')
7977
assert(result.ok)
@@ -83,7 +81,7 @@ Deno.test('adapter', async (t) => {
8381
const adapter = createAdapter({
8482
...baseStubClient,
8583
get: resolves(undefined), // looking up store produces undefined
86-
}, baseOptions)
84+
})
8785

8886
const err = await adapter.createDoc({
8987
store: 'foo',
@@ -106,7 +104,7 @@ Deno.test('adapter', async (t) => {
106104
...baseStubClient,
107105
del,
108106
scan: resolves(['0', []]),
109-
}, baseOptions)
107+
})
110108

111109
const result = await adapter.destroyStore('foo')
112110

@@ -123,7 +121,7 @@ Deno.test('adapter', async (t) => {
123121
...baseStubClient,
124122
del,
125123
scan: resolves(['0', ['{foo}_baz', '{foo}_bar']]),
126-
}, baseOptions)
124+
})
127125

128126
const result = await adapter.destroyStore('foo')
129127

@@ -149,7 +147,7 @@ Deno.test('adapter', async (t) => {
149147
k === 'store_foo'
150148
? Promise.resolve(JSON.stringify({ active: true }))
151149
: Promise.resolve(null),
152-
}, baseOptions)
150+
})
153151

154152
const result = await adapter.createDoc({
155153
store: 'foo',
@@ -171,7 +169,7 @@ Deno.test('adapter', async (t) => {
171169
k === 'store_foo'
172170
? Promise.resolve(JSON.stringify({ active: true })) // store
173171
: Promise.resolve(JSON.stringify({ foo: 'bar' })), // doc already exists
174-
}, baseOptions)
172+
})
175173

176174
const err = await adapter.createDoc({
177175
store: 'foo',
@@ -201,7 +199,7 @@ Deno.test('adapter', async (t) => {
201199
k === 'store_foo'
202200
? Promise.resolve(JSON.stringify({ foo: 'bar' }))
203201
: Promise.resolve(undefined), // not found
204-
}, baseOptions)
202+
})
205203

206204
await adapter.createDoc({
207205
store: 'foo',
@@ -235,7 +233,7 @@ Deno.test('adapter', async (t) => {
235233
? Promise.resolve(JSON.stringify({ active: true })) // store
236234
: (assertEquals(k, '{foo}_bar'), Promise.resolve(JSON.stringify(value)))
237235
},
238-
}, baseOptions)
236+
})
239237

240238
const result = await adapter.getDoc({
241239
store: 'foo',
@@ -252,7 +250,7 @@ Deno.test('adapter', async (t) => {
252250
k === 'store_foo'
253251
? Promise.resolve(JSON.stringify({ foo: 'bar' }))
254252
: Promise.resolve(undefined),
255-
}, baseOptions)
253+
})
256254

257255
const err = await adapter.getDoc({
258256
store: 'foo',
@@ -281,15 +279,14 @@ Deno.test('adapter', async (t) => {
281279
k === 'store_foo'
282280
? Promise.resolve(JSON.stringify({ active: true }))
283281
: Promise.resolve(undefined),
284-
}, baseOptions)
282+
})
285283

286284
const result = await adapter.updateDoc({
287285
store: 'foo',
288286
key: 'bar',
289287
value: { hello: 'world' },
290288
ttl: String(123),
291289
})
292-
console.log(result)
293290
assert(result.ok)
294291
})
295292

@@ -306,7 +303,7 @@ Deno.test('adapter', async (t) => {
306303
k === 'store_foo'
307304
? Promise.resolve(JSON.stringify({ foo: 'bar' }))
308305
: Promise.resolve(undefined), // not found
309-
}, baseOptions)
306+
})
310307

311308
await adapter.updateDoc({
312309
store: 'foo',
@@ -334,7 +331,7 @@ Deno.test('adapter', async (t) => {
334331
const adapter = createAdapter({
335332
...baseStubClient,
336333
get: (k) => k === 'store_foo' ? Promise.resolve('{"active": true}') : Promise.resolve(null),
337-
}, baseOptions)
334+
})
338335

339336
const result = await adapter.deleteDoc({
340337
store: 'foo',

deno.jsonc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"tasks": {
3+
"prepare": "deno run -A npm:husky@^8 install",
4+
"staged": "deno run -A npm:lint-staged@^13",
35
"cache": "deno cache --lock=deno.lock --lock-write deps.js dev_deps.js",
46
"test": "deno lint && deno fmt --check && deno test -A --unstable --no-check",
57
"test:suite": "deno test --allow-net --allow-env --no-check --no-lock --import-map=https://raw.githubusercontent.com/hyper63/hyper/hyper-test%40v2.1.4/packages/test/import_map.json https://raw.githubusercontent.com/hyper63/hyper/hyper-test%40v2.1.4/packages/test/mod.js",

0 commit comments

Comments
 (0)