Skip to content

Commit

Permalink
Node: Add FLUSHDB command. (valkey-io#1986)
Browse files Browse the repository at this point in the history
* Add `FLUSHDB` command.

Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand authored Jul 19, 2024
1 parent 2ecad75 commit 05bb0ae
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 2 additions & 0 deletions node/npm/glide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function initialize() {
Logger,
LPosOptions,
ExpireOptions,
FlushMode,
InfoOptions,
InsertPosition,
SetOptions,
Expand Down Expand Up @@ -133,6 +134,7 @@ function initialize() {
Logger,
LPosOptions,
ExpireOptions,
FlushMode,
InfoOptions,
InsertPosition,
SetOptions,
Expand Down
28 changes: 11 additions & 17 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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, []);
}
}

Expand Down
30 changes: 22 additions & 8 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ReturnType,
} from "./BaseClient";
import {
FlushMode,
InfoOptions,
LolwutOptions,
createClientGetName,
Expand All @@ -22,8 +21,9 @@ import {
createCustomCommand,
createDBSize,
createEcho,
createFunctionLoad,
createFlushAll,
createFlushDB,
createFunctionLoad,
createInfo,
createLolwut,
createPing,
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
*
Expand All @@ -430,11 +430,25 @@ export class GlideClient extends BaseClient {
* ```
*/
public flushall(mode?: FlushMode): Promise<string> {
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<string> {
return this.createWritePromise(createFlushDB(mode));
}

/**
Expand Down
32 changes: 28 additions & 4 deletions node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ReturnType,
} from "./BaseClient";
import {
FlushMode,
InfoOptions,
LolwutOptions,
createClientGetName,
Expand All @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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<string> {
return this.createWritePromise(
createFlushDB(mode),
toProtobufRoute(route),
);
}

/**
* Returns the number of keys in the database.
*
Expand Down
27 changes: 21 additions & 6 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -38,6 +36,9 @@ import {
createExpire,
createExpireAt,
createFlushAll,
createFlushDB,
createFunctionLoad,
createGeoAdd,
createGet,
createGetDel,
createHDel,
Expand Down Expand Up @@ -89,20 +90,20 @@ import {
createSCard,
createSDiff,
createSDiffStore,
createSetBit,
createSInter,
createSInterCard,
createSInterStore,
createSIsMember,
createSMembers,
createSMIsMember,
createSMembers,
createSMove,
createSPop,
createSRem,
createSUnion,
createSUnionStore,
createSelect,
createSet,
createSetBit,
createStrlen,
createTTL,
createTime,
Expand All @@ -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";

Expand Down Expand Up @@ -1840,12 +1841,26 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* 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
Expand Down
23 changes: 23 additions & 0 deletions node/src/commands/FlushMode.ts
Original file line number Diff line number Diff line change
@@ -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",
}
18 changes: 11 additions & 7 deletions node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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);
},
);

Expand Down
Loading

0 comments on commit 05bb0ae

Please sign in to comment.