Skip to content

Commit c8a96ce

Browse files
Brian DeteringBrian Detering
authored andcommitted
added compatability for ioredis #3
1 parent 31cf788 commit c8a96ce

File tree

8 files changed

+378
-341
lines changed

8 files changed

+378
-341
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ adding Redis as a caching layer.
88

99
```javascript
1010
const redisClient = require('redis').createClient();
11+
// the "ioredis" module is also supported
12+
// const Redis = require('ioredis');
13+
// const redisClient = new Redis();
1114
const DataLoader = require('dataloader');
1215
const RedisDataLoader = require('redis-dataloader')({ redis: redisClient });
1316

@@ -90,7 +93,7 @@ All the options available to Facebook Dataloader can be passed in here. An
9093
additional option called **expire** is also available, and will set a ttl in seconds
9194
on all keys set in redis if this option is passed.
9295

93-
By default, the "cacheKeyFn" will serialize objects and arrays using [json-stable-stringify](https://github.com/substack/json-stable-stringify) and allow all other values to pass through unchanged.
96+
The `cacheKeyFn` will default to serialize objects and arrays using [json-stable-stringify](https://github.com/substack/json-stable-stringify) and allow all other values to pass through unchanged.
9497

9598
### Caching
9699

@@ -109,3 +112,10 @@ you may want to disable to the local cache, and just rely on the redis cache ins
109112
```
110113
const loader = new RedisDataLoader('prefix', new DataLoader(), { cache: false });
111114
```
115+
116+
## Development
117+
118+
1. Install Dependencies `npm install`
119+
1. Start Redis `docker-compose stop && docker-compose rm && docker-compose build && docker-compose up -d`
120+
1. Run Tests `grunt test`
121+

gulpfile.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
const gulp = require('gulp');
22
const mocha = require('gulp-mocha');
33

4-
gulp.task('default', () => gulp.src('test.js', { read: false }).pipe(mocha()));
5-
gulp.task('test', () => gulp.src('test.js', { read: false }).pipe(mocha()));
4+
gulp.task('default', () =>
5+
gulp.src('test/**/*.unit.js', { read: false }).pipe(mocha())
6+
);
7+
gulp.task('test', () =>
8+
gulp.src('test/**/*.unit.js', { read: false }).pipe(mocha())
9+
);

index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ const _ = require('lodash');
22
const Promise = require('bluebird');
33
const DataLoader = require('dataloader');
44
const stringify = require('json-stable-stringify');
5+
const IORedis = require('ioredis');
56

67
module.exports = fig => {
78
const redis = fig.redis;
9+
const isIORedis = redis instanceof IORedis;
810

911
const parse = (resp, opt) =>
1012
new Promise((resolve, reject) => {
@@ -47,10 +49,13 @@ module.exports = fig => {
4749
multi.expire(fullKey, opt.expire);
4850
}
4951
multi.get(fullKey);
50-
multi.exec(
51-
(err, replies) =>
52-
err ? reject(err) : parse(_.last(replies), opt).then(resolve)
53-
);
52+
multi.exec((err, replies) => {
53+
const lastReply = isIORedis
54+
? _.last(_.last(replies))
55+
: _.last(replies);
56+
57+
return err ? reject(err) : parse(lastReply, opt).then(resolve);
58+
});
5459
})
5560
);
5661

@@ -66,10 +71,11 @@ module.exports = fig => {
6671
new Promise((resolve, reject) =>
6772
redis.mget(
6873
_.map(keys, k => makeKey(keySpace, k, opt.cacheKeyFn)),
69-
(err, results) =>
70-
err
74+
(err, results) => {
75+
return err
7176
? reject(err)
72-
: Promise.map(results, r => parse(r, opt)).then(resolve)
77+
: Promise.map(results, r => parse(r, opt)).then(resolve);
78+
}
7379
)
7480
);
7581

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redis-dataloader",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "DataLoader Using Redis as a Cache",
55
"main": "index.js",
66
"scripts": {
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"bluebird": "^3.5.0",
2828
"dataloader": "^1.2.0",
29+
"ioredis": "^3.1.2",
2930
"json-stable-stringify": "^1.0.1",
3031
"lodash": "^4.17.2"
3132
},
@@ -34,6 +35,7 @@
3435
"chai-as-promised": "^7.1.1",
3536
"gulp": "^3.9.1",
3637
"gulp-mocha": "^3.0.1",
38+
"ioredis": "^3.1.2",
3739
"redis": "^2.6.3",
3840
"sinon": "^1.17.6"
3941
}

0 commit comments

Comments
 (0)