Skip to content

Commit d563e4f

Browse files
yipin-chencyip10
authored andcommitted
Node: added MOVE command (valkey-io#2104)
* Node: added MOVE command Signed-off-by: Yi-Pin Chen <yi-pin.chen@improving.com> Signed-off-by: Chloe Yip <chloe.yip@improving.com>
1 parent c392472 commit d563e4f

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* Node: Added FUNCTION FLUSH command ([#1984](https://github.com/valkey-io/valkey-glide/pull/1984))
4848
* Node: Added FCALL and FCALL_RO commands ([#2011](https://github.com/valkey-io/valkey-glide/pull/2011))
4949
* Node: Added COPY command ([#2024](https://github.com/valkey-io/valkey-glide/pull/2024))
50+
* Node: Added MOVE command ([#2104](https://github.com/valkey-io/valkey-glide/pull/2104))
5051
* Node: Added ZMPOP command ([#1994](https://github.com/valkey-io/valkey-glide/pull/1994))
5152
* Node: Added ZINCRBY command ([#2009](https://github.com/valkey-io/valkey-glide/pull/2009))
5253
* Node: Added BZMPOP command ([#2018](https://github.com/valkey-io/valkey-glide/pull/2018))

node/src/Commands.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,16 @@ export function createCopy(
27142714
return createCommand(RequestType.Copy, args);
27152715
}
27162716

2717+
/**
2718+
* @internal
2719+
*/
2720+
export function createMove(
2721+
key: string,
2722+
dbIndex: number,
2723+
): command_request.Command {
2724+
return createCommand(RequestType.Move, [key, dbIndex.toString()]);
2725+
}
2726+
27172727
/**
27182728
* Optional arguments to LPOS command.
27192729
*

node/src/GlideClient.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
createInfo,
3939
createLastSave,
4040
createLolwut,
41+
createMove,
4142
createPing,
4243
createPublish,
4344
createRandomKey,
@@ -426,6 +427,26 @@ export class GlideClient extends BaseClient {
426427
);
427428
}
428429

430+
/**
431+
* Move `key` from the currently selected database to the database specified by `dbIndex`.
432+
*
433+
* See https://valkey.io/commands/move/ for more details.
434+
*
435+
* @param key - The key to move.
436+
* @param dbIndex - The index of the database to move `key` to.
437+
* @returns `true` if `key` was moved, or `false` if the `key` already exists in the destination
438+
* database or does not exist in the source database.
439+
*
440+
* @example
441+
* ```typescript
442+
* const result = await client.move("key", 1);
443+
* console.log(result); // Output: true
444+
* ```
445+
*/
446+
public async move(key: string, dbIndex: number): Promise<boolean> {
447+
return this.createWritePromise(createMove(key, dbIndex));
448+
}
449+
429450
/**
430451
* Displays a piece of generative computer art and the server version.
431452
*

node/src/Transaction.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ import {
142142
createMGet,
143143
createMSet,
144144
createMSetNX,
145+
createMove,
145146
createObjectEncoding,
146147
createObjectFreq,
147148
createObjectIdletime,
@@ -3438,6 +3439,21 @@ export class Transaction extends BaseTransaction<Transaction> {
34383439
return this.addAndReturn(createCopy(source, destination, options));
34393440
}
34403441

3442+
/**
3443+
* Move `key` from the currently selected database to the database specified by `dbIndex`.
3444+
*
3445+
* See https://valkey.io/commands/move/ for more details.
3446+
*
3447+
* @param key - The key to move.
3448+
* @param dbIndex - The index of the database to move `key` to.
3449+
*
3450+
* Command Response - `true` if `key` was moved, or `false` if the `key` already exists in the destination
3451+
* database or does not exist in the source database.
3452+
*/
3453+
public move(key: string, dbIndex: number): Transaction {
3454+
return this.addAndReturn(createMove(key, dbIndex));
3455+
}
3456+
34413457
/** Publish a message on pubsub channel.
34423458
*
34433459
* See https://valkey.io/commands/publish for more details.

node/tests/GlideClient.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,46 @@ describe("GlideClient", () => {
504504
TIMEOUT,
505505
);
506506

507+
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
508+
"move test_%p",
509+
async (protocol) => {
510+
const client = await GlideClient.createClient(
511+
getClientConfigurationOption(cluster.getAddresses(), protocol),
512+
);
513+
514+
const key1 = "{key}-1" + uuidv4();
515+
const key2 = "{key}-2" + uuidv4();
516+
const value = uuidv4();
517+
518+
expect(await client.select(0)).toEqual("OK");
519+
expect(await client.move(key1, 1)).toEqual(false);
520+
521+
expect(await client.set(key1, value)).toEqual("OK");
522+
expect(await client.get(key1)).toEqual(value);
523+
expect(await client.move(key1, 1)).toEqual(true);
524+
expect(await client.get(key1)).toEqual(null);
525+
expect(await client.select(1)).toEqual("OK");
526+
expect(await client.get(key1)).toEqual(value);
527+
528+
await expect(client.move(key1, -1)).rejects.toThrow(RequestError);
529+
530+
//transaction tests
531+
const transaction = new Transaction();
532+
transaction.select(1);
533+
transaction.move(key2, 0);
534+
transaction.set(key2, value);
535+
transaction.move(key2, 0);
536+
transaction.select(0);
537+
transaction.get(key2);
538+
const results = await client.exec(transaction);
539+
540+
expect(results).toEqual(["OK", false, "OK", true, "OK", value]);
541+
542+
client.close();
543+
},
544+
TIMEOUT,
545+
);
546+
507547
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
508548
"function load function list function stats test_%p",
509549
async (protocol) => {

0 commit comments

Comments
 (0)