mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
added a new mutate replacement for useEntityMutate to quickly update cache without revalidating
This commit is contained in:
@@ -270,7 +270,7 @@ export class Mutator<
|
|||||||
return (await this.many(qb)) as any;
|
return (await this.many(qb)) as any;
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateWhere(data: Input, where?: RepoQuery["where"]): Promise<MutatorResponse<Output[]>> {
|
async updateWhere(data: Partial<Input>, where?: RepoQuery["where"]): Promise<MutatorResponse<Output[]>> {
|
||||||
const entity = this.entity;
|
const entity = this.entity;
|
||||||
const validatedData = await this.getValidatedData(data, "update");
|
const validatedData = await this.getValidatedData(data, "update");
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { encodeSearch, objectTransform } from "core/utils";
|
|||||||
import type { EntityData, RepoQuery } from "data";
|
import type { EntityData, RepoQuery } from "data";
|
||||||
import type { ModuleApi, ResponseObject } from "modules/ModuleApi";
|
import type { ModuleApi, ResponseObject } from "modules/ModuleApi";
|
||||||
import useSWR, { type SWRConfiguration, mutate } from "swr";
|
import useSWR, { type SWRConfiguration, mutate } from "swr";
|
||||||
import { useApi } from "ui/client";
|
import { type Api, useApi } from "ui/client";
|
||||||
|
|
||||||
export class UseEntityApiError<Payload = any> extends Error {
|
export class UseEntityApiError<Payload = any> extends Error {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -148,9 +148,44 @@ export const useEntityQuery = <
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export async function mutateEntityCache<
|
||||||
|
Entity extends keyof DB | string,
|
||||||
|
Data = Entity extends keyof DB ? Omit<DB[Entity], "id"> : EntityData
|
||||||
|
>(api: Api["data"], entity: Entity, id: PrimaryFieldType, partialData: Partial<Data>) {
|
||||||
|
function update(prev: any, partialNext: any) {
|
||||||
|
if (
|
||||||
|
typeof prev !== "undefined" &&
|
||||||
|
typeof partialNext !== "undefined" &&
|
||||||
|
"id" in prev &&
|
||||||
|
prev.id === id
|
||||||
|
) {
|
||||||
|
return { ...prev, ...partialNext };
|
||||||
|
}
|
||||||
|
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
const entityKey = makeKey(api, entity);
|
||||||
|
|
||||||
|
return mutate(
|
||||||
|
(key) => typeof key === "string" && key.startsWith(entityKey),
|
||||||
|
async (data) => {
|
||||||
|
if (typeof data === "undefined") return;
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
return data.map((item) => update(item, partialData));
|
||||||
|
}
|
||||||
|
return update(data, partialData);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
revalidate: false
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export const useEntityMutate = <
|
export const useEntityMutate = <
|
||||||
Entity extends keyof DB | string,
|
Entity extends keyof DB | string,
|
||||||
Id extends PrimaryFieldType | undefined = undefined
|
Id extends PrimaryFieldType | undefined = undefined,
|
||||||
|
Data = Entity extends keyof DB ? Omit<DB[Entity], "id"> : EntityData
|
||||||
>(
|
>(
|
||||||
entity: Entity,
|
entity: Entity,
|
||||||
id?: Id,
|
id?: Id,
|
||||||
@@ -160,5 +195,15 @@ export const useEntityMutate = <
|
|||||||
...options,
|
...options,
|
||||||
enabled: false
|
enabled: false
|
||||||
});
|
});
|
||||||
return $q;
|
|
||||||
|
const _mutate = id
|
||||||
|
? (data) => mutateEntityCache($q.api, entity, id, data)
|
||||||
|
: (id, data) => mutateEntityCache($q.api, entity, id, data);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...$q,
|
||||||
|
mutate: _mutate as unknown as Id extends undefined
|
||||||
|
? (id: PrimaryFieldType, data: Partial<Data>) => Promise<void>
|
||||||
|
: (data: Partial<Data>) => Promise<void>
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,53 +1,69 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useEntity, useEntityQuery } from "ui/client/api/use-entity";
|
import { useEntity, useEntityMutate, useEntityQuery } from "ui/client/api/use-entity";
|
||||||
import { Scrollable } from "ui/layouts/AppShell/AppShell";
|
import { Scrollable } from "ui/layouts/AppShell/AppShell";
|
||||||
|
|
||||||
export default function SwrAndDataApi() {
|
export default function SwrAndDataApi() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<Scrollable>
|
||||||
|
asdf
|
||||||
<DirectDataApi />
|
<DirectDataApi />
|
||||||
<QueryDataApi />
|
<QueryDataApi />
|
||||||
</div>
|
<QueryMutateDataApi />
|
||||||
|
</Scrollable>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function QueryDataApi() {
|
function QueryMutateDataApi() {
|
||||||
const [text, setText] = useState("");
|
const { mutate } = useEntityMutate("comments");
|
||||||
const { data, update, ...r } = useEntityQuery("comments", 2, {
|
const { data, ...r } = useEntityQuery("comments", undefined, {
|
||||||
sort: { by: "id", dir: "desc" }
|
limit: 2
|
||||||
});
|
});
|
||||||
const comment = data ? data : null;
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setText(comment?.content ?? "");
|
|
||||||
}, [comment]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Scrollable>
|
<div>
|
||||||
|
bla
|
||||||
<pre>{JSON.stringify(r.key)}</pre>
|
<pre>{JSON.stringify(r.key)}</pre>
|
||||||
{r.error && <div>failed to load</div>}
|
{r.error && <div>failed to load</div>}
|
||||||
{r.isLoading && <div>loading...</div>}
|
{r.isLoading && <div>loading...</div>}
|
||||||
{data && <pre>{JSON.stringify(data, null, 2)}</pre>}
|
{data && <pre>{JSON.stringify(data, null, 2)}</pre>}
|
||||||
{data && (
|
{data && (
|
||||||
<form
|
<div>
|
||||||
onSubmit={async (e) => {
|
{data.map((comment) => (
|
||||||
e.preventDefault();
|
<input
|
||||||
if (!comment) return;
|
key={String(comment.id)}
|
||||||
await update({ content: text });
|
type="text"
|
||||||
return false;
|
value={comment.content}
|
||||||
}}
|
onChange={async (e) => {
|
||||||
>
|
await mutate(comment.id, { content: e.target.value });
|
||||||
<input type="text" value={text} onChange={(e) => setText(e.target.value)} />
|
}}
|
||||||
<button type="submit">submit</button>
|
className="border border-black"
|
||||||
</form>
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</Scrollable>
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function QueryDataApi() {
|
||||||
|
const { data, update, ...r } = useEntityQuery("comments", undefined, {
|
||||||
|
sort: { by: "id", dir: "asc" },
|
||||||
|
limit: 3
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<pre>{JSON.stringify(r.key)}</pre>
|
||||||
|
{r.error && <div>failed to load</div>}
|
||||||
|
{r.isLoading && <div>loading...</div>}
|
||||||
|
{data && <pre>{JSON.stringify(data, null, 2)}</pre>}
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function DirectDataApi() {
|
function DirectDataApi() {
|
||||||
const [data, setData] = useState<any>();
|
const [data, setData] = useState<any>();
|
||||||
const { create, read, update, _delete } = useEntity("users");
|
const { create, read, update, _delete } = useEntity("comments");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
read().then((data) => setData(data));
|
read().then((data) => setData(data));
|
||||||
|
|||||||
Reference in New Issue
Block a user