You can batch multiple set requests and execute them simultaneously using kvs.batchSet. This method will execute all batched requests
in parallel, and will successfully complete as many as possible.
There is no guaranteed order of completion. Batch operations will report which operations succeeded or failed.
By contrast, KVS transactions and Custom Entity Store transactions will succeed or fail all operations included in the transaction. That is, if any operation in the transaction cannot be completed, then all other operations will also fail.
Batch operations are not available through the legacy storage module of @forge/api.
Data stored through batch operations is still subject to the defined Forge hosted storage key and object size limits.
In addition:
kvs.batchSet operation can be a payload of 4MB, similar to transactions.We are currently working on addressing a bug that is incorrectly limiting request payloads for Transactions and Batch operations to 1MB instead of 4MB. See FRGE-1916 for additional details.
A batch operation will return an error and fail entirely if:
set requests.Execute multiple kvs.set or kvs.entity().set requests in one operation. You can execute a mix of both types of request in the same batch operation.
This method will return a BatchResponse that contain an array of
operatioons succeeded (successfulKeys) and failed (failedKeys).
1 2kvs.batchSet(items: BatchSetItem[]): Promise<BatchResponse>; // Definition of what needs to be stored interface BatchSetItem { entityName?: string; // If not provided it becomes a KVS request key: string; value: string | number | boolean | Record<string, any> | any[]; } interface BatchResponse { successfulKeys: BatchItemSuccess[]; failedKeys: BatchItemError[]; } interface BatchItemSuccess { key: string; entityName?: string; } interface BatchItemError { key: string; entityName?: string; error: { code: string; message: string; } }
The following batch operation sets multiple entities across different data structures:
1 2await kvs.batchSet([ { key: 'employee1', value: { surname:"Davis", age: -1, employmentyear: 2022, gender: "male", nationality: "Australian" }, entityName: "employee" }, { key: "untypedobject", value: { foo: "bar" }, } ]); /* { "successfulKeys": [ {"key": "untypedobject"}, {"entityName": "employee", "key": "employee1"} ], "failedKeys": [] } If some keys fail { successfulKeys: [{"key": "untypedobject"}] failedKeys: [{ "key": "employee1", "entityName": "employee", "error": { "code": "PROPERTY_VALUE_OUT_OF_BOUNDS", "message": "Property age value must be between: 1 and 100"} }] } */
Rate this page: