Skip to content

v4.0.0-rc.2 #1664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7983dd4
update workflows & README
leibale Sep 2, 2021
e421dc4
add .deepsource.toml
leibale Sep 2, 2021
b80afc6
fix client.quit, add error events on cluster, fix some "deepsource.io…
leibale Sep 2, 2021
18ad329
Release 4.0.0-rc.1
leibale Sep 6, 2021
1413a69
add cluster.duplicate, add some tests
leibale Sep 9, 2021
08837c8
fix #1650 - add support for Buffer in some commands, add GET_BUFFER c…
leibale Sep 13, 2021
9fc08d4
fix GET and GET_BUFFER return type
leibale Sep 13, 2021
b91897a
update FAQ
leibale Sep 14, 2021
64f4567
Update invalid code example in README.md (#1654)
ricsam Sep 14, 2021
0f5a278
fix #1652
leibale Sep 14, 2021
d5fa6a3
Merge branch 'master' of https://github.com/NodeRedis/node-redis
leibale Sep 14, 2021
0ab2245
ref #1653 - better types
leibale Sep 18, 2021
5412479
better types
leibale Sep 18, 2021
10b9c59
fix 54124793ad1b633d39d372bdff487c9888c017a7
leibale Sep 18, 2021
3cd31e3
Update GEOSEARCHSTORE.spec.ts
leibale Sep 18, 2021
3a169d5
fix #1660 - add support for client.HSET('key', 'field', 'value')
leibale Sep 20, 2021
4585a52
Merge branch 'master' of https://github.com/NodeRedis/node-redis
leibale Sep 20, 2021
d79bc55
upgrade dependencies, update README
leibale Sep 20, 2021
1819b9c
fix #1659 - add support for db-number in client options url
leibale Sep 21, 2021
ad151cd
fix README, remove unused import, downgrade typedoc & typedoc-plugin-…
leibale Sep 21, 2021
1c13a65
update client-configurations.md
leibale Sep 21, 2021
9237a4e
fix README
leibale Sep 23, 2021
42dcf80
add CLUSTER_SLOTS, add some tests
leibale Sep 23, 2021
5be5474
Merge branch 'v4.0' of https://github.com/NodeRedis/node-redis
leibale Sep 23, 2021
7d286e7
fix "createClient with url" test with redis 5
leibale Sep 23, 2021
06ee6af
remove unused imports
leibale Sep 23, 2021
01df651
Release 4.0.0-rc.2
leibale Sep 23, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 73 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,22 @@ npm install redis@next
import { createClient } from 'redis';

(async () => {
const client = createClient();
const client = createClient();

client.on('error', (err) => console.log('Redis Client Error', err));
client.on('error', (err) => console.log('Redis Client Error', err));

await client.connect();
await client.connect();

await client.set('key', 'value');
const value = await client.get('key');
await client.set('key', 'value');
const value = await client.get('key');
})();
```

The above code connects to localhost on port 6379. To connect to a different host or port, use a connection string in the format `[redis[s]:]//[[username][:password]@][host][:port]`:
The above code connects to localhost on port 6379. To connect to a different host or port, use a connection string in the format `redis[s]://[[username][:password]@][host][:port][/db-number]`:

```typescript
createClient({
socket: {
url: 'redis://alice:[email protected]:6380'
}
url: 'redis://alice:[email protected]:6380',
});
```

Expand All @@ -79,8 +77,8 @@ Modifiers to commands are specified using a JavaScript object:

```typescript
await client.set('key', 'value', {
EX: 10,
NX: true
EX: 10,
NX: true,
});
```

Expand Down Expand Up @@ -108,11 +106,11 @@ Start a [transaction](https://redis.io/topics/transactions) by calling `.multi()
```typescript
await client.set('another-key', 'another-value');

const [ setKeyReply, otherKeyValue ] = await client.multi()
.set('key', 'value')
.get('another-key')
.exec()
]); // ['OK', 'another-value']
const [setKeyReply, otherKeyValue] = await client
.multi()
.set('key', 'value')
.get('another-key')
.exec(); // ['OK', 'another-value']
```

You can also [watch](https://redis.io/topics/transactions#optimistic-locking-using-check-and-set) keys by calling `.watch()`. Your transaction will abort if any of the watched keys change.
Expand All @@ -128,10 +126,7 @@ This pattern works especially well for blocking commands—such as `BLPOP` and `
```typescript
import { commandOptions } from 'redis';

const blPopPromise = client.blPop(
commandOptions({ isolated: true }),
'key'
);
const blPopPromise = client.blPop(commandOptions({ isolated: true }), 'key');

await client.lPush('key', ['1', '2']);

Expand All @@ -153,12 +148,12 @@ await subscriber.connect();
Once you have one, simply subscribe and unsubscribe as needed:

```typescript
await subscriber.subscribe('channel', message => {
console.log(message); // 'message'
await subscriber.subscribe('channel', (message) => {
console.log(message); // 'message'
});

await subscriber.pSubscribe('channe*', (message, channel) => {
console.log(message, channel); // 'message', 'channel'
console.log(message, channel); // 'message', 'channel'
});

await subscriber.unsubscribe('channel');
Expand All @@ -178,26 +173,29 @@ await publisher.publish('channel', 'message');

```typescript
for await (const key of client.scanIterator()) {
// use the key!
await client.get(key);
// use the key!
await client.get(key);
}
```

This works with `HSCAN`, `SSCAN`, and `ZSCAN` too:

```typescript
for await (const member of client.hScanIterator('hash')) {}
for await (const { field, value } of client.sScanIterator('set')) {}
for await (const { member, score } of client.zScanIterator('sorted-set')) {}
for await (const member of client.hScanIterator('hash')) {
}
for await (const { field, value } of client.sScanIterator('set')) {
}
for await (const { member, score } of client.zScanIterator('sorted-set')) {
}
```

You can override the default options by providing a configuration object:

```typescript
client.scanIterator({
TYPE: 'string', // `SCAN` only
MATCH: 'patter*',
COUNT: 100
TYPE: 'string', // `SCAN` only
MATCH: 'patter*',
COUNT: 100,
});
```

Expand All @@ -209,27 +207,26 @@ Define new functions using [Lua scripts](https://redis.io/commands/eval) which e
import { createClient, defineScript } from 'redis';

(async () => {
const client = createClient({
scripts: {
add: defineScript({
NUMBER_OF_KEYS: 1,
SCRIPT:
'local val = redis.pcall("GET", KEYS[1]);' +
'return val + ARGV[1];',
transformArguments(key: string, toAdd: number): Array<string> {
return [key, number.toString()];
},
transformReply(reply: number): number {
return reply;
}
})
}
});

await client.connect();

await client.set('key', '1');
await client.add('key', 2); // 3
const client = createClient({
scripts: {
add: defineScript({
NUMBER_OF_KEYS: 1,
SCRIPT:
"local val = redis.pcall('GET', KEYS[1]);' + 'return val + ARGV[1];",
transformArguments(key: string, toAdd: number): Array<string> {
return [key, number.toString()];
},
transformReply(reply: number): number {
return reply;
},
}),
},
});

await client.connect();

await client.set('key', '1');
await client.add('key', 2); // 3
})();
```

Expand All @@ -241,22 +238,25 @@ Connecting to a cluster is a bit different. Create the client by specifying some
import { createCluster } from 'redis';

(async () => {
const cluster = createCluster({
rootNodes: [{
host: '10.0.0.1',
port: 30001
}, {
host: '10.0.0.2',
port: 30002
}]
});

cluster.on('error', (err) => console.log('Redis Cluster Error', err));

await cluster.connect();

await cluster.set('key', 'value');
const value = await cluster.get('key');
const cluster = createCluster({
rootNodes: [
{
host: '10.0.0.1',
port: 30001,
},
{
host: '10.0.0.2',
port: 30002,
},
],
});

cluster.on('error', (err) => console.log('Redis Cluster Error', err));

await cluster.connect();

await cluster.set('key', 'value');
const value = await cluster.get('key');
})();
```

Expand All @@ -273,8 +273,8 @@ Of course, if you don't do something with your Promises you're certain to get [u

```typescript
await Promise.all([
client.set('Tm9kZSBSZWRpcw==', 'users:1'),
client.sAdd('users:1:tokens', 'Tm9kZSBSZWRpcw==')
client.set('Tm9kZSBSZWRpcw==', 'users:1'),
client.sAdd('users:1:tokens', 'Tm9kZSBSZWRpcw=='),
]);
```

Expand All @@ -284,7 +284,9 @@ If you'd like to contribute, check out the [contributing guide](CONTRIBUTING.md)

Thank you to all the people who already contributed to Node Redis!

<a href="https://github.com/NodeRedis/node-redis/graphs/contributors"><img src="https://opencollective.com/node-redis/contributors.svg?width=1012" /></a>
<a href="https://github.com/NodeRedis/node-redis/graphs/contributors">
<img src="https://contrib.rocks/image?repo=NodeRedis/node-redis"/>
</a>

## License

Expand Down
2 changes: 1 addition & 1 deletion docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ When a socket closed unexpectedly, all the commands that were already sent will

## How are commands batched?

Commands are pipelined using [`queueMicrotask`](https://nodejs.org/api/globals.html#globals_queuemicrotask_callback). Commands from the same "tick" will be sent in batches and respect the [`writableHighWaterMark`](https://nodejs.org/api/stream.html#stream_new_stream_writable_options).
Commands are pipelined using [`queueMicrotask`](https://nodejs.org/api/globals.html#globals_queuemicrotask_callback).

If `socket.write()` returns `false`—meaning that ["all or part of the data was queued in user memory"](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback:~:text=all%20or%20part%20of%20the%20data%20was%20queued%20in%20user%20memory)—the commands will stack in memory until the [`drain`](https://nodejs.org/api/net.html#net_event_drain) event is fired.
40 changes: 21 additions & 19 deletions docs/client-configuration.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# `createClient` configuration

| Property | Default | Description |
|--------------------------|------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|
| socket | | Object defining socket connection properties |
| socket.url | | `[redis[s]:]//[[username][:password]@][host][:port]` |
| socket.host | `'localhost'` | Hostname to connect to |
| socket.port | `6379` | Port to connect to |
| socket.username | | ACL username ([see ACL guide](https://redis.io/topics/acl)) |
| socket.password | | ACL password or the old "--requirepass" password |
| socket.connectTimeout | `5000` | The timeout for connecting to the Redis Server (in milliseconds) |
| socket.noDelay | `true` | Enable/disable the use of [`Nagle's algorithm`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) |
| socket.keepAlive | `5000` | Enable/disable the [`keep-alive`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) functionality |
| socket.tls | | Set to `true` to enable [TLS Configuration](https://nodejs.org/api/tls.html#tls_tls_connect_options_callback) |
| socket.reconnectStrategy | `retries => Math.min(retries * 50, 500)` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
| modules | | Object defining which [Redis Modules](https://redis.io/modules) to include (TODO - document) |
| scripts | | Object defining Lua scripts to use with this client. See [Lua Scripts](../README.md#lua-scripts) |
| commandsQueueMaxLength | | Maximum length of the client's internal command queue |
| readonly | `false` | Connect in [`READONLY`](https://redis.io/commands/readonly) mode |
| legacyMode | `false` | Maintain some backwards compatibility (see the [Migration Guide](v3-to-v4.md)) |
| isolationPoolOptions | | See the [Isolated Execution Guide](./isolated-execution.md) |
| Property | Default | Description |
|--------------------------|------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| url | | `redis[s]://[[username][:password]@][host][:port][/db-number]` (see [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details) |
| socket | | Object defining socket connection properties |
| socket.host | `'localhost'` | Hostname to connect to |
| socket.port | `6379` | Port to connect to |
| socket.path | | UNIX Socket to connect to |
| socket.connectTimeout | `5000` | The timeout for connecting to the Redis Server (in milliseconds) |
| socket.noDelay | `true` | Enable/disable the use of [`Nagle's algorithm`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) |
| socket.keepAlive | `5000` | Enable/disable the [`keep-alive`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) functionality |
| socket.tls | | Set to `true` to enable [TLS Configuration](https://nodejs.org/api/tls.html#tls_tls_connect_options_callback) |
| socket.reconnectStrategy | `retries => Math.min(retries * 50, 500)` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
| username | | ACL username ([see ACL guide](https://redis.io/topics/acl)) |
| password | | ACL password or the old "--requirepass" password |
| database | | Database number to connect to (see [`SELECT`](https://redis.io/commands/select) command) |
| modules | | Object defining which [Redis Modules](https://redis.io/modules) to include (TODO - document) |
| scripts | | Object defining Lua Scripts to use with this client (see [Lua Scripts](../README.md#lua-scripts)) |
| commandsQueueMaxLength | | Maximum length of the client's internal command queue |
| readonly | `false` | Connect in [`READONLY`](https://redis.io/commands/readonly) mode |
| legacyMode | `false` | Maintain some backwards compatibility (see the [Migration Guide](v3-to-v4.md)) |
| isolationPoolOptions | | See the [Isolated Execution Guide](./isolated-execution.md) |

## Reconnect Strategy

Expand Down
Loading