diff --git a/src/database/core/db.ts b/src/database/core/db.ts index e059c6141ab6..51d66ecfd952 100644 --- a/src/database/core/db.ts +++ b/src/database/core/db.ts @@ -10,7 +10,15 @@ import { DB_User } from '@/database/schemas/user'; import { uuid } from '@/utils/uuid'; import { migrateSettingsToUser } from './migrations/migrateSettingsToUser'; -import { dbSchemaV1, dbSchemaV2, dbSchemaV3, dbSchemaV4, dbSchemaV5, dbSchemaV6 } from './schemas'; +import { + dbSchemaV1, + dbSchemaV2, + dbSchemaV3, + dbSchemaV4, + dbSchemaV5, + dbSchemaV6, + dbSchemaV7, +} from './schemas'; import { DBModel, LOBE_CHAT_LOCAL_DB_NAME } from './types/db'; interface LobeDBSchemaMap { @@ -50,6 +58,10 @@ export class LocalDB extends Dexie { .stores(dbSchemaV6) .upgrade((trans) => this.upgradeToV6(trans)); + this.version(7) + .stores(dbSchemaV7) + .upgrade((trans) => this.upgradeToV7(trans)); + this.files = this.table('files'); this.sessions = this.table('sessions'); this.messages = this.table('messages'); @@ -115,6 +127,18 @@ export class LocalDB extends Dexie { if (!user.uuid) user.uuid = uuid(); }); }; + + /** + * 2024.03.14 + * add `id` in plugins + */ + upgradeToV7 = async (trans: Transaction) => { + const plugins = trans.table('plugins'); + + await plugins.toCollection().modify((plugin: DB_Plugin) => { + plugin.id = plugin.identifier; + }); + }; } export const LocalDBInstance = new LocalDB(); diff --git a/src/database/core/schemas.ts b/src/database/core/schemas.ts index 894d1bf0ae8c..f07a699c96c4 100644 --- a/src/database/core/schemas.ts +++ b/src/database/core/schemas.ts @@ -66,3 +66,13 @@ export const dbSchemaV6 = { '&id, role, content, fromModel, favorite, plugin.identifier, plugin.apiName, translate.content, createdAt, updatedAt, sessionId, topicId, quotaId, parentId, [sessionId+topicId], traceId', users: '++id, uuid', }; + +// ************************************** // +// ******* Version 7 - 2024-03-14 ******* // +// ************************************** // +// - Added id to `plugins` table +export const dbSchemaV7 = { + ...dbSchemaV6, + plugins: + '&identifier, id, type, manifest.type, manifest.meta.title, manifest.meta.description, manifest.meta.author, createdAt, updatedAt', +}; diff --git a/src/database/models/__tests__/plugin.test.ts b/src/database/models/__tests__/plugin.test.ts index 15541001e63c..236f16996dc8 100644 --- a/src/database/models/__tests__/plugin.test.ts +++ b/src/database/models/__tests__/plugin.test.ts @@ -10,6 +10,7 @@ describe('PluginModel', () => { // 设置正确结构的插件数据 pluginData = { identifier: 'test-plugin', + id: 'test-plugin', manifest: {}, type: 'plugin', }; diff --git a/src/database/models/plugin.ts b/src/database/models/plugin.ts index a12042ec893e..ba91966e5f05 100644 --- a/src/database/models/plugin.ts +++ b/src/database/models/plugin.ts @@ -1,8 +1,17 @@ +import { LobeChatPluginManifest } from '@lobehub/chat-plugin-sdk'; + import { BaseModel } from '@/database/core'; +import { LobeTool } from '@/types/tool'; import { merge } from '@/utils/merge'; import { DB_Plugin, DB_PluginSchema } from '../schemas/plugin'; +export interface InstallPluginParams { + identifier: string; + manifest?: LobeChatPluginManifest; + type: 'plugin' | 'customPlugin'; +} + class _PluginModel extends BaseModel { constructor() { super('plugins', DB_PluginSchema); @@ -15,14 +24,17 @@ class _PluginModel extends BaseModel { // **************** Create *************** // - create = async (plugin: DB_Plugin) => { + create = async (plugin: InstallPluginParams) => { const old = await this.table.get(plugin.identifier); + const dbPlugin = this.mapToDBPlugin(plugin); - return this.table.put(merge(old, plugin), plugin.identifier); + return this.table.put(merge(old, dbPlugin), plugin.identifier); }; - batchCreate = async (plugins: DB_Plugin[]) => { - return this._batchAdd(plugins); + batchCreate = async (plugins: LobeTool[]) => { + const dbPlugins = plugins.map((item) => this.mapToDBPlugin(item)); + + return this._batchAdd(dbPlugins); }; // **************** Delete *************** // @@ -38,6 +50,12 @@ class _PluginModel extends BaseModel { update: (id: string, value: Partial) => Promise = async (id, value) => { return this.table.update(id, value); }; + + // **************** Helper *************** // + + mapToDBPlugin(plugin: LobeTool) { + return { ...plugin, id: plugin.identifier } as DB_Plugin; + } } export const PluginModel = new _PluginModel(); diff --git a/src/database/schemas/plugin.ts b/src/database/schemas/plugin.ts index 84e5fa5f5e36..0f3dae905b5b 100644 --- a/src/database/schemas/plugin.ts +++ b/src/database/schemas/plugin.ts @@ -3,6 +3,7 @@ import { z } from 'zod'; export const DB_PluginSchema = z.object({ identifier: z.string(), + id: z.string(), type: z.enum(['plugin', 'customPlugin']), manifest: z.any().optional(), settings: z.any().optional(),