From ae479fbe9b11384afe273cb78cccd86607294c61 Mon Sep 17 00:00:00 2001 From: Weixuan Fu Date: Wed, 17 Jan 2024 03:20:04 +0800 Subject: [PATCH] migrate settings --- ui/src/db-tables/FoldersTable.ts | 1 + ui/src/db-tables/TableBase.ts | 4 +-- ui/src/db-tables/UserSettingsTable.ts | 46 ++++++++++++++++----------- ui/src/db-tables/WorkspaceDB.ts | 4 +-- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/ui/src/db-tables/FoldersTable.ts b/ui/src/db-tables/FoldersTable.ts index 2793a03..07d7479 100644 --- a/ui/src/db-tables/FoldersTable.ts +++ b/ui/src/db-tables/FoldersTable.ts @@ -4,6 +4,7 @@ import { Folder } from "../types/dbTypes"; import { validateOrSaveAllJsonFileMyWorkflows } from "../utils"; import { getWorkspaceIndexDB, updateWorkspaceIndexDB } from "./IndexDBUtils"; import { v4 as uuidv4 } from "uuid"; +import { TableBase } from "./TableBase"; export class FoldersTable { static readonly TABLE_NAME = "folders"; diff --git a/ui/src/db-tables/TableBase.ts b/ui/src/db-tables/TableBase.ts index bd9c99e..c3918c3 100644 --- a/ui/src/db-tables/TableBase.ts +++ b/ui/src/db-tables/TableBase.ts @@ -13,8 +13,8 @@ export class TableBase { public async listAll(): Promise { const objs = await indexdb[this.tableName].toArray(); if (objs?.length) return objs as T[]; - // const records = await this.getRecords(); - // return Object.values(records); + const records = await this.getRecords(); + return Object.values(records); } public async getRecords(): Promise> { diff --git a/ui/src/db-tables/UserSettingsTable.ts b/ui/src/db-tables/UserSettingsTable.ts index 31fc120..34c4c1b 100644 --- a/ui/src/db-tables/UserSettingsTable.ts +++ b/ui/src/db-tables/UserSettingsTable.ts @@ -1,14 +1,18 @@ import { getDB, getSystemDir, saveDB } from "../Api"; import { UserSettings } from "../types/dbTypes"; import { getWorkspaceIndexDB, updateWorkspaceIndexDB } from "./IndexDBUtils"; +import { TableBase } from "./TableBase"; +import { indexdb } from "./indexdb"; -export class UserSettingsTable { +const DEFAULT_USER = "guest"; + +export class UserSettingsTable extends TableBase { public records: UserSettings; static readonly TABLE_NAME = "userSettings"; - public defaultUser = "guest"; - private constructor() { + constructor() { + super("userSettings"); this.records = { - userID: this.defaultUser, + userID: DEFAULT_USER, topBarStyle: { top: 0, left: 0, @@ -21,9 +25,7 @@ export class UserSettingsTable { twoWaySync: false, }; } - public listSettings() { - return this.records; - } + public getSetting(key: K): UserSettings[K] { return this.records[key]; } @@ -32,27 +34,35 @@ export class UserSettingsTable { ...this.records, ...newPairs, }; + indexdb.userSettings.put(this.records); saveDB(UserSettingsTable.TABLE_NAME, JSON.stringify(this.records)); updateWorkspaceIndexDB(); } static async load(): Promise { const instance = new UserSettingsTable(); - const jsonStr = await getDB(UserSettingsTable.TABLE_NAME); - let json = jsonStr != null ? JSON.parse(jsonStr) : null; - if (json == null) { - const comfyspace = (await getWorkspaceIndexDB()) ?? "{}"; - const comfyspaceData = JSON.parse(comfyspace); - json = comfyspaceData[UserSettingsTable.TABLE_NAME] || {}; + let settings = await indexdb.userSettings.get(DEFAULT_USER); + if (settings == null) { + // throw new Error("User settings not found"); + const jsonStr = await getDB(UserSettingsTable.TABLE_NAME); + let settings = jsonStr != null ? JSON.parse(jsonStr) : null; + if (settings == null) { + const comfyspace = (await getWorkspaceIndexDB()) ?? "{}"; + const comfyspaceData = JSON.parse(comfyspace); + settings = comfyspaceData[UserSettingsTable.TABLE_NAME] || {}; + } } - if (!json.myWorkflowsDir) { + if (settings == null) { + settings = instance.records; + } + if (!settings.myWorkflowsDir) { const getDir = await getSystemDir(); - json.myWorkflowsDir = `${getDir.dir_path}/my_workflows`; + settings.myWorkflowsDir = `${getDir.dir_path}/my_workflows`; } - if (!json.userID) { - json.userID = instance.defaultUser; + if (!settings.userID) { + settings.userID = DEFAULT_USER; } - instance.records = json; + instance.records = settings; return instance; } } diff --git a/ui/src/db-tables/WorkspaceDB.ts b/ui/src/db-tables/WorkspaceDB.ts index d9dc7ed..674b04f 100644 --- a/ui/src/db-tables/WorkspaceDB.ts +++ b/ui/src/db-tables/WorkspaceDB.ts @@ -363,8 +363,8 @@ export async function backfillIndexdb() { }; const backfillUserSettings = async () => { try { - const all = await userSettingsTable?.listSettings(); - all && (await indexdb.userSettings.add(all)); + const all = await userSettingsTable?.listAll(); + all && (await indexdb.userSettings.bulkAdd(all)); } catch (error) { console.error(error); }