1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00
Files
node-redis/docs/v4-to-v5.md
Leibale 13f1fa9e58 WIP
2023-05-10 14:55:03 +03:00

4.7 KiB

v4 to v5 migration guide

Command Options

In v4, command options are passed as a first optional argument:

await client.get('key'); // `string | null`
await client.get(client.commandOptions({ returnBuffers: true }), 'key'); // `Buffer | null`

This has a couple of flaws:

  1. The argument types are checked in runtime, which is a performance hit.
  2. Code suggestions are less readable/usable, due to "function overloading".
  3. Overall, "user code" is not as readable as it could be.

The new API for v5

With the new API, instead of passing the options directly to the commands we use a "proxy client" to store them:

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

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

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

withCommandOptions can be used to override all of the command options, without reusing any existing ones.

To override just a specific option, use the following functions:

  • withFlags - override flags only.
  • asap - override asap to true.
  • isolated - override isolated to true.

Quit VS Disconnect

The QUIT command has been deprecated in Redis 7.2 and should now also be considered deprecated in Node-Redis. Instead of sending a QUIT command to the server, the client can simply close the network connection.

Rather than using client.quit(), your code should use client.close() or client.disconnect().

TODO difference between close and disconnect...

Scan Iterators

TODO Yields chunks instead of individual items. Allows multi key operations. See the Scan Iterators guide.

Legacy Mode

TODO

const client = createClient(),
  legacyClient = client.legacy();

// use `client` for the new API
await client.set('key', 'value');

// use `legacyClient` for the "legacy" callback API
legacyClient.set('key', 'value', (err, reply) => {
  // ...
});

Isolation Pool

TODO The isolationPool has been moved to it's on class ClientPool. You can create pool from a client using client.createPool().

Commands

Some command arguments/replies have changed to align more closely to data types returned by Redis:

  • ACL GETUSER: selectors
  • CLIENT KILL: enum ClientKillFilters -> const CLIENT_KILL_FILTERS 1
  • CLUSTER FAILOVER: enum FailoverModes -> const FAILOVER_MODES 1
  • LCS IDX: length has been changed to len, matches has been changed from Array<{ key1: RangeReply; key2: RangeReply; }> to Array<[key1: RangeReply, key2: RangeReply]>
  • HEXISTS: boolean -> number 2
  • HRANDFIELD_COUNT_WITHVALUES: Record<BlobString, BlobString> -> Array<{ field: BlobString; value: BlobString; }> (it can return duplicates).
  • SCAN, HSCAN, SSCAN, and ZSCAN: cursor type is string instead of number?
  • HSETNX: boolean -> number 2
  • ZINTER: instead of client.ZINTER('key', { WEIGHTS: [1] }) use client.ZINTER({ key: 'key', weight: 1 }])
  • ZINTER_WITHSCORES: instead of client.ZINTER_WITHSCORES('key', { WEIGHTS: [1] }) use client.ZINTER_WITHSCORES({ key: 'key', weight: 1 }])
  • ZUNION: instead of client.ZUNION('key', { WEIGHTS: [1] }) use client.ZUNION({ key: 'key', weight: 1 }])
  • ZUNION_WITHSCORES: instead of client.ZUNION_WITHSCORES('key', { WEIGHTS: [1] }) use client.ZUNION_WITHSCORES({ key: 'key', weight: 1 }])
  • SETNX: boolean -> number 2
  • COPY: destinationDb -> DB, replace -> REPLACE, boolean -> number 2
  • EXPIRE: boolean -> number 2
  • EXPIREAT: boolean -> number 2
  • MOVE: boolean -> number 2
  • PEXPIRE: boolean -> number 2
  • PEXPIREAT: boolean -> number 2
  • RENAMENX: boolean -> number 2
  • HSCAN: tuples has been renamed to entries
  • PFADD: boolean -> number 2
  • SCRIPT EXISTS: Array<boolean> -> Array<number> 2
  • SISMEMBER: boolean -> number 2
  • SMISMEMBER: Array<boolean> -> Array<number> 2
  • SMOVE: boolean -> number 2
  • TS.ADD: boolean -> number 2
  • GEOSEARCH_WITH/GEORADIUS_WITH: GeoReplyWith -> GEO_REPLY_WITH 1
  • GEORADIUSSTORE -> GEORADIUS_STORE
  • GEORADIUSBYMEMBERSTORE -> GEORADIUSBYMEMBER_STORE
  • XACK: boolean -> number 2
  • XADD: the INCR option has been removed, use XADD_INCR instead

  1. TODO ↩︎

  2. TODO ↩︎