Skip to content

Commit

Permalink
♻️ refactor: refactor the db model (lobehub#1567)
Browse files Browse the repository at this point in the history
* ♻️ refactor: refactor the db code

* ✅ test: fix test

* ♻️ refactor: refactor the  user model
  • Loading branch information
arvinxx committed Mar 14, 2024
1 parent 2cf2abd commit 3d56dd6
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 307 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ bun.lockb
sitemap*.xml
robots.txt

*.patch
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"uuid": "^9",
"yaml": "^2",
"zod": "^3",
"zustand": "^4.4",
"zustand": "^4.5.2",
"zustand-utils": "^1.3.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/plugin/store/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const GET = async (req: Request) => {

let res: Response;

res = await fetch(pluginStore.getPluginIndexUrl(locale as any));
res = await fetch(pluginStore.getPluginIndexUrl(locale as any), { next: { revalidate: 3600 } });

if (res.status === 404) {
res = await fetch(pluginStore.getPluginIndexUrl(DEFAULT_LANG));
Expand Down
165 changes: 88 additions & 77 deletions src/database/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,8 @@ class _MessageModel extends BaseModel {
constructor() {
super('messages', DB_MessageSchema);
}
async create(data: CreateMessageParams) {
const id = nanoid();

const messageData: DB_Message = this.mapChatMessageToDBMessage(data as ChatMessage);

return this._add(messageData, id);
}

async batchCreate(messages: ChatMessage[]) {
const data: DB_Message[] = messages.map((m) => this.mapChatMessageToDBMessage(m));

return this._batchAdd(data);
}
// **************** Query *************** //

async query({
sessionId,
Expand Down Expand Up @@ -91,45 +80,79 @@ class _MessageModel extends BaseModel {
return this.table.get(id);
}

async delete(id: string) {
return this.table.delete(id);
async queryAll() {
const data: DBModel<DB_Message>[] = await this.table.orderBy('updatedAt').toArray();

return data.map((element) => this.mapToChatMessage(element));
}

async clearTable() {
return this.table.clear();
async queryBySessionId(sessionId: string) {
return this.table.where('sessionId').equals(sessionId).toArray();
}

async update(id: string, data: DeepPartial<DB_Message>) {
return super._update(id, data);
queryByTopicId = async (topicId: string) => {
const dbMessages = await this.table.where('topicId').equals(topicId).toArray();

return dbMessages.map((message) => this.mapToChatMessage(message));
};

async count() {
return this.table.count();
}

async updatePluginState(id: string, key: string, value: any) {
const item = await this.findById(id);
// **************** Create *************** //

return this.update(id, { pluginState: { ...item.pluginState, [key]: value } });
async create(data: CreateMessageParams) {
const id = nanoid();

const messageData: DB_Message = this.mapChatMessageToDBMessage(data as ChatMessage);

return this._add(messageData, id);
}

/**
* Batch updates multiple fields of the specified messages.
*
* @param {string[]} messageIds - The identifiers of the messages to be updated.
* @param {Partial<DB_Message>} updateFields - An object containing the fields to update and their new values.
* @returns {Promise<number>} - The number of updated messages.
*/
async batchUpdate(messageIds: string[], updateFields: Partial<DB_Message>): Promise<number> {
// Retrieve the messages by their IDs
const messagesToUpdate = await this.table.where(':id').anyOf(messageIds).toArray();
async batchCreate(messages: ChatMessage[]) {
const data: DB_Message[] = messages.map((m) => this.mapChatMessageToDBMessage(m));

// Update the specified fields of each message
const updatedMessages = messagesToUpdate.map((message) => ({
...message,
...updateFields,
}));
return this._batchAdd(data);
}

// Use the bulkPut method to update the messages in bulk
await this.table.bulkPut(updatedMessages);
async duplicateMessages(messages: ChatMessage[]): Promise<ChatMessage[]> {
const duplicatedMessages = await this.createDuplicateMessages(messages);
// 批量添加复制后的消息到数据库
await this.batchCreate(duplicatedMessages);
return duplicatedMessages;
}

return updatedMessages.length;
async createDuplicateMessages(messages: ChatMessage[]): Promise<ChatMessage[]> {
// 创建一个映射来存储原始消息ID和复制消息ID之间的关系
const idMapping = new Map<string, string>();

// 首先复制所有消息,并为每个复制的消息生成新的ID
const duplicatedMessages = messages.map((originalMessage) => {
const newId = nanoid();
idMapping.set(originalMessage.id, newId);

return { ...originalMessage, id: newId };
});

// 更新 parentId 为复制后的新ID
for (const duplicatedMessage of duplicatedMessages) {
if (duplicatedMessage.parentId && idMapping.has(duplicatedMessage.parentId)) {
duplicatedMessage.parentId = idMapping.get(duplicatedMessage.parentId);
}
}

return duplicatedMessages;
}

// **************** Delete *************** //

async delete(id: string) {
return this.table.delete(id);
}

async clearTable() {
return this.table.clear();
}

/**
Expand Down Expand Up @@ -158,55 +181,43 @@ class _MessageModel extends BaseModel {
return this.table.bulkDelete(messageIds);
}

async queryAll() {
const data: DBModel<DB_Message>[] = await this.table.orderBy('updatedAt').toArray();

return data.map((element) => this.mapToChatMessage(element));
}

async count() {
return this.table.count();
}
// **************** Update *************** //

async queryBySessionId(sessionId: string) {
return this.table.where('sessionId').equals(sessionId).toArray();
async update(id: string, data: DeepPartial<DB_Message>) {
return this._update(id, data);
}

queryByTopicId = async (topicId: string) => {
const dbMessages = await this.table.where('topicId').equals(topicId).toArray();

return dbMessages.map((message) => this.mapToChatMessage(message));
};
async updatePluginState(id: string, key: string, value: any) {
const item = await this.findById(id);

async duplicateMessages(messages: ChatMessage[]): Promise<ChatMessage[]> {
const duplicatedMessages = await this.createDuplicateMessages(messages);
// 批量添加复制后的消息到数据库
await this.batchCreate(duplicatedMessages);
return duplicatedMessages;
return this.update(id, { pluginState: { ...item.pluginState, [key]: value } });
}

async createDuplicateMessages(messages: ChatMessage[]): Promise<ChatMessage[]> {
// 创建一个映射来存储原始消息ID和复制消息ID之间的关系
const idMapping = new Map<string, string>();

// 首先复制所有消息,并为每个复制的消息生成新的ID
const duplicatedMessages = messages.map((originalMessage) => {
const newId = nanoid();
idMapping.set(originalMessage.id, newId);
/**
* Batch updates multiple fields of the specified messages.
*
* @param {string[]} messageIds - The identifiers of the messages to be updated.
* @param {Partial<DB_Message>} updateFields - An object containing the fields to update and their new values.
* @returns {Promise<number>} - The number of updated messages.
*/
async batchUpdate(messageIds: string[], updateFields: Partial<DB_Message>): Promise<number> {
// Retrieve the messages by their IDs
const messagesToUpdate = await this.table.where(':id').anyOf(messageIds).toArray();

return { ...originalMessage, id: newId };
});
// Update the specified fields of each message
const updatedMessages = messagesToUpdate.map((message) => ({
...message,
...updateFields,
}));

// 更新 parentId 为复制后的新ID
for (const duplicatedMessage of duplicatedMessages) {
if (duplicatedMessage.parentId && idMapping.has(duplicatedMessage.parentId)) {
duplicatedMessage.parentId = idMapping.get(duplicatedMessage.parentId);
}
}
// Use the bulkPut method to update the messages in bulk
await this.table.bulkPut(updatedMessages);

return duplicatedMessages;
return updatedMessages.length;
}

// **************** Helper *************** //

private mapChatMessageToDBMessage(message: ChatMessage): DB_Message {
const { extra, ...messageData } = message;

Expand Down
13 changes: 9 additions & 4 deletions src/database/models/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ class _PluginModel extends BaseModel {
constructor() {
super('plugins', DB_PluginSchema);
}
// **************** Query *************** //

getList = async (): Promise<DB_Plugin[]> => {
return this.table.toArray();
};

// **************** Create *************** //

create = async (plugin: DB_Plugin) => {
const old = await this.table.get(plugin.identifier);

Expand All @@ -21,18 +24,20 @@ class _PluginModel extends BaseModel {
batchCreate = async (plugins: DB_Plugin[]) => {
return this._batchAdd(plugins);
};
// **************** Delete *************** //

delete(id: string) {
return this.table.delete(id);
}
clear() {
return this.table.clear();
}

// **************** Update *************** //

update: (id: string, value: Partial<DB_Plugin>) => Promise<number> = async (id, value) => {
return this.table.update(id, value);
};

clear() {
return this.table.clear();
}
}

export const PluginModel = new _PluginModel();
Loading

0 comments on commit 3d56dd6

Please sign in to comment.