* feat(errors): Add specialized timeout error types for maintenance scenarios - Added `SocketTimeoutDuringMaintananceError`, a subclass of `TimeoutError`, to handle socket timeouts during maintenance. - Added `CommandTimeoutDuringMaintenanceError`, another subclass of `TimeoutError`, to address command write timeouts during maintenance. * feat(linked-list): Add EmptyAwareSinglyLinkedList and enhance DoublyLinkedList functionality - Introduced `EmptyAwareSinglyLinkedList`, a subclass of `SinglyLinkedList` that emits an `empty` event when the list becomes empty due to `reset`, `shift`, or `remove` operations. - Added `nodes()` iterator method to `DoublyLinkedList` for iterating over nodes directly. - Enhanced unit tests for `DoublyLinkedList` and `SinglyLinkedList` to cover edge cases and new functionality. - Added comprehensive tests for `EmptyAwareSinglyLinkedList` to validate `empty` event emission under various scenarios. - Improved code formatting and consistency. * refactor(commands-queue): Improve push notification handling - Replaced `setInvalidateCallback` with a more flexible `addPushHandler` method, allowing multiple handlers for push notifications. - Introduced the `PushHandler` type to standardize push notification processing. - Refactored `RedisCommandsQueue` to use a `#pushHandlers` array, enabling dynamic and modular handling of push notifications. - Updated `RedisClient` to leverage the new handler mechanism for `invalidate` push notifications, simplifying and decoupling logic. * feat(commands-queue): Add method to wait for in-flight commands to complete - Introduced `waitForInflightCommandsToComplete` method to asynchronously wait for all in-flight commands to finish processing. - Utilized the `empty` event from `#waitingForReply` to signal when all commands have been completed. * feat(commands-queue): Introduce maintenance mode support for commands-queue - Added `#maintenanceCommandTimeout` and `setMaintenanceCommandTimeout` method to dynamically adjust command timeouts during maintenance * refator(client): Extract socket event listener setup into helper method * refactor(socket): Add maintenance mode support and dynamic timeout handling - Added `#maintenanceTimeout` and `setMaintenanceTimeout` method to dynamically adjust socket timeouts during maintenance. * feat(client): Add Redis Enterprise maintenance configuration options - Added `maintPushNotifications` option to control how the client handles Redis Enterprise maintenance push notifications (`disabled`, `enabled`, `au to`). - Added `maintMovingEndpointType` option to specify the endpoint type for reconnecting during a MOVING notification (`auto`, `internal-ip`, `external-ip`, etc.). - Added `maintRelaxedCommandTimeout` option to define a relaxed timeout for commands during maintenance. - Added `maintRelaxedSocketTimeout` option to define a relaxed timeout for the socket during maintenance. - Enforced RESP3 requirement for maintenance-related features (`maintPushNotifications`). * feat(client): Add socket helpers and pause mechanism - Introduced `#paused` flag with corresponding `_pause` and `_unpause` methods to temporarily halt writing commands to the socket during maintenance windows. - Updated `#write` method to respect the `#paused` flag, preventing new commands from being written during maintenance. - Added `_ejectSocket` method to safely detach from and return the current socket - Added `_insertSocket` method to receive and start using a new socket * feat(client): Add Redis Enterprise maintenance handling capabilities - Introduced `EnterpriseMaintenanceManager` to manage Redis Enterprise maintenance events and push notifications. - Integrated `EnterpriseMaintenanceManager` into `RedisClient` to handle maintenance push notifications and manage socket transitions. - Implemented graceful handling of MOVING, MIGRATING, and FAILOVER push notifications, including socket replacement and timeout adjustments. * test: add E2E test infrastructure for Redis maintenance scenarios * test: add E2E tests for Redis Enterprise maintenance timeout handling (#3) * test: add connection handoff test --------- Co-authored-by: Pavel Pashov <pavel.pashov@redis.com> Co-authored-by: Pavel Pashov <60297174+PavelPashov@users.noreply.github.com>
@redis/search
This package provides support for the RediSearch module, which adds indexing and querying support for data stored in Redis Hashes or as JSON documents with the RedisJSON module.
Should be used with redis/@redis/client.
⚠️ To use these extra commands, your Redis server must have the RediSearch module installed. To index and query JSON documents, you'll also need to add the RedisJSON module.
Usage
For complete examples, see search-hashes.js and search-json.js in the examples folder.
Indexing and Querying Data in Redis Hashes
Creating an Index
Before we can perform any searches, we need to tell RediSearch how to index our data, and which Redis keys to find that data in. The FT.CREATE command creates a RediSearch index. Here's how to use it to create an index we'll call idx:animals where we want to index hashes containing name, species and age fields, and whose key names in Redis begin with the prefix noderedis:animals:
await client.ft.create('idx:animals', {
name: {
type: SCHEMA_FIELD_TYPE.TEXT,
SORTABLE: true
},
species: SCHEMA_FIELD_TYPE.TAG,
age: SCHEMA_FIELD_TYPE.NUMERIC
}, {
ON: 'HASH',
PREFIX: 'noderedis:animals'
});
See the FT.CREATE documentation for information about the different field types and additional options.
Querying the Index
Once we've created an index, and added some data to Redis hashes whose keys begin with the prefix noderedis:animals, we can start writing some search queries. RediSearch supports a rich query syntax for full-text search, faceted search, aggregation and more. Check out the FT.SEARCH documentation and the query syntax reference for more information.
Let's write a query to find all the animals where the species field has the value dog:
const results = await client.ft.search('idx:animals', '@species:{dog}');
results looks like this:
{
total: 2,
documents: [
{
id: 'noderedis:animals:4',
value: {
name: 'Fido',
species: 'dog',
age: '7'
}
},
{
id: 'noderedis:animals:3',
value: {
name: 'Rover',
species: 'dog',
age: '9'
}
}
]
}
Indexing and Querying Data with RedisJSON
RediSearch can also index and query JSON documents stored in Redis using the RedisJSON module. The approach is similar to that for indexing and searching data in hashes, but we can now use JSON Path like syntax and the data no longer has to be flat name/value pairs - it can contain nested objects and arrays.
Creating an Index
As before, we create an index with the FT.CREATE command, this time specifying we want to index JSON documents that look like this:
{
name: 'Alice',
age: 32,
coins: 100
}
Each document represents a user in some system, and users have name, age and coins properties.
One way we might choose to index these documents is as follows:
await client.ft.create('idx:users', {
'$.name': {
type: SCHEMA_FIELD_TYPE.TEXT,
SORTABLE: 'UNF'
},
'$.age': {
type: SCHEMA_FIELD_TYPE.NUMERIC,
AS: 'age'
},
'$.coins': {
type: SCHEMA_FIELD_TYPE.NUMERIC,
AS: 'coins'
}
}, {
ON: 'JSON',
PREFIX: 'noderedis:users'
});
Note that we're using JSON Path to specify where the fields to index are in our JSON documents, and the AS clause to define a name/alias for each field. We'll use these when writing queries.
Querying the Index
Now we have an index and some data stored as JSON documents in Redis (see the JSON package documentation for examples of how to store JSON), we can write some queries...
We'll use the RediSearch query language and FT.SEARCH command. Here's a query to find users under the age of 30:
await client.ft.search('idx:users', '@age:[0 30]');