refactor: add batch insert into indexDB

This commit is contained in:
Tristan Yang
2023-03-10 14:33:36 +08:00
parent 928db8f1fa
commit 0cd7c1e9dd
6 changed files with 105 additions and 35 deletions
+4 -1
View File
@@ -1,7 +1,10 @@
import localforage from "localforage";
import * as localforage from "localforage";
import { extendPrototype } from 'localforage-setitems';
import useRehydrate from "./useRehydrate";
import { KEY_UID, CACHE_VERSION } from "../config";
extendPrototype(localforage);
const tables = [
{
storeName: "channels",
@@ -1,3 +1,5 @@
import localforage from "localforage";
import { Channel } from "../../types/channel";
import clearTable from "./clear.handler";
interface Params {
data: any;
@@ -5,7 +7,7 @@ interface Params {
operation: string;
}
export default async function handler({ operation, data, payload }: Params) {
const table = window.CACHE["channels"];
const table = window.CACHE["channels"] as typeof localforage;
if (operation.startsWith("reset")) {
clearTable("channels");
return;
@@ -13,12 +15,11 @@ export default async function handler({ operation, data, payload }: Params) {
switch (operation) {
case "fillChannels":
{
const chs = payload;
await Promise.all(
chs.map(({ gid, ...rest }) => {
return table?.setItem(gid + "", { gid, ...rest });
})
);
const chs = payload as Channel[];
const arr = chs.map(({ gid, ...rest }) => {
return { key: gid + "", value: rest };
});
await table.setItems(arr);
}
break;
case "addChannel":
@@ -39,7 +40,6 @@ export default async function handler({ operation, data, payload }: Params) {
await table?.setItem(id + "", data.byId[id]);
}
break;
default:
break;
}
@@ -1,3 +1,4 @@
import localforage from "localforage";
import clearTable from "./clear.handler";
interface Params {
@@ -5,7 +6,7 @@ interface Params {
operation: string;
}
export default async function handler({ operation, payload }: Params) {
const table = window.CACHE["server"];
const table = window.CACHE["server"] as typeof localforage;
if (operation.startsWith("reset")) {
clearTable("server");
return;
@@ -14,11 +15,10 @@ export default async function handler({ operation, payload }: Params) {
case "updateInfo":
{
const data = payload;
await Promise.all(
Object.entries(data).map(([_key, _val]) => {
return table?.setItem(_key, _val);
})
);
const arr = Object.entries(data).map(([_key, _val]) => {
return { key: _key, value: _val };
});
await table.setItems(arr);
}
break;
default:
+8 -7
View File
@@ -1,7 +1,9 @@
import localforage from "localforage";
import { User } from "../../types/user";
import clearTable from "./clear.handler";
export default async function handler({ operation, data, payload }) {
const table = window.CACHE["users"];
const table = window.CACHE["users"] as typeof localforage;
if (operation.startsWith("reset")) {
clearTable("users");
return;
@@ -9,12 +11,11 @@ export default async function handler({ operation, data, payload }) {
switch (operation) {
case "fillUsers":
{
const users = payload;
await Promise.all(
users.map(({ uid, ...rest }) => {
return table?.setItem(uid + "", { uid, ...rest });
})
);
const users = payload as User[];
const arr = users.map(({ uid, ...rest }) => {
return { key: uid + "", value: rest };
});
await table.setItems(arr);
}
break;
case "updateUsersByLogs":