1
0
mirror of https://github.com/redis/node-redis.git synced 2025-07-31 05:44:24 +03:00
Files
node-redis/docs/command-options.md
Shaya Potter b2d35c5286 V5 bringing RESP3, Sentinel and TypeMapping to node-redis
RESP3 Support
   - Some commands responses in RESP3 aren't stable yet and therefore return an "untyped" ReplyUnion.
 
Sentinel

TypeMapping

Correctly types Multi commands

Note: some API changes to be further documented in v4-to-v5.md
2024-10-15 17:46:52 +03:00

1.9 KiB

Command Options

⚠️ The command options API in v5 has breaking changes from the previous version. For more details, refer to the v4-to-v5 guide.

Command Options are used to create "proxy clients" that change the behavior of executed commands. See the sections below for details.

Type Mapping

Some RESP types can be mapped to more than one JavaScript type. For example, "Blob String" can be mapped to string or Buffer. You can override the default type mapping using the withTypeMapping function:

await client.get('key'); // `string | null`

const proxyClient = client.withTypeMapping({
  [TYPES.BLOB_STRING]: Buffer
});

await proxyClient.get('key'); // `Buffer | null`

See RESP for a full list of types.

Abort Signal

The client batches commands before sending them to Redis. Commands that haven't been written to the socket yet can be aborted using the AbortSignal API:

const controller = new AbortController(),
  client = client.withAbortSignal(controller.signal);

try {
  const promise = client.get('key');
  controller.abort();
  await promise;
} catch (err) {
  // AbortError
}

ASAP

Commands that are executed in the "asap" mode are added to the beginning of the "to sent" queue.

const asapClient = client.asap();
await asapClient.ping();

withCommandOptions

You can set all of the above command options in a single call with the withCommandOptions function:

client.withCommandOptions({
  typeMapping: ...,
  abortSignal: ...,
  asap: ...
});

If any of the above options are omitted, the default value will be used. For example, the following client would not be in ASAP mode:

client.asap().withCommandOptions({
  typeMapping: ...,
  abortSignal: ...
});