-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: flushall should mutate data, not fork it (#1125)
- Loading branch information
Showing
340 changed files
with
18,206 additions
and
6,032 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ Check the [compatibility table](compat.md) for supported redis commands. | |
## Usage ([try it in your browser with RunKit](https://runkit.com/npm/ioredis-mock)) | ||
|
||
```js | ||
const Redis = require('ioredis-mock'); | ||
const Redis = require('ioredis-mock') | ||
const redis = new Redis({ | ||
// `options.data` does not exist in `ioredis`, only `ioredis-mock` | ||
data: { | ||
|
@@ -36,7 +36,7 @@ const redis = new Redis({ | |
'user:1': { id: '1', username: 'superman', email: '[email protected]' }, | ||
'user:2': { id: '2', username: 'batman', email: '[email protected]' }, | ||
}, | ||
}); | ||
}) | ||
// Basically use it just like ioredis | ||
``` | ||
|
||
|
@@ -45,57 +45,57 @@ const redis = new Redis({ | |
There's a browser build available. You can import it directly (`import Redis from 'ioredis-mock/browser.js'`), or use it on unpkg.com: | ||
|
||
```js | ||
import Redis from 'https://unpkg.com/ioredis-mock'; | ||
import Redis from 'https://unpkg.com/ioredis-mock' | ||
|
||
const redis = new Redis(); | ||
redis.set('foo', 'bar'); | ||
console.log(await redis.get('foo')); | ||
const redis = new Redis() | ||
redis.set('foo', 'bar') | ||
console.log(await redis.get('foo')) | ||
``` | ||
|
||
### Breaking API changes from v5 | ||
|
||
Before v6, each instance of `ioredis-mock` lived in isolation: | ||
|
||
```js | ||
const Redis = require('ioredis-mock'); | ||
const redis1 = new Redis(); | ||
const redis2 = new Redis(); | ||
const Redis = require('ioredis-mock') | ||
const redis1 = new Redis() | ||
const redis2 = new Redis() | ||
|
||
await redis1.set('foo', 'bar'); | ||
console.log(await redis1.get('foo'), await redis2.get('foo')); // 'bar', null | ||
await redis1.set('foo', 'bar') | ||
console.log(await redis1.get('foo'), await redis2.get('foo')) // 'bar', null | ||
``` | ||
|
||
In v6 the [internals were rewritten](https://github.com/stipsan/ioredis-mock/pull/1110) to behave more like real life redis, if the host and port is the same, the context is now shared: | ||
|
||
```js | ||
const Redis = require('ioredis-mock'); | ||
const redis1 = new Redis(); | ||
const redis2 = new Redis(); | ||
const redis3 = new Redis({ port: 6380 }); // 6379 is the default port | ||
const Redis = require('ioredis-mock') | ||
const redis1 = new Redis() | ||
const redis2 = new Redis() | ||
const redis3 = new Redis({ port: 6380 }) // 6379 is the default port | ||
|
||
await redis1.set('foo', 'bar'); | ||
await redis1.set('foo', 'bar') | ||
console.log( | ||
await redis1.get('foo'), // 'bar' | ||
await redis2.get('foo'), // 'bar' | ||
await redis3.get('foo') // null | ||
); | ||
) | ||
``` | ||
|
||
And since `ioredis-mock` now persist data between instances, you'll [likely](https://github.com/luin/ioredis/blob/8278ec0a435756c54ba4f98587aec1a913e8b7d3/test/helpers/global.ts#L8) need to run `flushall` between testing suites: | ||
|
||
```js | ||
const Redis = require('ioredis-mock'); | ||
const Redis = require('ioredis-mock') | ||
|
||
afterEach((done) => { | ||
new Redis().flushall().then(() => done()); | ||
}); | ||
afterEach(done => { | ||
new Redis().flushall().then(() => done()) | ||
}) | ||
``` | ||
|
||
#### `createConnectedClient` is deprecated | ||
|
||
Replace it with `.duplicate()` or use another `new Redis` instance. | ||
|
||
### `ioredis-mock/jest.js` is deprecated | ||
#### `ioredis-mock/jest.js` is deprecated | ||
|
||
`ioredis-mock` is no longer doing a `import { Command } from 'ioredis'` internally, it's now doing a direct import `import Command from 'ioredis/built/command'` and thus the `jest.js` [workaround](https://github.com/stipsan/ioredis-mock/issues/568) is no longer needed: | ||
|
||
|
@@ -110,26 +110,26 @@ We also support redis [publish/subscribe](https://redis.io/topics/pubsub) channe | |
Like [ioredis](https://github.com/luin/ioredis#pubsub), you need two clients: | ||
|
||
```js | ||
const Redis = require('ioredis-mock'); | ||
const redisPub = new Redis(); | ||
const redisSub = new Redis(); | ||
const Redis = require('ioredis-mock') | ||
const redisPub = new Redis() | ||
const redisSub = new Redis() | ||
|
||
redisSub.on('message', (channel, message) => { | ||
console.log(`Received ${message} from ${channel}`); | ||
}); | ||
redisSub.subscribe('emails'); | ||
redisPub.publish('emails', '[email protected]'); | ||
console.log(`Received ${message} from ${channel}`) | ||
}) | ||
redisSub.subscribe('emails') | ||
redisPub.publish('emails', '[email protected]') | ||
``` | ||
|
||
### Promises | ||
|
||
By default, ioredis-mock uses the native Promise library. If you need (or prefer) [bluebird](http://bluebirdjs.com/) promises, set `Redis.Promise`: | ||
|
||
```js | ||
var Promise = require('bluebird'); | ||
var Redis = require('ioredis-mock'); | ||
var Promise = require('bluebird') | ||
var Redis = require('ioredis-mock') | ||
|
||
Redis.Promise = Promise; | ||
Redis.Promise = Promise | ||
``` | ||
### Lua scripting | ||
|
@@ -142,26 +142,26 @@ You could define a custom command `multiply` which accepts one | |
key and one argument. A redis key, where you can get the multiplicand, and an argument which will be the multiplicator: | ||
```js | ||
const Redis = require('ioredis-mock'); | ||
const redis = new Redis({ data: { k1: 5 } }); | ||
const Redis = require('ioredis-mock') | ||
const redis = new Redis({ data: { k1: 5 } }) | ||
const commandDefinition = { | ||
numberOfKeys: 1, | ||
lua: 'return redis.call("GET", KEYS[1]) * ARGV[1]', | ||
}; | ||
redis.defineCommand('multiply', commandDefinition); // defineCommand(name, definition) | ||
} | ||
redis.defineCommand('multiply', commandDefinition) // defineCommand(name, definition) | ||
// now we can call our brand new multiply command as an ordinary command | ||
redis.multiply('k1', 10).then((result) => { | ||
expect(result).toBe(5 * 10); | ||
}); | ||
redis.multiply('k1', 10).then(result => { | ||
expect(result).toBe(5 * 10) | ||
}) | ||
``` | ||
|
||
You can also achieve the same effect by using the `eval` command: | ||
|
||
```js | ||
const Redis = require('ioredis-mock'); | ||
const redis = new Redis({ data: { k1: 5 } }); | ||
const result = redis.eval(`return redis.call("GET", "k1") * 10`); | ||
expect(result).toBe(5 * 10); | ||
const Redis = require('ioredis-mock') | ||
const redis = new Redis({ data: { k1: 5 } }) | ||
const result = redis.eval(`return redis.call("GET", "k1") * 10`) | ||
expect(result).toBe(5 * 10) | ||
``` | ||
|
||
note we are calling the ordinary redis `GET` command by using the global `redis` object's `call` method. | ||
|
@@ -178,11 +178,11 @@ As a difference from ioredis we currently don't support: | |
Work on Cluster support has started, the current implementation is minimal and PRs welcome #359 | ||
|
||
```js | ||
const Redis = require('ioredis-mock'); | ||
const Redis = require('ioredis-mock') | ||
|
||
const cluster = new Redis.Cluster(['redis://localhost:7001']); | ||
const nodes = cluster.nodes; | ||
expect(nodes.length).toEqual(1); | ||
const cluster = new Redis.Cluster(['redis://localhost:7001']) | ||
const nodes = cluster.nodes | ||
expect(nodes.length).toEqual(1) | ||
``` | ||
|
||
## [Roadmap](https://github.com/users/stipsan/projects/1/views/4) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = { | ||
presets: [['@babel/env', { targets: { node: '10' }, bugfixes: true }]], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import hrtime from 'browser-process-hrtime'; | ||
import { Buffer } from 'buffer'; | ||
import hrtime from 'browser-process-hrtime' | ||
import { Buffer } from 'buffer' | ||
|
||
global.Buffer = Buffer; | ||
global.Buffer = Buffer | ||
|
||
export function nextTickShim(fn) { | ||
Promise.resolve().then(() => { | ||
fn(); | ||
}); | ||
fn() | ||
}) | ||
} | ||
|
||
export const hrtimeShim = hrtime; | ||
export const hrtimeShim = hrtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
// declare peer dependencies so RunKit don't throw errors | ||
require('ioredis/package.json'); | ||
require('redis-commands/package.json'); | ||
require('ioredis/package.json') | ||
require('redis-commands/package.json') | ||
|
||
const Redis = require('ioredis-mock'); | ||
const Redis = require('ioredis-mock') | ||
const redis = new Redis({ | ||
data: { | ||
user_next: '3', | ||
|
@@ -13,19 +13,19 @@ const redis = new Redis({ | |
'user:1': { id: '1', username: 'superman', email: '[email protected]' }, | ||
'user:2': { id: '2', username: 'batman', email: '[email protected]' }, | ||
}, | ||
}); | ||
}) | ||
|
||
async function main() { | ||
const userNext = await redis.incr('user_next'); | ||
const userNext = await redis.incr('user_next') | ||
await redis.hmset( | ||
`user:${userNext}`, | ||
new Map([ | ||
['id', userNext], | ||
['username', 'wonderwoman'], | ||
['email', '[email protected]'], | ||
]) | ||
); | ||
console.log(await redis.hgetall(`user:${userNext}`)); | ||
) | ||
console.log(await redis.hgetall(`user:${userNext}`)) | ||
} | ||
|
||
main(); | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.