Skip to content

Commit 36a3e24

Browse files
authored
!feat(utils): optimizeBatchPatch with Record (#26)
1 parent c7db3b9 commit 36a3e24

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

src/typesInternal.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ export type ReturnSyncHook<H extends HookContext = HookContext> = (
2222
export type ReturnAsyncHook<H extends HookContext = HookContext> = (
2323
context: H,
2424
) => Promise<H>;
25+
26+
export type KeyOf<T> = Extract<keyof T, string>;

src/utils/optimizeBatchPatch.ts

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Id, Params } from "@feathersjs/feathers";
22
import { deepEqual } from "fast-equals";
3+
import type { KeyOf } from "../typesInternal";
34

4-
export type OptimizeBatchPatchOptions = {
5-
id?: string;
5+
export type OptimizeBatchPatchOptions<IdKey extends string> = {
6+
/** the key of the id property */
7+
id?: IdKey;
68
};
79

810
export type OptimizeBatchPatchResultItem<
@@ -11,18 +13,26 @@ export type OptimizeBatchPatchResultItem<
1113
> = [Id, T, P | undefined];
1214

1315
export function optimizeBatchPatch<
14-
T extends Record<string, unknown>,
16+
T extends Record<string, any>,
17+
IdKey extends KeyOf<T>,
1518
P extends Params,
19+
R extends Omit<T, IdKey> = Omit<T, IdKey>,
1620
>(
17-
items: Map<Id, T>,
18-
options?: OptimizeBatchPatchOptions,
19-
): OptimizeBatchPatchResultItem<T, P>[] {
20-
const map: { ids: Id[]; data: T }[] = [];
21+
items: T[],
22+
options?: OptimizeBatchPatchOptions<IdKey>,
23+
): OptimizeBatchPatchResultItem<R, P>[] {
24+
const map: { ids: Id[]; data: R }[] = [];
2125

22-
const id = options?.id ?? "id";
26+
const idKey = options?.id ?? "id";
2327

24-
for (const [id, data] of items) {
25-
const index = map.findIndex((item) => deepEqual(item.data, data));
28+
for (const _data of items) {
29+
const data = _data as unknown as R;
30+
const id = _data[idKey];
31+
delete data[idKey as any];
32+
33+
const index = map.findIndex((item) => {
34+
return deepEqual(item.data, data);
35+
});
2636

2737
if (index === -1) {
2838
map.push({ ids: [id], data });
@@ -33,34 +43,56 @@ export function optimizeBatchPatch<
3343

3444
return map.map(({ ids, data }) => {
3545
return ids.length === 1
36-
? ([ids[0], data, undefined] as OptimizeBatchPatchResultItem<T, P>)
46+
? ([ids[0], data, undefined] as OptimizeBatchPatchResultItem<R, P>)
3747
: ([
3848
null,
3949
data,
4050
{
4151
query: {
42-
[id]: { $in: ids },
52+
[idKey]: { $in: ids },
4353
},
4454
},
45-
] as OptimizeBatchPatchResultItem<T, P>);
55+
] as OptimizeBatchPatchResultItem<R, P>);
4656
});
4757
}
4858

4959
if (import.meta.vitest) {
5060
const { it, expect } = import.meta.vitest;
5161
it("optimizeBatchPatch", () => {
52-
const items = new Map<Id, Record<string, unknown>>([
53-
["1", { name: "John" }],
54-
["2", { name: "Jane" }],
55-
["3", { name: "John" }],
56-
["4", { name: "Jane" }],
57-
[5, { name: "Jack" }],
58-
]);
59-
60-
expect(optimizeBatchPatch(items)).toEqual([
62+
expect(
63+
optimizeBatchPatch(
64+
[
65+
{ id: "1", name: "John" },
66+
{ id: "2", name: "Jane" },
67+
{ id: "3", name: "John" },
68+
{ id: "4", name: "Jane" },
69+
{ id: 5, name: "Jack" },
70+
],
71+
{ id: "id" },
72+
),
73+
).toEqual([
6174
[null, { name: "John" }, { query: { id: { $in: ["1", "3"] } } }],
6275
[null, { name: "Jane" }, { query: { id: { $in: ["2", "4"] } } }],
6376
[5, { name: "Jack" }, undefined],
6477
]);
6578
});
79+
80+
it("optimizeBatchPatch with _id", () => {
81+
expect(
82+
optimizeBatchPatch(
83+
[
84+
{ _id: "1", name: "John" },
85+
{ _id: "2", name: "Jane" },
86+
{ _id: "3", name: "John" },
87+
{ _id: "4", name: "Jane" },
88+
{ _id: 5, name: "Jack" },
89+
],
90+
{ id: "_id" },
91+
),
92+
).toEqual([
93+
[null, { name: "John" }, { query: { _id: { $in: ["1", "3"] } } }],
94+
[null, { name: "Jane" }, { query: { _id: { $in: ["2", "4"] } } }],
95+
[5, { name: "Jack" }, undefined],
96+
]);
97+
});
6698
}

0 commit comments

Comments
 (0)