Skip to content

Commit

Permalink
🗃️ refactor: upgrade plugin db schema (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Mar 14, 2024
1 parent 2c4ee81 commit 757574a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
26 changes: 25 additions & 1 deletion src/database/core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/database/core/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
1 change: 1 addition & 0 deletions src/database/models/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('PluginModel', () => {
// 设置正确结构的插件数据
pluginData = {
identifier: 'test-plugin',
id: 'test-plugin',
manifest: {},
type: 'plugin',
};
Expand Down
26 changes: 22 additions & 4 deletions src/database/models/plugin.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 *************** //

Expand All @@ -38,6 +50,12 @@ class _PluginModel extends BaseModel {
update: (id: string, value: Partial<DB_Plugin>) => Promise<number> = 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();
1 change: 1 addition & 0 deletions src/database/schemas/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 757574a

Please sign in to comment.