mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
feat(core): extract updateSessionOwnerId and renameSession to @openchatlab/core
Move pure SQL session CRUD operations (owner_id update, rename) to the shared core package as DatabaseAdapter functions. Electron wrappers now delegate to core, keeping only connection management and error handling.
This commit is contained in:
1 parent
1f11f94212
commit
c0e1647ab6
4 files changed
+32
-17
No files matched your search
@@ -6,7 +6,12 @@
|
||||
import Database from 'better-sqlite3'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { CHAT_DB_SCHEMA, FTS_TABLE_SCHEMA } from '@openchatlab/core'
|
||||
import {
|
||||
CHAT_DB_SCHEMA,
|
||||
FTS_TABLE_SCHEMA,
|
||||
updateSessionOwnerId as coreUpdateOwnerId,
|
||||
renameSession as coreRenameSession,
|
||||
} from '@openchatlab/core'
|
||||
import { BetterSqliteAdapter, writeParseResultToDb } from '@openchatlab/node-runtime'
|
||||
import type { ParseResult } from '../../../src/types/base'
|
||||
import { migrateDatabase, needsMigration, CURRENT_SCHEMA_VERSION } from './migrations'
|
||||
@@ -118,15 +123,11 @@ export function importData(parseResult: ParseResult): string {
|
||||
* 更新会话的 ownerId
|
||||
*/
|
||||
export function updateSessionOwnerId(sessionId: string, ownerId: string | null): boolean {
|
||||
// 使用带迁移的打开方式,确保 owner_id 列存在
|
||||
const db = openDatabaseWithMigration(sessionId)
|
||||
if (!db) {
|
||||
return false
|
||||
}
|
||||
if (!db) return false
|
||||
|
||||
try {
|
||||
const stmt = db.prepare('UPDATE meta SET owner_id = ?')
|
||||
stmt.run(ownerId)
|
||||
coreUpdateOwnerId(new BetterSqliteAdapter(db), ownerId)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('[Database] Failed to update session ownerId:', error)
|
||||
@@ -169,22 +170,18 @@ export function deleteSession(sessionId: string): boolean {
|
||||
*/
|
||||
export function renameSession(sessionId: string, newName: string): boolean {
|
||||
const dbPath = getDbPath(sessionId)
|
||||
if (!fs.existsSync(dbPath)) {
|
||||
return false
|
||||
}
|
||||
if (!fs.existsSync(dbPath)) return false
|
||||
|
||||
const db = new Database(dbPath)
|
||||
db.pragma('journal_mode = WAL')
|
||||
try {
|
||||
const db = new Database(dbPath)
|
||||
db.pragma('journal_mode = WAL')
|
||||
|
||||
const stmt = db.prepare('UPDATE meta SET name = ?')
|
||||
stmt.run(newName)
|
||||
|
||||
db.close()
|
||||
coreRenameSession(new BetterSqliteAdapter(db), newName)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('[Database] Failed to rename session:', error)
|
||||
return false
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -96,6 +96,8 @@ export {
|
||||
deleteMember,
|
||||
ensureAliasesColumn,
|
||||
ensureAvatarColumn,
|
||||
updateSessionOwnerId,
|
||||
renameSession,
|
||||
} from './query'
|
||||
|
||||
// 查询类型
|
||||
|
||||
@@ -21,6 +21,8 @@ export {
|
||||
getChatSessionSummary,
|
||||
saveChatSessionSummary,
|
||||
updateSessionGapThreshold,
|
||||
updateSessionOwnerId,
|
||||
renameSession,
|
||||
clearSessionIndex,
|
||||
generateSessionIndex,
|
||||
generateIncrementalSessionIndex,
|
||||
|
||||
@@ -584,6 +584,20 @@ export function updateSessionGapThreshold(db: DatabaseAdapter, gapThreshold: num
|
||||
db.prepare('UPDATE meta SET session_gap_threshold = ?').run(gapThreshold)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update session owner_id in meta table.
|
||||
*/
|
||||
export function updateSessionOwnerId(db: DatabaseAdapter, ownerId: string | null): void {
|
||||
db.prepare('UPDATE meta SET owner_id = ?').run(ownerId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename a session (update name in meta table).
|
||||
*/
|
||||
export function renameSession(db: DatabaseAdapter, newName: string): void {
|
||||
db.prepare('UPDATE meta SET name = ?').run(newName)
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all session index data (chat_session + message_context).
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user