-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
- 1) Many methods now is not available by old names (with
legacyMode
also too)
So, only way for now (without modifying thousands of files) is patch client object like this, or remap keys with .toLowerCase()
:
client.hgetall = client.hGetAll;
client.hget = client.hGet;
client.hset = client.hSet;
client.hmset = client.hSet;
client.setex = client.setEx;
...
- 2)
hSet
does not allow objects as second parameter anymore
client.hSet('key', {a: 1, b:2}, callback);
< ./node_modules/@node-redis/client/dist/lib/commander.js:72
yield `$${byteLength.toString()}${DELIMITER}`;
^
TypeError: Cannot read properties of undefined (reading 'toString')
at encodeCommand (./node_modules/@node-redis/client/dist/lib/commander.js:72:30)
at encodeCommand.next (<anonymous>)
at RedisSocket.writeCommand (./node_modules/@node-redis/client/dist/lib/client/socket.js:56:20)
at Commander._RedisClient_tick (./node_modules/@node-redis/client/dist/lib/client/index.js:415:64)
at Commander._RedisClient_sendCommand (./node_modules/@node-redis/client/dist/lib/client/index.js:396:82)
at Commander.sendCommand (./node_modules/@node-redis/client/dist/lib/client/index.js:349:93)
at Commander.<computed> (./node_modules/@node-redis/client/dist/lib/client/index.js:383:14)
But previously, on 3.x its works:
client.hmset('key', {a: 1, b: 2}, callback);
< HMSET key a 1 b 2
Functionality hiddenly broken, and no alternative provided.
Current solution is writing a wrap for hmset
like this:
client.hmset = (hkey, parms, callback) => {
client.hSet(hkey, Object.entries(parms).flat(), callback)
}
- 3) Connection errors did not crash process with uncatched errors anymore. Strange thing, but seems like something changed in error policy of this module. I haven't figured it out yet, but when connection is lost, algorithm simply stop on redis
commandSending
forever.
- 4) (Bonus) Types for
legacyMode
and callbacks is not provided, but in 3.x its does, and for 2.x too: https://www.npmjs.com/package/@types/redis,
Dirty way for now is patchnode_modules/@node-redis/client/dist/lib/client/index.d.ts
and add it toWithCommands
type
export declare type RedisClientCommandSignatureLegacy<C extends RedisCommand> = (...args: [...options: Parameters<C['transformArguments']>, callback: (e: Error | null, result: RedisCommandReply<C>) => void]) => void
Also, I think that users can additionally import something like LegacyTypesHelper
module that will override signatures until legacyMode enabled.
- 5) Importing of
{ RedisClient } from "redis"
now is not available anymore.
Current solution is craft it by yourself: (but it seems like a crutch)
import { createClient } from 'redis'
type RedisClient = ReturnType<typeof createClient>
But I think develops can provide more useful type with generics, but not delete it at all.
Many things, like cache adapters, models or abstract classes require that type. Also, will be useful, if develop may declare that
code expected pure RedisClient or RedisClient with RediJson module.
Anyway it should be described in migration guide.
-
6) Another thing for the migration guide. SCAN args have changed, requiring
client.SCAN(cursor, { MATCH: keyPattern, COUNT: pageSize })
pageSize is a Number. It used to takeclient.SCAN(cursorString, 'MATCH', keyPattern, 'COUNT', pageSize.toString()).
And the response has changed. Instead of an array of strings
[<cursor>, <keys>],
it now returns a structure{cursor: <number>, keys: <keys>}
. (thx for reporting @jimsnab)
Finally, I should say that is very sad example of "how modules shouldn't make breaking updates", especially of so popular module. Feels like a spit into consumers.