Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: added APPEND command #2095

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* Node: Added ZREMRANGEBYLEX command ([#2025](https://github.com/valkey-io/valkey-glide/pull/2025))
* Node: Added ZSCAN command ([#2061](https://github.com/valkey-io/valkey-glide/pull/2061))
* Node: Added SETRANGE command ([#2066](https://github.com/valkey-io/valkey-glide/pull/2066))
* Node: Added APPEND command ([#2095](https://github.com/valkey-io/valkey-glide/pull/2095))
* Node: Added XDEL command ([#2064](https://github.com/valkey-io/valkey-glide/pull/2064))
* Node: Added LMPOP & BLMPOP command ([#2050](https://github.com/valkey-io/valkey-glide/pull/2050))
* Node: Added PUBSUB support ([#1964](https://github.com/valkey-io/valkey-glide/pull/1964))
Expand Down
27 changes: 27 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
StreamReadOptions,
StreamTrimOptions,
ZAddOptions,
createAppend,
createBLMPop,
createBLMove,
createBLPop,
Expand Down Expand Up @@ -4611,6 +4612,32 @@ export class BaseClient {
return this.createWritePromise(createSetRange(key, offset, value));
}

/**
* Appends a `value` to a `key`. If `key` does not exist it is created and set as an empty string,
* so `APPEND` will be similar to {@link set} in this special case.
*
* See https://valkey.io/commands/append/ for more details.
*
* @param key - The key of the string.
* @param value - The key of the string.
* @returns The length of the string after appending the value.
*
* @example
* ```typescript
* const len = await client.append("key", "Hello");
* console.log(len);
* // Output: 5 - Indicates that "Hello" has been appended to the value of "key", which was initially
* // empty, resulting in a new value of "Hello" with a length of 5 - similar to the set operation.
* len = await client.append("key", " world");
* console.log(result);
* // Output: 11 - Indicates that " world" has been appended to the value of "key", resulting in a
* // new value of "Hello world" with a length of 11.
* ```
*/
public async append(key: string, value: string): Promise<number> {
return this.createWritePromise(createAppend(key, value));
}

/**
* Pops one or more elements from the first non-empty list from the provided `keys`.
*
Expand Down
8 changes: 8 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,14 @@ export function createSetRange(
return createCommand(RequestType.SetRange, [key, offset.toString(), value]);
}

/** @internal */
export function createAppend(
key: string,
value: string,
): command_request.Command {
return createCommand(RequestType.Append, [key, value]);
}

/**
* @internal
*/
Expand Down
16 changes: 16 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
StreamReadOptions,
StreamTrimOptions,
ZAddOptions,
createAppend,
createBLMPop,
createBLMove,
createBLPop,
Expand Down Expand Up @@ -2772,6 +2773,21 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createSetRange(key, offset, value));
}

/**
* Appends a `value` to a `key`. If `key` does not exist it is created and set as an empty string,
* so `APPEND` will be similar to {@link set} in this special case.
*
* See https://valkey.io/commands/append/ for more details.
*
* @param key - The key of the string.
* @param value - The key of the string.
*
* Command Response - The length of the string after appending the value.
*/
public append(key: string, value: string): T {
return this.addAndReturn(createAppend(key, value));
}

/**
* Pops one or more elements from the first non-empty list from the provided `keys`.
*
Expand Down
23 changes: 23 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4707,6 +4707,29 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"append test_%p",
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
const key2 = uuidv4();
const value = uuidv4();

// Append on non-existing string(similar to SET)
expect(await client.append(key1, value)).toBe(value.length);
expect(await client.append(key1, value)).toBe(value.length * 2);
expect(await client.get(key1)).toEqual(value.concat(value));

// key exists but holding the wrong kind of value
expect(await client.sadd(key2, ["a"])).toBe(1);
await expect(client.append(key2, "_")).rejects.toThrow(
RequestError,
);
}, protocol);
},
config.timeout,
);

// Set command tests

async function setWithExpiryOptions(client: BaseClient) {
Expand Down
6 changes: 5 additions & 1 deletion node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,11 @@ export async function transactionTest(
baseTransaction.strlen(key1);
responseData.push(["strlen(key1)", 3]);
baseTransaction.setrange(key1, 0, "GLIDE");
responseData.push(["setrange(key1, 0, 'GLIDE'", 5]);
responseData.push(["setrange(key1, 0, 'GLIDE')", 5]);
baseTransaction.del([key1]);
responseData.push(["del([key1])", 1]);
baseTransaction.append(key1, "bar");
responseData.push(["append(key1, value)", 3]);
baseTransaction.del([key1]);
responseData.push(["del([key1])", 1]);
baseTransaction.hset(key4, { [field]: value });
Expand Down
Loading