patchBatch
Batch patching utility that takes an array of items to be changed and returns an array of arguments to be called with the patch method.
This utility is useful when you need to patch multiple items with varying data in as few requests as possible.
ts
import { patchBatch } from 'feathers-utils/utils';Type declaration
ts
export type PatchBatchOptions<IdKey extends string> = {
/** the key of the id property */
id?: IdKey
}
export type PatchBatchResultItem<T = Record<string, unknown>, P = Params> = [
Id | null,
T,
P | undefined,
]
/**
* Batch patching utility that takes an array of items to be changed and returns an array of arguments to be called with the `patch` method.
*
* This utility is useful when you need to patch multiple items with varying data in as few requests as possible.
*
* @example
* ```ts
* const items = [
* { id: 1, value: 10 },
* { id: 2, value: 10 },
* { id: 3, value: 20 },
* ];
*
* const batched = patchBatch(items, { id: 'id' });
* // batched will be:
* // [
* // [null, { value: 10 }, { query: { id: { $in: [1, 2] } } }],
* // [3, { value: 20 }, undefined],
* // ]
*
* await Promise.all(batched.map(args => service.patch(...args)));
* ```
*/
export declare function patchBatch<
T extends Record<string, any>,
IdKey extends KeyOf<T>,
P extends Params,
R extends Omit<T, IdKey> = Omit<T, IdKey>,
>(items: T[], options?: PatchBatchOptions<IdKey>): PatchBatchResultItem<R, P>[]| Argument | Type | Description |
|---|---|---|
| items | T[] | |
| options | PatchBatchOptions<IdKey> |
Example
ts
const items = [
{ id: 1, value: 10 },
{ id: 2, value: 10 },
{ id: 3, value: 20 },
];
const batched = patchBatch(items, { id: 'id' });
// batched will be:
// [
// [null, { value: 10 }, { query: { id: { $in: [1, 2] } } }],
// [3, { value: 20 }, undefined],
// ]
await Promise.all(batched.map(args => service.patch(...args)));