Comments (10)
I think that's reasonable. Now we just need a name. But that's a problem for Future Leibale. And probably Future Guy. ;)
from node-redis.
- until we fix it you can use
client.sendCommand
to workaround it:
const reply = await client.sendCommand(['ZADD', 'key', 'NX', '1', 'member']);
- there are 2 ways to fix it:
- changing ZADD type to always return
number | null
(which is not true in the "simple case" of ZADD). - remove support for NX, XX, LT, and GT in the main ZADD command, keep the return type
number
, createZADD_CONDITINAL
(have a better name?) which will support all modifiers and have a return type ofnumber | null
.
- changing ZADD type to always return
Which one is better in your opinion? (@guyroyse @sjpotter)
from node-redis.
I'm leaning toward option 1. But I am concerned that either change could break existing code.
from node-redis.
Option 1 can be considered a bug fix, option 2 is a breaking change for sure. Should we do 1 for v4, and 2 for v5?
from node-redis.
I would oppose option 2 because I don't see a reason why ZADD should be treated any different from most other commands. This would need specific documentation just for ZADD. That is reasonable for things that are more complex like scan or transactions but not for ZADD in my opinion.
from node-redis.
@puchm the main reason is to simplify the "main case", i.e:
const reply = await client.zAdd('key', {
member: 'a',
score: 1
});
if NX, XX, LT, and GT are supported in the main ZADD, the type of reply
will be number | null
, which is "not true" because it'll never be a null
unless you use one of NX XX LT or GT. By separating it into multiple commands we can have different return types for different cases (i.e. number
for the "normal case", and number | null
for the "conditional" case).
from node-redis.
there are 2 ways to fix it:
changing ZADD type to always return number | null (which is not true in the "simple case" of ZADD).
remove support for NX, XX, LT, and GT in the main ZADD command, keep the return type number, create ZADD_CONDITINAL (have a better name?) which will support all modifiers and have a return type of number | null.
I prefer the latter, as it makes the return type obvious. I dislike return types that are not possible, but now one has to code around them due to that.
from node-redis.
@leibale I guess the optimal way would be to use the power of Typescript to make the return type dynamic based on what is passed in, but I don't think that's possible with the current architecture. Still, the number of methods you have where the name is different from the actual Redis command is really low and should probably stay that way. I'm wondering if there are other cases where some return types are just not possible depending on what is passed in?
I.e. it looks to me that there is a similar situation with SET. If neither GET nor XX nor NX are given the return type of SET will never be null. How do you handle that?
from node-redis.
The "optimal way" would be to use TypeScript generics for that, but we are already "abusing" them for other things, which makes that very hard. This is the best option I found, but I hate it...
interface Command {
transformArguments(...args: Array<unknown>): unknown;
}
function transformArguments(key: string, options?: { NUMBER: false; }): string;
function transformArguments(key: string, options?: { NUMBER: true; }): number;
function transformArguments(key: string, options?: { NUMBER: boolean; }) {
if (options?.NUMBER) {
return ['GET', key, 'NUMBER'] as never;
}
return ['GET', key] as never;
}
const GET = {
transformArguments
} satisfies Command;
const COMMANDS = {
GET,
get: GET
};
type COMMANDS = typeof COMMANDS;
type RedisClient = {
[COMMAND in keyof COMMANDS]: COMMANDS[COMMAND]['transformArguments'];
};
const client = undefined as unknown as RedisClient;
const a = client.get('key');
const b = client.get('key', { NUMBER: true });
if you have any ideas please share.. ;)
In SET we didn't do it since in the "normal case" the reply does not matter, but we did it in other commands (for example, XAUTOCLAIM_JUSTID).
from node-redis.
Having exactly this issue as well with the null reply screwing things up ...
from node-redis.
Related Issues (20)
- Quit is not getting closed HOT 3
- Detect pub sub subscription status/connection on the cluster client?
- FT.AGGREGATE _ LOAD * is not supported
- Is there a read from option in redis-cluster? HOT 2
- Successfully connected to Redis server, but did not trigger connect, ready event callback HOT 1
- sRandMember seems to not support the count argument HOT 2
- Unable to connect to literal IPv6 addresses via the `url` option
- [docs] The `sharded-channel-moved` redirection failed. HOT 1
- calling aclGetUser on none existing user throws a null error
- No way to quit a BLOCKed connection? HOT 2
- Not able to work with binary data (gzip) HOT 1
- arbitrary code execution when compiling specifically crafted malicious code
- hmset() : redisClient.hmset is not a function HOT 1
- ft.aggregate does not aggregate when filtering tags
- How to handle Bigint cursor? HOT 3
- FT.SEARCH: Cannot read properties of undefined (reading 'length') HOT 2
- The issue does not belong to node-redis repo but sharing it here to get recommendations as ioredis is deprecated
- Allow `password` to take in a generator/callback to support temporary passwords that must be refreshed. HOT 3
- Not able to see node js as code snippets HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-redis.