diff --git a/README.md b/README.md index 4fca8d405d..80a34b7552 100644 --- a/README.md +++ b/README.md @@ -92,8 +92,11 @@ Modifiers to commands are specified using a JavaScript object: ```typescript await client.set('key', 'value', { - EX: 10, - NX: true + expiration: { + type: 'EX', + value: 10 + }, + condition: 'NX' }); ``` @@ -108,10 +111,9 @@ await client.hVals('key'); // ['value1', 'value2'] ```typescript await client.hSet('key', 'field', Buffer.from('value')); // 'OK' -await client.hGetAll( - commandOptions({ returnBuffers: true }), - 'key' -); // { field: } +await client.withFlags({ + [TYPES.BLOB_STRING]: Buffer +}).hGetAll('key'); // { field: } ``` ### Unsupported Redis Commands @@ -151,8 +153,7 @@ This pattern works especially well for blocking commands—such as `BLPOP` and ` ```typescript import { commandOptions } from 'redis'; -const blPopPromise = client.blPop( - commandOptions({ isolated: true }), +const blPopPromise = client.isolated().blPop( 'key', 0 ); diff --git a/packages/client/lib/commands/SET.ts b/packages/client/lib/commands/SET.ts index bb39bd8960..3def49d604 100644 --- a/packages/client/lib/commands/SET.ts +++ b/packages/client/lib/commands/SET.ts @@ -1,27 +1,46 @@ import { RedisArgument, SimpleStringReply, BlobStringReply, NullReply, Command } from '../RESP/types'; -type MaximumOneOf = - K extends keyof T ? { [P in K]?: T[K] } & Partial, never>> : never; +interface SetOptions { + 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<{ - EX: number; - PX: number; - EXAT: number; - PXAT: number; - KEEPTTL: true; -}>; - -type SetGuards = MaximumOneOf<{ - NX: true; - XX: true; -}>; - -interface SetCommonOptions { - GET?: true; + condition?: 'NX' | 'XX'; + /** + * @deprecated Use `condition` 'NX' instead + */ + NX?: boolean; + /** + * @deprecated Use `condition` 'XX' instead + */ + XX?: boolean; + + GET?: boolean; } -export type SetOptions = SetTTL & SetGuards & SetCommonOptions; - export default { FIRST_KEY_INDEX: 1, transformArguments(key: RedisArgument, value: RedisArgument | number, options?: SetOptions) { @@ -31,7 +50,18 @@ export default { 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()); } else if (options?.PX !== undefined) { args.push('PX', options.PX.toString()); @@ -43,7 +73,9 @@ export default { args.push('KEEPTTL'); } - if (options?.NX) { + if (options?.condition) { + args.push(options.condition); + } else if (options?.NX) { args.push('NX'); } else if (options?.XX) { args.push('XX');