From 05bb0aed2e4d1fecc82114b64d0846fa11c93cbf Mon Sep 17 00:00:00 2001 From: Yury-Fridlyand Date: Fri, 19 Jul 2024 15:27:40 -0700 Subject: [PATCH] Node: Add `FLUSHDB` command. (#1986) * Add `FLUSHDB` command. Signed-off-by: Yury-Fridlyand --- CHANGELOG.md | 1 + node/npm/glide/index.ts | 2 ++ node/src/Commands.ts | 28 +++++++++-------------- node/src/GlideClient.ts | 30 ++++++++++++++++++------- node/src/GlideClusterClient.ts | 32 +++++++++++++++++++++++---- node/src/Transaction.ts | 27 +++++++++++++++++----- node/src/commands/FlushMode.ts | 23 +++++++++++++++++++ node/tests/RedisClient.test.ts | 18 +++++++++------ node/tests/RedisClusterClient.test.ts | 29 ++++++++++++++++++++++++ node/tests/SharedTests.ts | 2 +- node/tests/TestUtilities.ts | 7 ++++++ 11 files changed, 156 insertions(+), 43 deletions(-) create mode 100644 node/src/commands/FlushMode.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f58d85d0f6..7551169053 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ #### Changes +* Node: Added FLUSHDB command ([#1986](https://github.com/valkey-io/valkey-glide/pull/1986)) * Node: Added GETDEL command ([#1968](https://github.com/valkey-io/valkey-glide/pull/1968)) * Node: Added SETBIT command ([#1978](https://github.com/valkey-io/valkey-glide/pull/1978)) * Node: Added LPUSHX and RPUSHX command([#1959](https://github.com/valkey-io/valkey-glide/pull/1959)) diff --git a/node/npm/glide/index.ts b/node/npm/glide/index.ts index cc181ad00e..d3a2d6af84 100644 --- a/node/npm/glide/index.ts +++ b/node/npm/glide/index.ts @@ -87,6 +87,7 @@ function initialize() { Logger, LPosOptions, ExpireOptions, + FlushMode, InfoOptions, InsertPosition, SetOptions, @@ -133,6 +134,7 @@ function initialize() { Logger, LPosOptions, ExpireOptions, + FlushMode, InfoOptions, InsertPosition, SetOptions, diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 78af65bd62..73bf204452 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -5,6 +5,7 @@ import { createLeakedStringVec, MAX_REQUEST_ARGS_LEN } from "glide-rs"; import Long from "long"; import { LPosOptions } from "./commands/LPosOptions"; +import { FlushMode } from "./commands/FlushMode"; import { command_request } from "./ProtobufMessage"; import { GeospatialData } from "./commands/geospatial/GeospatialData"; @@ -1729,31 +1730,24 @@ export function createLolwut(options?: LolwutOptions): command_request.Command { } /** - * Defines flushing mode for: - * - * `FLUSHALL` command. - * - * See https://valkey.io/commands/flushall/ for details. + * @internal */ -export enum FlushMode { - /** - * Flushes synchronously. - * - * since Valkey 6.2 and above. - */ - SYNC = "SYNC", - /** Flushes asynchronously. */ - ASYNC = "ASYNC", +export function createFlushAll(mode?: FlushMode): command_request.Command { + if (mode) { + return createCommand(RequestType.FlushAll, [mode.toString()]); + } else { + return createCommand(RequestType.FlushAll, []); + } } /** * @internal */ -export function createFlushAll(mode?: FlushMode): command_request.Command { +export function createFlushDB(mode?: FlushMode): command_request.Command { if (mode) { - return createCommand(RequestType.FlushAll, [mode.toString()]); + return createCommand(RequestType.FlushDB, [mode.toString()]); } else { - return createCommand(RequestType.FlushAll, []); + return createCommand(RequestType.FlushDB, []); } } diff --git a/node/src/GlideClient.ts b/node/src/GlideClient.ts index 652a6a74bb..0ca4b477b7 100644 --- a/node/src/GlideClient.ts +++ b/node/src/GlideClient.ts @@ -10,7 +10,6 @@ import { ReturnType, } from "./BaseClient"; import { - FlushMode, InfoOptions, LolwutOptions, createClientGetName, @@ -22,8 +21,9 @@ import { createCustomCommand, createDBSize, createEcho, - createFunctionLoad, createFlushAll, + createFlushDB, + createFunctionLoad, createInfo, createLolwut, createPing, @@ -33,6 +33,7 @@ import { } from "./Commands"; import { connection_request } from "./ProtobufMessage"; import { Transaction } from "./Transaction"; +import { FlushMode } from "./commands/FlushMode"; /* eslint-disable-next-line @typescript-eslint/no-namespace */ export namespace GlideClientConfiguration { @@ -416,7 +417,6 @@ export class GlideClient extends BaseClient { /** * Deletes all the keys of all the existing databases. This command never fails. - * The command will be routed to all primary nodes. * * See https://valkey.io/commands/flushall/ for more details. * @@ -430,11 +430,25 @@ export class GlideClient extends BaseClient { * ``` */ public flushall(mode?: FlushMode): Promise { - if (mode) { - return this.createWritePromise(createFlushAll(mode)); - } else { - return this.createWritePromise(createFlushAll()); - } + return this.createWritePromise(createFlushAll(mode)); + } + + /** + * Deletes all the keys of the currently selected database. This command never fails. + * + * See https://valkey.io/commands/flushdb/ for more details. + * + * @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}. + * @returns `OK`. + * + * @example + * ```typescript + * const result = await client.flushdb(FlushMode.SYNC); + * console.log(result); // Output: 'OK' + * ``` + */ + public flushdb(mode?: FlushMode): Promise { + return this.createWritePromise(createFlushDB(mode)); } /** diff --git a/node/src/GlideClusterClient.ts b/node/src/GlideClusterClient.ts index 9a6d334b6f..24f7e48579 100644 --- a/node/src/GlideClusterClient.ts +++ b/node/src/GlideClusterClient.ts @@ -10,7 +10,6 @@ import { ReturnType, } from "./BaseClient"; import { - FlushMode, InfoOptions, LolwutOptions, createClientGetName, @@ -22,14 +21,16 @@ import { createCustomCommand, createDBSize, createEcho, - createFunctionLoad, createFlushAll, + createFlushDB, + createFunctionLoad, createInfo, createLolwut, createPing, createPublish, createTime, } from "./Commands"; +import { FlushMode } from "./commands/FlushMode"; import { RequestError } from "./Errors"; import { command_request, connection_request } from "./ProtobufMessage"; import { ClusterTransaction } from "./Transaction"; @@ -694,8 +695,8 @@ export class GlideClusterClient extends BaseClient { * See https://valkey.io/commands/flushall/ for more details. * * @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}. - * @param route - The command will be routed to all primaries, unless `route` is provided, in which - * case the client will route the command to the nodes defined by `route`. + * @param route - The command will be routed to all primary nodes, unless `route` is provided, in which + * case the client will route the command to the nodes defined by `route`. * @returns `OK`. * * @example @@ -711,6 +712,29 @@ export class GlideClusterClient extends BaseClient { ); } + /** + * Deletes all the keys of the currently selected database. This command never fails. + * + * See https://valkey.io/commands/flushdb/ for more details. + * + * @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}. + * @param route - The command will be routed to all primary nodes, unless `route` is provided, in which + * case the client will route the command to the nodes defined by `route`. + * @returns `OK`. + * + * @example + * ```typescript + * const result = await client.flushdb(FlushMode.SYNC); + * console.log(result); // Output: 'OK' + * ``` + */ + public flushdb(mode?: FlushMode, route?: Routes): Promise { + return this.createWritePromise( + createFlushDB(mode), + toProtobufRoute(route), + ); + } + /** * Returns the number of keys in the database. * diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 1192bb052f..3c94a761f8 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -2,11 +2,9 @@ * Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */ -import { LPosOptions } from "./commands/LPosOptions"; import { AggregationType, ExpireOptions, - FlushMode, InfoOptions, InsertPosition, KeyWeight, @@ -38,6 +36,9 @@ import { createExpire, createExpireAt, createFlushAll, + createFlushDB, + createFunctionLoad, + createGeoAdd, createGet, createGetDel, createHDel, @@ -89,13 +90,12 @@ import { createSCard, createSDiff, createSDiffStore, - createSetBit, createSInter, createSInterCard, createSInterStore, createSIsMember, - createSMembers, createSMIsMember, + createSMembers, createSMove, createSPop, createSRem, @@ -103,6 +103,7 @@ import { createSUnionStore, createSelect, createSet, + createSetBit, createStrlen, createTTL, createTime, @@ -129,12 +130,12 @@ import { createZRemRangeByRank, createZRemRangeByScore, createZScore, - createGeoAdd, createZRevRank, createZRevRankWithScore, - createFunctionLoad, } from "./Commands"; import { command_request } from "./ProtobufMessage"; +import { FlushMode } from "./commands/FlushMode"; +import { LPosOptions } from "./commands/LPosOptions"; import { GeoAddOptions } from "./commands/geospatial/GeoAddOptions"; import { GeospatialData } from "./commands/geospatial/GeospatialData"; @@ -1840,12 +1841,26 @@ export class BaseTransaction> { * See https://valkey.io/commands/flushall/ for more details. * * @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}. + * * Command Response - `OK`. */ public flushall(mode?: FlushMode): T { return this.addAndReturn(createFlushAll(mode)); } + /** + * Deletes all the keys of the currently selected database. This command never fails. + * + * See https://valkey.io/commands/flushdb/ for more details. + * + * @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}. + * + * Command Response - `OK`. + */ + public flushdb(mode?: FlushMode): T { + return this.addAndReturn(createFlushDB(mode)); + } + /** * Returns the index of the first occurrence of `element` inside the list specified by `key`. If no * match is found, `null` is returned. If the `count` option is specified, then the function returns diff --git a/node/src/commands/FlushMode.ts b/node/src/commands/FlushMode.ts new file mode 100644 index 0000000000..78b9ca10c0 --- /dev/null +++ b/node/src/commands/FlushMode.ts @@ -0,0 +1,23 @@ +/** + * Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 + */ + +// Import below added to fix up the TSdoc link, but eslint blames for unused import. +/* eslint-disable-next-line @typescript-eslint/no-unused-vars */ +import { GlideClient } from "src/GlideClient"; + +/** + * Defines flushing mode for {@link GlideClient.flushall|flushall} and {@link GlideClient.flushdb|flushdb} commands. + * + * See https://valkey.io/commands/flushall/ and https://valkey.io/commands/flushdb/ for details. + */ +export enum FlushMode { + /** + * Flushes synchronously. + * + * since Valkey version 6.2.0. + */ + SYNC = "SYNC", + /** Flushes asynchronously. */ + ASYNC = "ASYNC", +} diff --git a/node/tests/RedisClient.test.ts b/node/tests/RedisClient.test.ts index 0b0b844813..b97d87f7b4 100644 --- a/node/tests/RedisClient.test.ts +++ b/node/tests/RedisClient.test.ts @@ -33,6 +33,7 @@ import { parseEndpoints, transactionTest, } from "./TestUtilities"; +import { FlushMode } from "../build-ts/src/commands/FlushMode.js"; /* eslint-disable @typescript-eslint/no-var-requires */ @@ -130,26 +131,29 @@ describe("GlideClient", () => { ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( - "simple select test", + "select dbsize flushdb test %p", async (protocol) => { client = await GlideClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); - let selectResult = await client.select(0); - checkSimple(selectResult).toEqual("OK"); + checkSimple(await client.select(0)).toEqual("OK"); const key = uuidv4(); const value = uuidv4(); const result = await client.set(key, value); checkSimple(result).toEqual("OK"); - selectResult = await client.select(1); - checkSimple(selectResult).toEqual("OK"); + checkSimple(await client.select(1)).toEqual("OK"); expect(await client.get(key)).toEqual(null); + checkSimple(await client.flushdb()).toEqual("OK"); + expect(await client.dbsize()).toEqual(0); - selectResult = await client.select(0); - checkSimple(selectResult).toEqual("OK"); + checkSimple(await client.select(0)).toEqual("OK"); checkSimple(await client.get(key)).toEqual(value); + + expect(await client.dbsize()).toBeGreaterThan(0); + checkSimple(await client.flushdb(FlushMode.SYNC)).toEqual("OK"); + expect(await client.dbsize()).toEqual(0); }, ); diff --git a/node/tests/RedisClusterClient.test.ts b/node/tests/RedisClusterClient.test.ts index bec58865ea..1158bd1513 100644 --- a/node/tests/RedisClusterClient.test.ts +++ b/node/tests/RedisClusterClient.test.ts @@ -35,6 +35,7 @@ import { parseEndpoints, transactionTest, } from "./TestUtilities"; +import { FlushMode } from "../build-ts/src/commands/FlushMode"; type Context = { client: GlideClusterClient; }; @@ -524,6 +525,34 @@ describe("GlideClusterClient", () => { TIMEOUT, ); + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( + "flushdb flushall dbsize test_%p", + async (protocol) => { + const client = await GlideClusterClient.createClient( + getClientConfigurationOption(cluster.getAddresses(), protocol), + ); + + expect(await client.dbsize()).toBeGreaterThanOrEqual(0); + checkSimple(await client.set(uuidv4(), uuidv4())).toEqual("OK"); + expect(await client.dbsize()).toBeGreaterThan(0); + + checkSimple(await client.flushall()).toEqual("OK"); + expect(await client.dbsize()).toEqual(0); + + checkSimple(await client.set(uuidv4(), uuidv4())).toEqual("OK"); + expect(await client.dbsize()).toEqual(1); + checkSimple(await client.flushdb(FlushMode.ASYNC)).toEqual("OK"); + expect(await client.dbsize()).toEqual(0); + + checkSimple(await client.set(uuidv4(), uuidv4())).toEqual("OK"); + expect(await client.dbsize()).toEqual(1); + checkSimple(await client.flushdb(FlushMode.SYNC)).toEqual("OK"); + expect(await client.dbsize()).toEqual(0); + + client.close(); + }, + ); + describe.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "Protocol is RESP2 = %s", (protocol) => { diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index d646835084..8ef475b5cf 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -8,7 +8,6 @@ import { v4 as uuidv4 } from "uuid"; import { ClosingError, ExpireOptions, - FlushMode, GlideClient, GlideClusterClient, InfoOptions, @@ -32,6 +31,7 @@ import { LPosOptions } from "../build-ts/src/commands/LPosOptions"; import { GeospatialData } from "../build-ts/src/commands/geospatial/GeospatialData"; import { GeoAddOptions } from "../build-ts/src/commands/geospatial/GeoAddOptions"; import { ConditionalChange } from "../build-ts/src/commands/ConditionalChange"; +import { FlushMode } from "../build-ts/src/commands/FlushMode"; async function getVersion(): Promise<[number, number, number]> { const versionString = await new Promise((resolve, reject) => { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index 2c508cc2ca..3af1e1dfb2 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -21,6 +21,7 @@ import { import { checkIfServerVersionLessThan } from "./SharedTests"; import { LPosOptions } from "../build-ts/src/commands/LPosOptions"; import { GeospatialData } from "../build-ts/src/commands/geospatial/GeospatialData"; +import { FlushMode } from "../build-ts/src/commands/FlushMode"; beforeAll(() => { Logger.init("info"); @@ -358,6 +359,12 @@ export async function transactionTest( const args: ReturnType[] = []; baseTransaction.flushall(); args.push("OK"); + baseTransaction.flushall(FlushMode.SYNC); + args.push("OK"); + baseTransaction.flushdb(); + args.push("OK"); + baseTransaction.flushdb(FlushMode.SYNC); + args.push("OK"); baseTransaction.dbsize(); args.push(0); baseTransaction.set(key1, "bar");