revert loaddbs change

This commit is contained in:
Weixuan Fu
2024-02-02 16:21:49 +08:00
parent def5ada66d
commit 2f73ff304a
3 changed files with 41 additions and 14 deletions
+12 -5
View File
@@ -5,7 +5,7 @@ import { Table } from "./WorkspaceDB";
import { indexdb } from "./indexdb";
import { v4 } from "uuid";
export class TableBase<T> {
export class TableBase<T extends TableBaseModel> {
public readonly tableName: Table;
protected constructor(tableName: Table) {
@@ -17,7 +17,13 @@ export class TableBase<T> {
objs.forEach((f) => {
// only tags table is using name as primary key, we either give up backup that table to diskDB
// or add a id as primary key to it
backup[f.id ?? f.name] = f;
if (f.id) {
backup[f.id] = f;
} else if (f.name) {
backup[f.name] = f;
} else {
console.error("TableBaseModel should have id or name");
}
});
saveDB(this.tableName, JSON.stringify(backup));
}
@@ -30,6 +36,7 @@ export class TableBase<T> {
await this.saveDiskDB();
return newItem;
}
public async put(newItem: T): Promise<T> {
await indexdb[this.tableName].put(newItem as any);
await this.saveDiskDB();
@@ -37,12 +44,12 @@ export class TableBase<T> {
}
public async listAll(): Promise<T[]> {
const list = await indexdb[this.tableName].toArray();
return list as T[];
return list as any[];
}
public async batchQuery(ids: string[]): Promise<T[]> {
const list = await indexdb[this.tableName].bulkGet(ids);
return list as T[];
return list as any[];
}
public async getRecords(): Promise<Record<string, T>> {
@@ -66,7 +73,7 @@ export class TableBase<T> {
public async get(id: string): Promise<T | undefined> {
const obj = await indexdb[this.tableName].get(id);
return obj as T;
return obj as any;
}
public async delete(id: string) {
+27 -7
View File
@@ -32,16 +32,36 @@ export let mediaTable: MediaTable | null = null;
export let workflowVersionsTable: WorkflowVersionsTable | null = null;
export async function loadDBs() {
const loadDBs = async () => {
mediaTable = new MediaTable();
changelogsTable = await ChangelogsTable.load();
foldersTable = await FoldersTable.load();
userSettingsTable = await UserSettingsTable.load();
tagsTable = await TagsTable.load();
const loadWorkflows = async () => {
workflowsTable = await WorkflowsTable.load();
};
const loadTags = async () => {
tagsTable = await TagsTable.load();
};
const loadUserSettings = async () => {
userSettingsTable = await UserSettingsTable.load();
};
const loadFolders = async () => {
foldersTable = await FoldersTable.load();
};
const loadChangelogs = async () => {
changelogsTable = await ChangelogsTable.load();
};
const loadMedia = async () => {
mediaTable = new MediaTable();
};
const loadWorkflowVersions = async () => {
workflowVersionsTable = await WorkflowVersionsTable.load();
};
await loadDBs();
await Promise.all([
loadWorkflows(),
loadTags(),
loadUserSettings(),
loadFolders(),
loadChangelogs(),
loadMedia(),
loadWorkflowVersions(),
]);
if (localStorage.getItem("WORKSPACE_INDEXDB_BACKFILL") !== "true") {
await backfillIndexdb();
localStorage.setItem("WORKSPACE_INDEXDB_BACKFILL", "true");
+2 -2
View File
@@ -17,8 +17,8 @@ export interface Workflow extends SortableItem {
}
export interface TableBaseModel {
id: string;
name: string;
id?: string;
name?: string;
}
export type Tags = {