Skip to content
Open
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
25 changes: 25 additions & 0 deletions src/implementation/Client/GRPCClient/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
import { create } from "@bufbuild/protobuf";
import GRPCClient from "./GRPCClient";
import {
DeleteBulkStateRequestSchema,
DeleteStateRequestSchema,
ExecuteStateTransactionRequestSchema,
GetBulkStateRequestSchema,
Expand All @@ -35,6 +36,7 @@ import { Settings } from "../../../utils/Settings.util";
import { StateSaveResponseType } from "../../../types/state/StateSaveResponseType";
import { StateSaveOptions } from "../../../types/state/StateSaveOptions.type";
import { StateDeleteOptions } from "../../../types/state/StateDeleteOptions.type";
import { StateDeleteBulkOptions } from "../../../types/state/StateDeleteBulkOptions.type";
import { StateGetOptions } from "../../../types/state/StateGetOptions.type";
import { IStateOptions } from "../../../types/state/StateOptions.type";

Expand Down Expand Up @@ -130,6 +132,29 @@ export default class GRPCClientState implements IClientState {
return {};
}

async deleteBulk(
storeName: string,
items: KeyValuePairType[],
_options: StateDeleteBulkOptions = {},
): Promise<StateSaveResponseType> {
const states: StateItem[] = [];

for (const item of items) {
const si = create(StateItemSchema, {
key: item.key,
etag: item?.etag ? create(EtagSchema, { value: item.etag }) : undefined,
options: this._configureStateOptions(item?.options),
metadata: item.metadata ?? {},
});
Comment on lines +135 to +148
states.push(si);
}

const client = await this.client.getClient();
await client.deleteBulkState(create(DeleteBulkStateRequestSchema, { storeName, states }));

return {};
}

async transaction(
storeName: string,
operations: OperationType[] = [],
Expand Down
26 changes: 26 additions & 0 deletions src/implementation/Client/HTTPClient/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Logger } from "../../../logger/Logger";
import { StateSaveResponseType } from "../../../types/state/StateSaveResponseType";
import { StateSaveOptions } from "../../../types/state/StateSaveOptions.type";
import { StateDeleteOptions } from "../../../types/state/StateDeleteOptions.type";
import { StateDeleteBulkOptions } from "../../../types/state/StateDeleteBulkOptions.type";
import { THTTPExecuteParams } from "../../../types/http/THTTPExecuteParams.type";
import { StateGetOptions } from "../../../types/state/StateGetOptions.type";

Expand Down Expand Up @@ -120,6 +121,31 @@ export default class HTTPClientState implements IClientState {
return {};
}

async deleteBulk(
storeName: string,
items: KeyValuePairType[],
_options: StateDeleteBulkOptions = {},
): Promise<StateSaveResponseType> {
// The Dapr HTTP API does not expose a dedicated bulk-delete endpoint.
// Perform individual DELETE requests for each item.
try {
await Promise.all(
items.map((item) =>
Comment on lines +131 to +133
this.delete(storeName, item.key, {
etag: item.etag,
concurrency: item.options?.concurrency,
consistency: item.options?.consistency,
}),
Comment on lines +134 to +138
),
);
} catch (e: any) {
this.logger.error(`Error bulk deleting state from store ${storeName}, error: ${e}`);
return { error: e };
}

return {};
}

async transaction(
storeName: string,
operations: OperationType[] = [],
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import DaprPubSubStatusEnum from "./enum/DaprPubSubStatus.enum";
import StateConcurrencyEnum from "./enum/StateConcurrency.enum";
import StateConsistencyEnum from "./enum/StateConsistency.enum";
import { StateGetBulkOptions } from "./types/state/StateGetBulkOptions.type";
import { StateDeleteBulkOptions } from "./types/state/StateDeleteBulkOptions.type";

import DaprWorkflowClient from "./workflow/client/DaprWorkflowClient";
import WorkflowActivityContext from "./workflow/runtime/WorkflowActivityContext";
Expand Down Expand Up @@ -79,6 +80,7 @@ export {
StateConsistencyEnum,
PubSubBulkPublishResponse,
StateGetBulkOptions,
StateDeleteBulkOptions,
DaprWorkflowClient,
WorkflowActivityContext,
WorkflowContext,
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/Client/IClientState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ import { StateGetBulkOptions } from "../../types/state/StateGetBulkOptions.type"
import { StateSaveResponseType } from "../../types/state/StateSaveResponseType";
import { StateSaveOptions } from "../../types/state/StateSaveOptions.type";
import { StateDeleteOptions } from "../../types/state/StateDeleteOptions.type";
import { StateDeleteBulkOptions } from "../../types/state/StateDeleteBulkOptions.type";
import { StateGetOptions } from "../../types/state/StateGetOptions.type";

export default interface IClientState {
save(storeName: string, stateObjects: KeyValuePairType[], options?: StateSaveOptions): Promise<StateSaveResponseType>;
get(storeName: string, key: string, options?: Partial<StateGetOptions>): Promise<KeyValueType | string>;
getBulk(storeName: string, keys: string[], options?: StateGetBulkOptions): Promise<KeyValueType[]>;
delete(storeName: string, key: string, options?: Partial<StateDeleteOptions>): Promise<StateSaveResponseType>;
deleteBulk(
storeName: string,
items: KeyValuePairType[],
options?: StateDeleteBulkOptions,
): Promise<StateSaveResponseType>;
Comment on lines +32 to +36
transaction(storeName: string, operations?: OperationType[], metadata?: IRequestMetadata | null): Promise<void>;
query(storeName: string, query: StateQueryType): Promise<StateQueryResponseType>;
}
21 changes: 21 additions & 0 deletions src/types/state/StateDeleteBulkOptions.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { KeyValueType } from "../KeyValue.type";

export type StateDeleteBulkOptions = {
/**
* Metadata to be passed to the bulk delete operation.
*/
metadata?: KeyValueType;
};
58 changes: 58 additions & 0 deletions test/e2e/common/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,35 @@ describe("common/client/http", () => {
expect(res).toEqual("");
});

it("should be able to delete multiple keys in bulk from the state store", async () => {
await client.state.save(stateStoreName, [
{
key: "key-1",
value: "value-1",
},
{
key: "key-2",
value: "value-2",
},
{
key: "key-3",
value: "value-3",
},
]);

await client.state.deleteBulk(stateStoreName, [
{ key: "key-1", value: "" },
{ key: "key-3", value: "" },
]);

const res1 = await client.state.get(stateStoreName, "key-1");
const res2 = await client.state.get(stateStoreName, "key-2");
const res3 = await client.state.get(stateStoreName, "key-3");
expect(res1).toBeFalsy();
expect(res2).toEqual("value-2");
expect(res3).toBeFalsy();
});

it("should be able to perform a transaction that replaces a key and deletes another", async () => {
await client.state.transaction(stateStoreName, [
{
Expand Down Expand Up @@ -907,6 +936,35 @@ describe("common/client/grpc", () => {
expect(res).toEqual("");
});

it("should be able to delete multiple keys in bulk from the state store", async () => {
await client.state.save(stateStoreName, [
{
key: "key-1",
value: "value-1",
},
{
key: "key-2",
value: "value-2",
},
{
key: "key-3",
value: "value-3",
},
]);

await client.state.deleteBulk(stateStoreName, [
{ key: "key-1", value: "" },
{ key: "key-3", value: "" },
]);

const res1 = await client.state.get(stateStoreName, "key-1");
const res2 = await client.state.get(stateStoreName, "key-2");
const res3 = await client.state.get(stateStoreName, "key-3");
expect(res1).toEqual("");
expect(res2).toEqual("value-2");
expect(res3).toEqual("");
});

it("should be able to perform a transaction that replaces a key and deletes another", async () => {
await client.state.transaction(stateStoreName, [
{
Expand Down
Loading