fix: sqlite api

This commit is contained in:
ikechan8370 2025-04-10 12:09:23 +08:00
parent 0c602d6cb3
commit 64c77877ac
6 changed files with 86 additions and 0 deletions

View file

@ -249,7 +249,16 @@ export class SQLiteChannelStorage extends ChaiteStorage {
*/
async setItem (id, channel) {
await this.ensureInitialized()
if (!id) {
id = this._generateId()
}
// 加上时间戳
if (!channel.createdAt) {
channel.createdAt = new Date().toISOString()
}
channel.updatedAt = new Date().toISOString()
// 转换为数据库记录
const record = this._channelToRecord(channel)
record.id = id // 确保ID是指定的ID

View file

@ -221,7 +221,16 @@ export class SQLiteChatPresetStorage extends ChaiteStorage {
*/
async setItem (id, preset) {
await this.ensureInitialized()
if (!id) {
id = this._generateId()
}
// 加上时间戳
if (!preset.createdAt) {
preset.createdAt = new Date().toISOString()
}
preset.updatedAt = new Date().toISOString()
// 转换为数据库记录
const record = this._presetToRecord(preset)
record.id = id // 确保ID是指定的ID

View file

@ -24,6 +24,45 @@ export async function checkMigrate () {
const dbPath = path.join(dataDir, 'data.db')
// 删除所有id为空的行
logger.debug('开始修复id为空的数据行...')
const collectionsToClean = ['channel', 'chat_presets', 'tools', 'processors']
for (const collectionName of collectionsToClean) {
try {
const collection = ChatGPTStorage.collection(collectionName)
const allItems = await collection.findAll()
const invalidItems = allItems.filter(item => !item.id)
if (invalidItems.length > 0) {
logger.info(`${collectionName}中发现${invalidItems.length}条id为空的数据正在修复...`)
for (const item of invalidItems) {
// 生成一个新的唯一ID
const newId = `generated_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`
// 更新时间戳
const now = new Date().toISOString()
// 更新项目
item.id = newId
item.createdAt = now
item.updatedAt = now
// 保存更新后的项目
await collection.set(newId, item)
// 移除旧的无ID项
await collection.remove(item)
}
logger.info(`已成功修复${collectionName}中的${invalidItems.length}条无效数据`)
} else {
logger.debug(`${collectionName}中没有发现id为空的数据`)
}
} catch (err) {
logger.error(`修复${collectionName}中id为空的数据时出错:`, err)
}
}
// 定义要检查的存储对
const storagePairs = [
{

View file

@ -185,7 +185,16 @@ export class SQLiteProcessorsStorage extends ChaiteStorage {
*/
async setItem (id, processor) {
await this.ensureInitialized()
if (!id) {
id = this._generateId()
}
// 加上时间戳
if (!processor.createdAt) {
processor.createdAt = new Date().toISOString()
}
processor.updatedAt = new Date().toISOString()
// 转换为数据库记录
const record = this._processorToRecord(processor)
record.id = id // 确保ID是指定的ID

View file

@ -100,7 +100,16 @@ export class SQLiteToolsGroupStorage extends ChaiteStorage {
*/
async setItem (id, data) {
await this.ensureInitialized()
if (!id) {
id = this._generateId()
}
// 加上时间戳
if (!data.createdAt) {
data.createdAt = new Date().toISOString()
}
data.updatedAt = new Date().toISOString()
// 提取工具组数据
const { name, description, tools } = data
const updatedAt = Date.now()

View file

@ -211,6 +211,17 @@ export class SQLiteToolsStorage extends ChaiteStorage {
async setItem (id, tool) {
await this.ensureInitialized()
if (!id) {
id = this._generateId()
}
// 加上时间戳
if (!tool.createdAt) {
tool.createdAt = new Date().toISOString()
}
tool.updatedAt = new Date().toISOString()
// 转换为数据库记录
const record = this._toolToRecord(tool)
record.id = id // 确保ID是指定的ID