1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

clean SET command

This commit is contained in:
Leibale
2023-04-24 12:40:22 -04:00
parent 40cfb94ca2
commit 39bcac6484
2 changed files with 62 additions and 29 deletions

View File

@@ -92,8 +92,11 @@ Modifiers to commands are specified using a JavaScript object:
```typescript ```typescript
await client.set('key', 'value', { await client.set('key', 'value', {
EX: 10, expiration: {
NX: true type: 'EX',
value: 10
},
condition: 'NX'
}); });
``` ```
@@ -108,10 +111,9 @@ await client.hVals('key'); // ['value1', 'value2']
```typescript ```typescript
await client.hSet('key', 'field', Buffer.from('value')); // 'OK' await client.hSet('key', 'field', Buffer.from('value')); // 'OK'
await client.hGetAll( await client.withFlags({
commandOptions({ returnBuffers: true }), [TYPES.BLOB_STRING]: Buffer
'key' }).hGetAll('key'); // { field: <Buffer 76 61 6c 75 65> }
); // { field: <Buffer 76 61 6c 75 65> }
``` ```
### Unsupported Redis Commands ### Unsupported Redis Commands
@@ -151,8 +153,7 @@ This pattern works especially well for blocking commands—such as `BLPOP` and `
```typescript ```typescript
import { commandOptions } from 'redis'; import { commandOptions } from 'redis';
const blPopPromise = client.blPop( const blPopPromise = client.isolated().blPop(
commandOptions({ isolated: true }),
'key', 'key',
0 0
); );

View File

@@ -1,27 +1,46 @@
import { RedisArgument, SimpleStringReply, BlobStringReply, NullReply, Command } from '../RESP/types'; import { RedisArgument, SimpleStringReply, BlobStringReply, NullReply, Command } from '../RESP/types';
type MaximumOneOf<T, K extends keyof T = keyof T> = interface SetOptions {
K extends keyof T ? { [P in K]?: T[K] } & Partial<Record<Exclude<keyof T, K>, never>> : never; expiration?: {
type: 'EX' | 'PX' | 'EXAT' | 'PXAT';
value: number;
} | {
type: 'KEEPTTL';
} | 'KEEPTTL';
/**
* @deprecated Use `expiration` { type: 'EX', value: number } instead
*/
EX?: number;
/**
* @deprecated Use `expiration` { type: 'PX', value: number } instead
*/
PX?: number;
/**
* @deprecated Use `expiration` { type: 'EXAT', value: number } instead
*/
EXAT?: number;
/**
* @deprecated Use `expiration` { type: 'PXAT', value: number } instead
*/
PXAT?: number;
/**
* @deprecated Use `expiration` 'KEEPTTL' instead
*/
KEEPTTL?: boolean;
type SetTTL = MaximumOneOf<{ condition?: 'NX' | 'XX';
EX: number; /**
PX: number; * @deprecated Use `condition` 'NX' instead
EXAT: number; */
PXAT: number; NX?: boolean;
KEEPTTL: true; /**
}>; * @deprecated Use `condition` 'XX' instead
*/
type SetGuards = MaximumOneOf<{ XX?: boolean;
NX: true;
XX: true; GET?: boolean;
}>;
interface SetCommonOptions {
GET?: true;
} }
export type SetOptions = SetTTL & SetGuards & SetCommonOptions;
export default { export default {
FIRST_KEY_INDEX: 1, FIRST_KEY_INDEX: 1,
transformArguments(key: RedisArgument, value: RedisArgument | number, options?: SetOptions) { transformArguments(key: RedisArgument, value: RedisArgument | number, options?: SetOptions) {
@@ -31,7 +50,18 @@ export default {
typeof value === 'number' ? value.toString() : value typeof value === 'number' ? value.toString() : value
]; ];
if (options?.EX !== undefined) { if (options?.expiration) {
if (typeof options.expiration === 'string') {
args.push(options.expiration);
} else if (options.expiration.type === 'KEEPTTL') {
args.push('KEEPTTL');
} else {
args.push(
options.expiration.type,
options.expiration.value.toString()
);
}
} else if (options?.EX !== undefined) {
args.push('EX', options.EX.toString()); args.push('EX', options.EX.toString());
} else if (options?.PX !== undefined) { } else if (options?.PX !== undefined) {
args.push('PX', options.PX.toString()); args.push('PX', options.PX.toString());
@@ -43,7 +73,9 @@ export default {
args.push('KEEPTTL'); args.push('KEEPTTL');
} }
if (options?.NX) { if (options?.condition) {
args.push(options.condition);
} else if (options?.NX) {
args.push('NX'); args.push('NX');
} else if (options?.XX) { } else if (options?.XX) {
args.push('XX'); args.push('XX');